Alert Dialog (알림 대화상자)
알림 대화상자는 사용자의 작업 흐름을 잠시 멈추고 중요한 메시지를 전달하거나 응답을 받기 위해 사용하는 모달 대화상자예요. 예를 들어 작업 확인(confirm) 프롬프트나 오류 메시지 확인에 쓰이죠. alertdialog 역할(role) 덕분에 보조 기술과 브라우저가 알림 대화상자를 다른 대화상자와 구분해서, 시스템 알림음 재생 같은 특별한 처리를 할 수 있게 돼요.
출처: 문서
본문
Import (불러오기)
import { AlertDialog } from "@kobalte/core/alert-dialog";
// or
import { Root, Trigger, ... } from "@kobalte/core/alert-dialog";
// or (deprecated)
import { AlertDialog } from "@kobalte/core";
Features (기능)
- WAI ARIA Alert Dialog 디자인 패턴을 따릅니다.
- 모달(modal)과 비모달(non-modal) 모드를 지원해요.
- 렌더링된 제목과 설명을 통해 화면 낭독기(screen reader)에 공지해요.
- 열려 있는 동안 포커스가 가둬지고 스크롤이 차단돼요.
- Esc 키를 누르면 알림 대화상자가 닫혀요.
- 제어 방식(controlled) 또는 비제어 방식(uncontrolled)으로 사용할 수 있어요.
Anatomy (구조)
알림 대화상자는 다음의 부분들로 이루어져요:
- AlertDialog: 대화상자의 모든 부분을 담고 있어요.
- AlertDialog.Trigger: 대화상자를 여는 버튼이에요.
- AlertDialog.Portal: 대화상자가 열렸을 때 자식들을
body로 전달(portal)해요. - AlertDialog.Overlay: 대화상자가 열렸을 때 뷰의 비활성 영역을 덮는 레이어예요.
- AlertDialog.Content: 대화상자가 열렸을 때 렌더링할 콘텐츠를 담아요.
- AlertDialog.CloseButton: 대화상자를 닫는 버튼이에요.
- AlertDialog.Title: 대화상자가 열렸을 때 공지할 수 있는 접근성 제목이에요.
- AlertDialog.Description: 대화상자가 열렸을 때 공지할 수 있는 선택적 접근성 설명이에요.
<AlertDialog>
<AlertDialog.Trigger />
<AlertDialog.Portal>
<AlertDialog.Overlay />
<AlertDialog.Content>
<AlertDialog.CloseButton />
<AlertDialog.Title />
<AlertDialog.Description />
</AlertDialog.Content>
</AlertDialog.Portal>
</AlertDialog>
Example (예제)
기본 예제를 통해 알림 대화상자의 구조와 스타일링 방식을 확인해 보세요. 인터랙티브한 미리보기는 출처 문서에서 직접 확인할 수 있어요.
Usage (사용법)
Default open (기본으로 열기)
초기 비제어 방식 열림 값을 defaultOpen prop으로 제공할 수 있어요.
<AlertDialog defaultOpen>...</AlertDialog>
Controlled open (제어 방식 열기)
open prop을 사용하면 열림 상태를 제어할 수 있어요. 사용자가 트리거, 닫기 버튼 또는 오버레이를 누르면 onOpenChange 이벤트가 발생하고 새 값이 전달돼요.
import { createSignal } from "solid-js";
function ControlledExample() {
const [open, setOpen] = createSignal(false);
return (
<AlertDialog open={open()} onOpenChange={setOpen}>
...
</AlertDialog>
);
}
API Reference (API 참조)
AlertDialog
AlertDialog는 @kobalte/core/alert-dialog의 Root 임포트(그리고 더 이상 사용되지 않는 AlertDialog.Root)와 동일해요.
| Prop | Description |
|---|---|
| open | boolean The controlled open state of the dialog. |
| defaultOpen | boolean The default open state when initially rendered. Useful when you do not need to control the open state. |
| onOpenChange | (open: boolean) => void Event handler called when the open state of the dialog changes. |
| id | string A unique identifier for the component. The id is used to generate id attributes for nested components. If no id prop is provided, a generated id will be used. |
| modal | boolean Whether the dialog should be the only visible content for screen readers, when set to true: - interaction with outside elements will be disabled. - scroll will be locked. - focus will be locked inside the dialog content. - elements outside the dialog content will not be visible for screen readers. |
| preventScroll | boolean Whether the scroll should be locked even if the alert dialog is not modal. |
| forceMount | boolean Used to force mounting the dialog (portal, overlay and content) when more control is needed. Useful when controlling animation with SolidJS animation libraries. |
AlertDialog.Trigger
AlertDialog.Trigger는 Button으로 구성돼요.
| Data attribute | Description |
|---|---|
| data-expanded | Present when the dialog is open. |
| data-closed | Present when the dialog is close. |
AlertDialog.Content와 AlertDialog.Overlay는 동일한 data-attribute를 공유해요.
AlertDialog.Content
| Prop | Description |
|---|---|
| onOpenAutoFocus | (event: Event) => void Event handler called when focus moves into the component after opening. It can be prevented by calling event.preventDefault. |
| onCloseAutoFocus | (event: Event) => void Event handler called when focus moves to the trigger after closing. It can be prevented by calling event.preventDefault. |
| onEscapeKeyDown | (event: KeyboardEvent) => void Event handler called when the escape key is down. It can be prevented by calling event.preventDefault. |
| onPointerDownOutside | (event: PointerDownOutsideEvent) => void Event handler called when a pointer event occurs outside the bounds of the component. It can be prevented by calling event.preventDefault. |
| onFocusOutside | (event: FocusOutsideEvent) => void Event handler called when the focus moves outside the bounds of the component. It can be prevented by calling event.preventDefault. |
| onInteractOutside | (event: InteractOutsideEvent) => void Event handler called when an interaction (pointer or focus event) happens outside the bounds of the component. It can be prevented by calling event.preventDefault. |
Rendered elements (렌더링되는 요소)
| Component | Default rendered element |
|---|---|
AlertDialog |
none |
AlertDialog.Trigger |
button |
AlertDialog.Portal |
Portal |
AlertDialog.Overlay |
div |
AlertDialog.Content |
div |
AlertDialog.CloseButton |
button |
AlertDialog.Title |
h2 |
AlertDialog.Description |
p |
Accessibility (접근성)
Keyboard Interactions (키보드 상호작용)
| Key | Description |
|---|---|
| Space | When focus is on the trigger, opens/closes the dialog. |
| Enter | When focus is on the trigger, opens/closes the dialog. |
| Tab | Moves focus to the next focusable element. |
| Shift + Tab | Moves focus to the previous focusable element. |
| Esc | Closes the dialog and moves focus to the trigger. |
더 알아보기 (Learn more)
- Button 컴포넌트 — 트리거 버튼 구성
- Kobalte 시작하기 — 프로젝트 설정
- WAI ARIA Alert Dialog 패턴 — 접근성 가이드라인