Dialog
Dialog는 확인 액션, 선택 옵션 같은 일시적인 콘텐츠를 표시하는 부유 표면 컴포넌트입니다. 이 페이지에서는 다양한 예제와 props를 통해 Dialog 사용법을 설명합니다.
출처: 문서
본문
Dialog는 확인 액션, 선택 옵션 등과 같은 일시적인 콘텐츠를 표시하는 데 사용하는 부유 표면입니다. 모달처럼 화면 위에 떠서 사용자의 주의를 끌고 특정 작업을 완료하도록 유도할 때 유용합니다.
React 예제 (React examples)
기본 (Default)
import React from 'react'
import {Button} from '@primer/react'
import {Dialog} from '@primer/react/experimental'
export default function Default() {
const [isOpen, setIsOpen] = React.useState(false)
const buttonRef = React.useRef<HTMLButtonElement>(null)
const onDialogClose = React.useCallback(() => setIsOpen(false), [])
return (
<>
<Button ref={buttonRef} onClick={() => setIsOpen(!isOpen)}>
Show dialog
</Button>
{isOpen && (
<Dialog title="My Dialog" onClose={onDialogClose} returnFocusRef={buttonRef}>
This is where the dialog content would go.
</Dialog>
)}
</>
)
}
부제목과 함께 (With subtitle)
import React from 'react'
import {Button} from '@primer/react'
import {Dialog} from '@primer/react/experimental'
export default function Default() {
const [isOpen, setIsOpen] = React.useState(false)
const buttonRef = React.useRef<HTMLButtonElement>(null)
const onDialogClose = React.useCallback(() => setIsOpen(false), [])
return (
<>
<Button ref={buttonRef} onClick={() => setIsOpen(!isOpen)}>
Show dialog
</Button>
{isOpen && (
<Dialog
title="My Dialog"
subtitle="This subtitle adds additional context"
onClose={onDialogClose}
returnFocusRef={buttonRef}
>
This is where the dialog content would go.
</Dialog>
)}
</>
)
}
푸터 버튼과 함께 (With footer buttons)
import React from 'react'
import {Button} from '@primer/react'
import {Dialog} from '@primer/react/experimental'
export default function FooterButtons() {
const [isOpen, setIsOpen] = React.useState(false)
const buttonRef = React.useRef<HTMLButtonElement>(null)
const onDialogClose = React.useCallback(() => setIsOpen(false), [])
return (
<>
<Button ref={buttonRef} onClick={() => setIsOpen(!isOpen)}>
Show dialog
</Button>
{isOpen && (
<Dialog
title="My Dialog"
onClose={onDialogClose}
footerButtons={[
{buttonType: 'default', content: 'Cancel', onClick: onDialogClose},
{buttonType: 'danger', content: 'Delete the universe'},
{
buttonType: 'primary',
content: 'Continue',
onClick: () => {
alert('Continuing')
},
},
]}
returnFocusRef={buttonRef}
>
This is where the dialog content would go.
</Dialog>
)}
</>
)
}
사이드 시트 (오른쪽) (Side sheet - right)
일반 뷰포트에서의 Dialog 배치 가이드라인입니다.
import React from 'react'
import {Button} from '@primer/react'
import {Dialog} from '@primer/react/experimental'
export default function SideSheet() {
const [isOpen, setIsOpen] = React.useState(false)
const buttonRef = React.useRef<HTMLButtonElement>(null)
const onDialogClose = React.useCallback(() => setIsOpen(false), [])
return (
<>
<Button ref={buttonRef} onClick={() => setIsOpen(!isOpen)}>
Show dialog
</Button>
{isOpen && (
<Dialog title="My Dialog" onClose={onDialogClose} position="right" returnFocusRef={buttonRef}>
This is where the dialog content would go.
</Dialog>
)}
</>
)
}
반응형 배치: 좁은 뷰포트에서의 바텀 시트 (Responsive positioning: bottom-sheet in narrow viewports)
좁은 뷰포트에서의 Dialog 배치 가이드라인입니다.
import React from 'react'
import {Button} from '@primer/react'
import {Dialog} from '@primer/react/experimental'
export default function ResponsiveBottom() {
const [isOpen, setIsOpen] = React.useState(false)
const buttonRef = React.useRef<HTMLButtonElement>(null)
const onDialogClose = React.useCallback(() => setIsOpen(false), [])
return (
<>
<Button ref={buttonRef} onClick={() => setIsOpen(!isOpen)}>
Show dialog
</Button>
{isOpen && (
<Dialog
title="My Dialog"
onClose={onDialogClose}
position={{narrow: 'bottom', regular: 'center'}}
returnFocusRef={buttonRef}
>
This is where the dialog content would go.
</Dialog>
)}
</>
)
}
높이·너비 크기 변형 (Height and width size variant)
- Small: 콘텐츠가 아주 적은 Dialog에 사용합니다.
- Medium (기본값): 폼 작성이나 목록에서 옵션 선택 같은 대부분의 작업에 사용합니다.
- Large: medium 크기보다 본문에 더 많은 가로 공간이 필요한 콘텐츠(예: 댓글 입력 상자)에 적합합니다.
- Extra large: extra large Dialog를 사용하기 전에, 콘텐츠가 새 페이지에 더 적합한지 먼저 고려해 보세요.
기본적으로 Dialog 높이는 본문 콘텐츠의 높이에 맞춰 조정됩니다. 최대 높이에 도달하면 본문 콘텐츠가 스크롤됩니다.
이 예제는 'small' 높이·너비 변형을 사용합니다.
import React from 'react'
import {Button} from '@primer/react'
import {Dialog} from '@primer/react/experimental'
export default function Size() {
const [isOpen, setIsOpen] = React.useState(false)
const buttonRef = React.useRef<HTMLButtonElement>(null)
const onDialogClose = React.useCallback(() => setIsOpen(false), [])
return (
<>
<Button ref={buttonRef} onClick={() => setIsOpen(!isOpen)}>
Show dialog
</Button>
{isOpen && (
<Dialog title="My Dialog" onClose={onDialogClose} width="small" height="small" returnFocusRef={buttonRef}>
This is where the dialog content would go.
</Dialog>
)}
</>
)
}
더 많은 코드 예제 (More code examples)
Dialog의 Storybook 스토리를 참고하세요.
Props
Dialog
| Name | Default | Description |
|---|---|---|
title |
React.ReactNode — Dialog의 제목. 이 Dialog의 aria-label 역할도 합니다. |
|
subtitle |
React.ReactNode — Dialog의 부제목. 선택 사항. 제목 아래에 더 작은 크기와 낮은 대비로 렌더링됩니다. 이 Dialog의 aria-describedby 역할도 합니다. |
|
renderHeader |
React.FunctionComponent<React.PropsWithChildren> — 다이얼로그 헤더의 커스텀 렌더러를 제공합니다. 이 콘텐츠는 다이얼로그 본문 영역에 가장자리부터 끝까지 렌더링됩니다. 경고: 커스텀 렌더러를 쓰면 Primer UX 원칙을 위반할 수 있습니다. | |
renderBody |
React.FunctionComponent<React.PropsWithChildren> — 다이얼로그 본문의 커스텀 렌더 함수를 제공합니다. 경고: 커스텀 렌더러를 쓰면 Primer UX 원칙을 위반할 수 있습니다. | |
renderFooter |
React.FunctionComponent<React.PropsWithChildren> — 다이얼로그 푸터의 커스텀 렌더 함수를 제공합니다. 경고: 커스텀 렌더러를 쓰면 Primer UX 원칙을 위반할 수 있습니다. | |
footerButtons |
DialogButtonProps[] — Dialog 푸터에 렌더링할 버튼을 지정합니다. | |
onClose |
(gesture: 'close-button' | 'escape') => void — 다이얼로그를 닫는 제스처(Escape 키 누름 또는 오른쪽 위 'X' 클릭)가 사용되면 호출됩니다. gesture 인자는 닫는 데 사용된 제스처를 나타냅니다. | |
role |
'dialog' |
The ARIA role — 'dialog' | 'alertdialog'를 지정합니다. dialog modal과 alert dialog를 참고하세요. |
width |
'small' | 'medium' | 'large' | 'xlarge' | string | number — 다이얼로그의 너비. small: 296px, medium: 320px, large: 480px, xlarge: 640px. 유효한 CSS width 값(예: '400px', '80rem')이나 숫자(픽셀 너비로 해석)도 허용합니다. | |
height |
'small' | 'large' | 'auto' — 다이얼로그의 높이. small: 296x480, large: 480x640, auto: 콘텐츠에 따라 가변. | |
returnFocusRef |
React.RefObject — 다이얼로그가 닫힐 때 포커스를 되돌릴 요소입니다. | |
initialFocusRef |
React.RefObject — Dialog가 열릴 때 이 요소에 포커스를 둡니다. | |
position |
'center' | 'left' | 'right' | { narrow, regular, wide } — 다이얼로그의 위치입니다. | |
align |
'center' |
'top' | 'center' | 'bottom' — 다이얼로그의 세로 정렬. position이 'center'(기본값)일 때만 적용됩니다. 'top'은 화면 위에서 ~4rem 위치에, 'center'(기본값)는 세로로 중앙에, 'bottom'은 화면 아래쪽 근처에 배치합니다. |
className |
string | undefined — CSS 문자열 | |
style |
React.CSSProperties | undefined — React CSS 속성들 |
Dialog.Body
문서화된 props가 없습니다.
Dialog.Buttons
| Name | Default | Description |
|---|---|---|
buttons |
Required | DialogButtonProps[] (아래 표 참고) |
Dialog.CloseButton
| Name | Default | Description |
|---|---|---|
onClose |
Required | () => void — Dialog를 닫는 콜백 |
Dialog.Footer
문서화된 props가 없습니다.
Dialog.Header
문서화된 props가 없습니다.
Dialog.Title
문서화된 props가 없습니다.
DialogButtonProps
| Name | Default | Description |
|---|---|---|
buttonType |
'default' | 'danger' | 'primary' | 'normal' — 사용할 Button의 변형 | |
content |
Required | React.ReactNode — Button의 내부 텍스트 |
autoFocus |
boolean — true이고 이 버튼이 autoFocus를 true로 설정한 유일한 버튼이라면, 다이얼로그가 나타날 때 이 버튼에 자동으로 포커스를 둡니다. | |
ref |
React.RefObject — 렌더링된 Button의 DOM 노드 참조. focusTrap의 initialFocus와 함께 autoFocus와 같이 사용합니다. |
더 알아보기 (Learn more)
- Dialog 접근성 문서 — 포커스 관리와 ARIA 역할을 확인하세요.
- Dialog 가이드라인 문서 — 언제 Dialog를 써야 하는지 살펴보세요.
- Dialog
Storybook스토리에서 더 다양한 예제를 확인하세요.