Dialog (대화상자)
주 창이나 다른 대화상자 창 위에 겹쳐 표시되는 창 컴포넌트예요. 모달 대화상자 뒤의 콘텐츠는 비활성(inert) 상태가 되므로 사용자는 그와 상호작용할 수 없어요.
출처: 문서
본문
임포트 (Import)
import { Dialog } from "@kobalte/core/dialog";
// or
import { Root, Trigger, ... } from "@kobalte/core/dialog";
// or (deprecated)
import { Dialog } from "@kobalte/core";
기능 (Features)
- WAI-ARIA Dialog 디자인 패턴을 따라요.
- 모달(modal) 및 비모달(non-modal) 모드를 지원해요.
- 렌더된 제목(title)과 설명(description)을 통해 화면 낭독기 안내를 제공해요.
- 열려 있는 동안 포커스는 가둬지고 스크롤은 차단돼요.
- Esc 키를 누르면 대화상자가 닫혀요.
- 제어(controlled) 또는 비제어(uncontrolled) 방식으로 사용할 수 있어요.
구조 (Anatomy)
대화상자는 다음 요소로 구성돼요.
- Dialog: 대화상자의 모든 부분을 포함해요.
- Dialog.Trigger: 대화상자를 여는 버튼이에요.
- Dialog.Portal: 대화상자가 열렸을 때 자식들을
body로 포털(portal)해요. - Dialog.Overlay: 대화상자가 열렸을 때 보기의 비활성 부분을 덮는 레이어예요.
- Dialog.Content: 대화상자가 열렸을 때 렌더링될 콘텐츠를 포함해요.
- Dialog.CloseButton: 대화상자를 닫는 버튼이에요.
- Dialog.Title: 대화상자가 열렸을 때 공지될 접근 가능한 제목이에요.
- Dialog.Description: 대화상자가 열렸을 때 공지될 선택적 접근 가능한 설명이에요.
<Dialog>
<Dialog.Trigger />
<Dialog.Portal>
<Dialog.Overlay />
<Dialog.Content>
<Dialog.CloseButton />
<Dialog.Title />
<Dialog.Description />
</Dialog.Content>
</Dialog.Portal>
</Dialog>
예제 (Example)
기본 예제는 공식 문서 페이지에서 Dialog, Trigger, Portal, Overlay, Content, CloseButton, Title, Description 조합과 스타일시트로 확인할 수 있어요.
사용법 (Usage)
기본 열림 (Default open)
비제어 상태의 초기 열림 값은 defaultOpen prop으로 제공할 수 있어요.
<Dialog defaultOpen>...</Dialog>
제어된 열림 (Controlled open)
open prop을 사용해 열림 상태를 제어할 수 있어요. 사용자가 트리거, 닫기 버튼 또는 오버레이를 누르면 onOpenChange 이벤트가 발생하며 새 값을 받아요.
import { createSignal } from "solid-js";
function ControlledExample() {
const [open, setOpen] = createSignal(false);
return (
<Dialog open={open()} onOpenChange={setOpen}>
...
</Dialog>
);
}
API 참조 (API Reference)
Dialog
Dialog는 @kobalte/core/dialog의 Root 임포트(그리고 더 이상 사용되지 않는 Dialog.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 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. |
| translations | DialogIntlTranslations Localization strings. |
Dialog.Trigger
Dialog.Trigger는 Button으로 구성돼요.
| Data attribute | Description |
|---|---|
| data-expanded | Present when the dialog is open. |
| data-closed | Present when the dialog is close. |
Dialog.Content와 Dialog.Overlay는 동일한 data-attributes를 공유해요.
Dialog.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 |
|---|---|
Dialog |
none |
Dialog.Trigger |
button |
Dialog.Portal |
Portal |
Dialog.Overlay |
div |
Dialog.Content |
div |
Dialog.CloseButton |
button |
Dialog.Title |
h2 |
Dialog.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. |