ConfirmationDialog
ConfirmationDialog는 사용자 액션을 확인하는 데 사용되는 특화된 대화상자(dialog) 컴포넌트입니다. 잠재적으로 파괴적이거나 중요한 액션을 확인하거나 취소하도록 사용자에게 묻는 간단한 방법을 제공합니다. 이 페이지에서는 기본, 커스텀 버튼, danger 확인, 다양한 크기, useConfirm hook 사용법과 props를 설명합니다.
출처: 문서
본문
ConfirmationDialog는 사용자 액션을 확인하는 데 사용되는 특화된 대화상자 컴포넌트입니다. 잠재적으로 파괴적이거나 중요한 액션을 확인하거나 취소하도록 사용자에게 묻는 간단한 방법을 제공합니다.
React 예시
기본 (Default)
import React from 'react'
import {Button, ConfirmationDialog} from '@primer/react'
export default function Default() {
const [open, setOpen] = React.useState(false)
return (
<>
<Button onClick={() => setOpen(true)}>Enable product</Button>
{open ? (
<ConfirmationDialog
onClose={() => setOpen(false)}
title="Delete product"
confirmButtonContent="Enable"
cancelButtonContent="Cancel"
>
Are you sure you want to enable this product? This action cannot be undone.
</ConfirmationDialog>
) : null}
</>
)
}
커스텀 버튼 콘텐츠 (With custom button content)
import React from 'react'
import {Button, ConfirmationDialog} from '@primer/react'
export default function CustomButtons() {
const [open, setOpen] = React.useState(false)
return (
<>
<Button onClick={() => setOpen(true)}>Enable product</Button>
{open ? (
<ConfirmationDialog
onClose={() => setOpen(false)}
title="Delete product"
confirmButtonContent="Custom Delete"
cancelButtonContent="Custom Cancel"
>
Are you sure you want to enable this product? This action cannot be undone.
</ConfirmationDialog>
) : null}
</>
)
}
Danger 확인 (Danger confirmation)
import React from 'react'
import {Button, ConfirmationDialog} from '@primer/react'
export default function Danger() {
const [open, setOpen] = React.useState(false)
return (
<>
<Button variant="danger" onClick={() => setOpen(true)}>
Delete product
</Button>
{open ? (
<ConfirmationDialog
onClose={() => setOpen(false)}
title="Delete product"
confirmButtonContent="Delete"
cancelButtonContent="Cancel"
confirmButtonType="danger"
>
Are you sure you want to delete this product? This action cannot be undone.
</ConfirmationDialog>
) : null}
</>
)
}
다양한 크기 (Different sizes)
ConfirmationDialog는 다양한 콘텐츠 길이에 맞추기 위해 여러 너비와 높이 크기를 지원합니다.
- Small: 아주 짧고 콘텐츠가 거의 없는 확인에 사용합니다.
- Medium (기본): 대부분의 확인 시나리오에 적합합니다.
- Large: 더 자세한 설명이 필요한 확인에 사용합니다.
import React from 'react'
import {Button, ConfirmationDialog} from '@primer/react'
export default function Sizes() {
const [open, setOpen] = React.useState(false)
return (
<>
<Button onClick={() => setOpen(true)}>Enable product</Button>
{open ? (
<ConfirmationDialog
onClose={() => setOpen(false)}
title="Delete product"
confirmButtonContent="Delete"
cancelButtonContent="Cancel"
width="large"
height="auto"
>
Are you sure you want to enable this product? This action cannot be undone.
</ConfirmationDialog>
) : null}
</>
)
}
useConfirm hook 사용 (Using the useConfirm hook)
useConfirm hook은 상태를 수동으로 관리하지 않고 명령형으로 확인 대화상자를 보여 주는 편리한 방법을 제공합니다.
import React, {useCallback} from 'react'
import {Button, useConfirm} from '@primer/react'
export default function UseConfirm() {
const confirm = useConfirm()
const onButtonClick = useCallback(
async (event: React.MouseEvent) => {
if (
(await confirm({title: 'Are you sure?', content: 'Do you really want to turn this button green?'})) &&
event.target instanceof HTMLElement
) {
event.target.style.color = 'var(--fgColor-success)'
event.target.textContent = "I'm green!"
}
},
[confirm],
)
return (
<div>
<Button onClick={onButtonClick}>Turn me green!</Button>
</div>
)
}
더 많은 코드 예시 (More code examples)
ConfirmationDialog Storybook stories 를 참고하세요.
Props
ConfirmationDialog
| 이름 (Name) | 기본값 (Default) | 설명 (Description) |
|---|---|---|
| onClose | Required | (gesture: 'confirm' | 'close-button' | 'cancel' | 'escape') => void — 대화상자를 닫는 제스처가 수행될 때 호출됩니다. 첫 번째 인자는 제스처를 나타냅니다 |
| title | Required | ConfirmationDialog의 제목입니다. 보통 짧은 질문입니다 |
| cancelButtonContent | Cancel | 취소 버튼에 사용할 텍스트입니다 |
| confirmButtonContent | OK | 확인 버튼에 사용할 텍스트입니다 |
| confirmButtonType | normal | 'normal' | 'primary' | 'danger' — 확인 버튼에 사용할 버튼 유형입니다 |
| overrideButtonFocus | 'confirm' | 'cancel' | 대화상자가 열릴 때 처음 포커스될 버튼입니다. 기본적으로 confirm 버튼을 처음 포커스하지만, confirm 버튼이 danger면 cancel 버튼을 포커스합니다. 이 prop은 위험한 액션이 실수로 취해질 수 있으므로 드물게 사용해야 합니다 |
| className | string | 대화상자에 적용할 추가 클래스 이름입니다 |
| width | medium | 'small' | 'medium' | 'large' | 'xlarge' — 대화상자의 너비입니다. small: 296px, medium: 320px, large: 480px, xlarge: 640px |
| height | 'small' | 'large' | 'auto' — 대화상자의 높이입니다. small: 480px, large: 640px, auto: 콘텐츠에 따라 변함 |
더 알아보기 (Learn more)
- ConfirmationDialog 가이드라인: 타이틀·버튼 라벨·키보드·포커스·ARIA 지침을 살펴보세요.
- ConfirmationDialog 접근성: 접근성 지침을 확인하세요.
- Primer Product 가이드: 다른 컴포넌트와 원칙을 둘러보세요.