다이얼로그 (Dialog)
내장 포커스 트랩, 스크롤 잠금, 드래그 지원, 닫기 가능한 백드롭이 있는 스타일 없는 모달 다이얼로그 컴포넌트예요.
출처: 문서
본문
Features (기능)
- 하위 컴포넌트(
Root, Trigger, Portal, Backdrop, Positioner, Popup, Header, Title, HeaderActions, Maximizable, Close, Content, Footer)를 갖춘 컴파운드 API예요.
- 9가지 위치 모드:
center, top, bottom, left, right, topleft, topright, bottomleft, bottomright.
- 열고 닫을 때 포커스를 자동 관리하는 통합 포커스 트랩이에요.
- 클릭-외부 감지가 있는 닫기 가능한 Backdrop이에요.
- 뷰포트 경계 강제가 있는 내장 드래그 지원이에요.
- 전체화면 토글이 있는 최대화(Maximizable) 모드예요.
blockScroll을 통한 스크롤 잠금 지원이에요.
Usage (사용법)
import { Dialog } from 'primereact/dialog';
<Dialog.Root>
<Dialog.Trigger></Dialog.Trigger>
<Dialog.Portal>
<Dialog.Backdrop />
<Dialog.Positioner>
<Dialog.Popup>
<Dialog.Header>
<Dialog.Title></Dialog.Title>
<Dialog.HeaderActions>
<Dialog.Maximizable></Dialog.Maximizable>
<Dialog.Close></Dialog.Close>
</Dialog.HeaderActions>
</Dialog.Header>
<Dialog.Content></Dialog.Content>
<Dialog.Footer></Dialog.Footer>
</Dialog.Popup>
</Dialog.Positioner>
</Dialog.Portal>
</Dialog.Root>
Behavior (동작)
모션 애니메이션 (Motion Animation)
<Dialog.Popup motionProps={{ name: 'p-dialog', appear: true }}>...</Dialog.Popup>
애니메이션 단계, CSS 변수, 숨김 전략에 대한 자세한 내용은 Motion을 참조하세요.
다형성 렌더링 (Polymorphic Rendering)
어떤 하위 컴포넌트든 as를 써서 렌더링되는 HTML 요소를 바꿀 수 있어요.
<Dialog.Trigger as="div">...</Dialog.Trigger>
<Dialog.Close as="a">...</Dialog.Close>
기본 요소: Root=div, Trigger=button, Positioner=div, Popup=div, Header=div, Title=div, HeaderActions=div, Maximizable=button, Close=button, Content=div, Footer=div이에요.
렌더 함수 children (Render Function Children)
하위 컴포넌트는 children으로 렌더 함수를 받아 컴포넌트 인스턴스에 접근할 수 있게 해줘요.
<Dialog.Content>{(instance) => <div>{instance.dialog?.state.open ? 'Open' : 'Closed'}</div>}</Dialog.Content>
API
DialogRoot
| Attribute |
Value |
data-open |
Present when the dialog is open |
data-closed |
Present when the dialog is closed |
data-maximized |
Present when the dialog is maximized |
| Label |
Type |
Description |
| root |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| positioner |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the positioner's DOM element. |
| backdrop |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the backdrop's DOM element. |
| trigger |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the trigger's DOM element. |
| header |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the header's DOM element. |
| title |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the title's DOM element. |
| headerActions |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the headerActions's DOM element. |
| maximizable |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the maximizable's DOM element. |
| close |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the close's DOM element. |
| content |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the content's DOM element. |
| footer |
DialogRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the footer's DOM element. |
DialogTrigger
| Attribute |
Value |
data-open |
Present when the dialog is open |
data-closed |
Present when the dialog is closed |
| Label |
Type |
Description |
| root |
DialogTriggerPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
DialogPortal
| Label |
Type |
Description |
| root |
DialogPortalPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
DialogBackdrop
애니메이션 오버레이 렌더링을 위해 내부적으로 Backdrop 컴포넌트를 사용해요.
| Label |
Type |
Description |
| root |
DialogBackdropPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
DialogPositioner
| Attribute |
Value |
data-position |
"center" | "top" | "bottom" | "left" | "right" | "topleft" | "topright" | "bottomleft" | "bottomright" |
data-scroll-behavior |
"inside" | "outside" |
data-open |
Present when the dialog is open |
data-closed |
Present when the dialog is closed |
data-maximized |
Present when the dialog is maximized |
| Label |
Type |
Description |
| root |
DialogPositionerPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Attribute |
Value |
data-open |
Present when the dialog is open |
data-maximized |
Present when the dialog is maximized |
포커스 트랩 경계를 위해 내부적으로 VisuallyHidden 요소를 사용해요.
| Label |
Type |
Description |
| root |
DialogPopupPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Label |
Type |
Description |
| root |
DialogHeaderPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
DialogTitle
| Label |
Type |
Description |
| root |
DialogTitlePassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Label |
Type |
Description |
| root |
DialogHeaderActionsPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
DialogMaximizable
| Attribute |
Value |
data-maximized |
Present when the dialog is maximized |
data-minimized |
Present when the dialog is not maximized |
| Label |
Type |
Description |
| root |
DialogMaximizablePassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
DialogClose
| Label |
Type |
Description |
| root |
DialogClosePassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
DialogContent
| Attribute |
Value |
data-maximized |
Present when the dialog is maximized |
| Label |
Type |
Description |
| root |
DialogContentPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| Label |
Type |
Description |
| root |
DialogFooterPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
Accessibility (접근성)
스크린 리더 (Screen Reader)
Dialog 컴포넌트는 dialog role을 사용하고 헤더 요소를 가리키는 aria-labelledby를 기본으로 쓰지만, 모든 속성은 루트 요소에 전달되므로 aria-labelledby로 이 기본 동작을 덮어쓸 수 있어요. 또한 포커스가 팝업 안에 유지되므로 aria-modal도 추가돼요.
Trigger 요소는 다이얼로그 상태를 나타내는 aria-expanded와 aria-controls도 가져요.
오버레이 키보드 지원 (Overlay Keyboard Support)
| Key |
Function |
tab |
Moves focus to the next the focusable element within the dialog if modal is true. Otherwise, the focusable element in the page tab sequence. |
shift + tab |
Moves focus to the previous the focusable element within the dialog if modal is true. Otherwise, the focusable element in the page tab sequence. |
escape |
Closes the dialog if closeOnEscape is true. |
| Key |
Function |
enter |
Closes the dialog. |
space |
Closes the dialog. |
더 알아보기 (Learn more)