알림 다이얼로그
알림 다이얼로그 (Alert Dialog)
진행하려면 사용자 응답이 필요한 고품질의 비스타일 React 알림 다이얼로그 컴포넌트예요.
본문
진행하기 위해 사용자 응답을 요구하는 고품질의 비스타일 React 알림 다이얼로그 컴포넌트예요.
데모 (Demo)
Tailwind
이 예시는 Tailwind CSS로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.tsx */
import { AlertDialog } from '@base-ui/react/alert-dialog';
const buttonClasses =
'flex h-8 items-center justify-center gap-2 border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 dark:text-white select-none hover:not-data-disabled:bg-neutral-100 dark:hover:not-data-disabled:bg-neutral-800 active:not-data-disabled:bg-neutral-200 dark:active:not-data-disabled:bg-neutral-700 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400';
const dangerButtonClasses = `${buttonClasses} text-red-700 dark:text-red-400`;
export default function ExampleAlertDialog() {
return (
<AlertDialog.Root>
<AlertDialog.Trigger className={dangerButtonClasses}>Discard draft</AlertDialog.Trigger>
<AlertDialog.Portal>
<AlertDialog.Backdrop className="fixed inset-0 min-h-dvh bg-black opacity-20 transition-opacity duration-150 data-ending-style:opacity-0 data-starting-style:opacity-0 dark:opacity-50 supports-[-webkit-touch-callout:none]:absolute" />
<AlertDialog.Popup className="fixed top-1/2 left-1/2 -mt-8 flex w-96 max-w-[calc(100vw-3rem)] -translate-x-1/2 -translate-y-1/2 flex-col gap-4 bg-white dark:bg-neutral-950 p-4 text-neutral-950 dark:text-white border border-neutral-950 dark:border-white shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:shadow-none transition-[scale,opacity] duration-100 ease-out data-ending-style:scale-[0.98] data-ending-style:opacity-0 data-starting-style:scale-[0.98] data-starting-style:opacity-0">
<div className="flex flex-col gap-1">
<AlertDialog.Title className="text-base font-bold">Discard draft?</AlertDialog.Title>
<AlertDialog.Description className="text-sm text-neutral-600 dark:text-neutral-400">
You can’t undo this action.
</AlertDialog.Description>
</div>
<div className="flex justify-end gap-3">
<AlertDialog.Close className={buttonClasses}>Cancel</AlertDialog.Close>
<AlertDialog.Close className={dangerButtonClasses}>Discard</AlertDialog.Close>
</div>
</AlertDialog.Popup>
</AlertDialog.Portal>
</AlertDialog.Root>
);
}
CSS Modules
이 예시는 CSS Modules로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.module.css */
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
&[data-color='red'] {
color: oklch(50.5% 0.213 27.518deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.4% 0.191 22.216deg);
}
}
@media (hover: hover) {
&:hover:not([data-disabled]) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not([data-disabled]) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
&[data-disabled] {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
}
.Backdrop {
position: fixed;
min-height: 100dvh;
inset: 0;
background-color: black;
opacity: 0.2;
transition: opacity 150ms;
/* iOS 26+: Ensure the backdrop covers the entire visible viewport. */
@supports (-webkit-touch-callout: none) {
position: absolute;
}
@media (prefers-color-scheme: dark) {
opacity: 0.5;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
}
}
.Popup {
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: 1rem;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 24rem;
max-width: calc(100vw - 3rem);
margin-top: -2rem;
padding: 1rem;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
transform: translate(-50%, -50%) scale(0.98);
}
}
.Intro {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.Title {
margin: 0;
font-size: 1rem;
line-height: 1.5rem;
font-weight: 700;
}
.Description {
margin: 0;
font-size: 0.875rem;
line-height: 1.25rem;
color: oklch(43.9% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.Actions {
display: flex;
justify-content: end;
gap: 0.75rem;
}
/* index.tsx */
import { AlertDialog } from '@base-ui/react/alert-dialog';
import styles from './index.module.css';
export default function ExampleAlertDialog() {
return (
<AlertDialog.Root>
<AlertDialog.Trigger data-color="red" className={styles.Button}>
Discard draft
</AlertDialog.Trigger>
<AlertDialog.Portal>
<AlertDialog.Backdrop className={styles.Backdrop} />
<AlertDialog.Popup className={styles.Popup}>
<div className={styles.Intro}>
<AlertDialog.Title className={styles.Title}>Discard draft?</AlertDialog.Title>
<AlertDialog.Description className={styles.Description}>
You can't undo this action.
</AlertDialog.Description>
</div>
<div className={styles.Actions}>
<AlertDialog.Close className={styles.Button}>Cancel</AlertDialog.Close>
<AlertDialog.Close data-color="red" className={styles.Button}>
Discard
</AlertDialog.Close>
</div>
</AlertDialog.Popup>
</AlertDialog.Portal>
</AlertDialog.Root>
);
}
구조 (Anatomy)
컴포넌트를 임포트하고 파트들을 조립하세요.
import { AlertDialog } from '@base-ui/react/alert-dialog';
<AlertDialog.Root>
<AlertDialog.Trigger />
<AlertDialog.Portal>
<AlertDialog.Backdrop />
<AlertDialog.Viewport>
<AlertDialog.Popup>
<AlertDialog.Title />
<AlertDialog.Description />
<AlertDialog.Close />
</AlertDialog.Popup>
</AlertDialog.Viewport>
</AlertDialog.Portal>
</AlertDialog.Root>;
예시 (Examples)
메뉴에서 열기
메뉴를 사용해 다이얼로그를 열려면 다이얼로그 상태를 제어하고, 메뉴 항목의 onClick 핸들러에서 명령형으로 열면 돼요.
데모 (Demo)
Tailwind
이 예시는 Tailwind CSS로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.tsx */
'use client';
import * as React from 'react';
import { AlertDialog } from '@base-ui/react/alert-dialog';
import { Menu } from '@base-ui/react/menu';
export default function ExampleAlertDialog() {
const [dialogOpen, setDialogOpen] = React.useState(false);
return (
<React.Fragment>
<Menu.Root>
<Menu.Trigger className={buttonClass}>
Playlist <CaretDownIcon />
</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner className="outline-hidden" sideOffset={8} align="start">
<Menu.Popup className="relative origin-[var(--transform-origin)] border border-neutral-950 bg-white py-1 text-neutral-950 shadow-[0.25rem_0.25rem_0] shadow-black/12 outline-hidden transition-[scale,opacity] duration-100 ease-out data-ending-style:scale-[0.98] data-ending-style:opacity-0 data-starting-style:scale-[0.98] data-starting-style:opacity-0 dark:border-white dark:bg-neutral-950 dark:text-white dark:shadow-none">
<Menu.Item className={itemClass}>Play</Menu.Item>
<Menu.Item className={itemClass}>Share</Menu.Item>
<Menu.Separator className="mx-1 my-1 h-px bg-neutral-950 dark:bg-white" />
{/* Open the alert dialog when the menu item is clicked */}
<Menu.Item className={itemClass} onClick={() => setDialogOpen(true)}>
Delete…
</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
{/* Control the alert dialog state */}
<AlertDialog.Root open={dialogOpen} onOpenChange={setDialogOpen}>
<AlertDialog.Portal>
<AlertDialog.Backdrop className="fixed inset-0 min-h-dvh bg-black opacity-20 transition-opacity duration-150 data-ending-style:opacity-0 data-starting-style:opacity-0 dark:opacity-50 supports-[-webkit-touch-callout:none]:absolute" />
<AlertDialog.Popup className="fixed top-1/2 left-1/2 -mt-8 flex w-96 max-w-[calc(100vw-3rem)] -translate-x-1/2 -translate-y-1/2 flex-col gap-4 bg-white dark:bg-neutral-950 p-4 text-neutral-950 dark:text-white border border-neutral-950 dark:border-white shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:shadow-none transition-[scale,opacity] duration-100 ease-out data-ending-style:scale-[0.98] data-ending-style:opacity-0 data-starting-style:scale-[0.98] data-starting-style:opacity-0">
<div className="flex flex-col gap-1">
<AlertDialog.Title className="text-base font-bold">
Delete playlist?
</AlertDialog.Title>
<AlertDialog.Description className="text-sm text-neutral-600 dark:text-neutral-400">
You can't undo this action.
</AlertDialog.Description>
</div>
<div className="flex justify-end gap-3">
<AlertDialog.Close className={buttonClass}>Cancel</AlertDialog.Close>
<AlertDialog.Close className={`${buttonClass} text-red-700 dark:text-red-400`}>
Delete
</AlertDialog.Close>
</div>
</AlertDialog.Popup>
</AlertDialog.Portal>
</AlertDialog.Root>
</React.Fragment>
);
}
const buttonClass =
'flex h-8 items-center justify-center gap-1.5 border border-neutral-950 bg-white px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 select-none hover:not-data-disabled:bg-neutral-100 active:not-data-disabled:bg-neutral-200 data-pressed:bg-neutral-100 dark:border-white dark:bg-neutral-950 dark:text-white dark:hover:not-data-disabled:bg-neutral-800 dark:active:not-data-disabled:bg-neutral-700 dark:data-pressed:bg-neutral-800 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white';
const itemClass =
"flex cursor-default py-2 pr-8 pl-4 text-sm leading-4 outline-hidden select-none data-highlighted:relative data-highlighted:z-0 data-highlighted:text-white data-highlighted:before:absolute data-highlighted:before:inset-x-1 data-highlighted:before:inset-y-0 data-highlighted:before:z-[-1] data-highlighted:before:bg-neutral-950 data-highlighted:before:content-[''] dark:data-highlighted:text-neutral-950 dark:data-highlighted:before:bg-white";
function CaretDownIcon(props: React.ComponentProps<'svg'>) {
return (
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="currentColor"
{...props}
style={{ display: 'block', ...props.style }}
>
<path d="M12 6H4l4 4.5z" />
</svg>
);
}
CSS Modules
이 예시는 CSS Modules로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.module.css */
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.375rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
&[data-color='red'] {
color: oklch(50.5% 0.213 27.518deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.4% 0.191 22.216deg);
}
}
@media (hover: hover) {
&:hover:not([data-disabled]) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not([data-disabled]) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&[data-pressed] {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.Positioner {
outline: 0;
}
.MenuPopup {
box-sizing: border-box;
position: relative;
outline: 0;
padding-block: 0.25rem;
border: 1px solid oklch(14.5% 0 0deg);
border-radius: 0;
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transform-origin: var(--transform-origin);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
transform: scale(0.98);
}
}
.MenuItem {
outline: 0;
cursor: default;
-webkit-user-select: none;
user-select: none;
padding-block: 0.5rem;
padding-left: 1rem;
padding-right: 2rem;
display: flex;
font-size: 0.875rem;
line-height: 1rem;
&[data-highlighted] {
z-index: 0;
position: relative;
color: white;
@media (prefers-color-scheme: dark) {
color: oklch(14.5% 0 0deg);
}
}
&[data-highlighted]::before {
content: '';
z-index: -1;
position: absolute;
inset-block: 0;
inset-inline: 0.25rem;
background-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
}
.Separator {
margin: 0.25rem;
height: 1px;
background-color: oklch(14.5% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: white;
}
}
.Backdrop {
position: fixed;
min-height: 100dvh;
inset: 0;
background-color: black;
opacity: 0.2;
transition: opacity 150ms;
/* iOS 26+: Ensure the backdrop covers the entire visible viewport. */
@supports (-webkit-touch-callout: none) {
position: absolute;
}
@media (prefers-color-scheme: dark) {
opacity: 0.5;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
}
}
.DialogPopup {
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: 1rem;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 24rem;
max-width: calc(100vw - 3rem);
margin-top: -2rem;
padding: 1rem;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
transform: translate(-50%, -50%) scale(0.98);
}
}
.Intro {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.Title {
margin: 0;
font-size: 1rem;
line-height: 1.5rem;
font-weight: 700;
}
.Description {
margin: 0;
font-size: 0.875rem;
line-height: 1.25rem;
color: oklch(43.9% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.Actions {
display: flex;
justify-content: end;
gap: 0.75rem;
}
/* index.tsx */
'use client';
import * as React from 'react';
import { AlertDialog } from '@base-ui/react/alert-dialog';
import { Menu } from '@base-ui/react/menu';
import styles from './index.module.css';
export default function ExampleAlertDialog() {
const [dialogOpen, setDialogOpen] = React.useState(false);
return (
<React.Fragment>
<Menu.Root>
<Menu.Trigger className={styles.Button}>
Playlist <CaretDownIcon />
</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner className={styles.Positioner} sideOffset={8} align="start">
<Menu.Popup className={styles.MenuPopup}>
<Menu.Item className={styles.MenuItem}>Play</Menu.Item>
<Menu.Item className={styles.MenuItem}>Share</Menu.Item>
<Menu.Separator className={styles.Separator} />
{/* Open the alert dialog when the menu item is clicked */}
<Menu.Item className={styles.MenuItem} onClick={() => setDialogOpen(true)}>
Delete…
</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
{/* Control the alert dialog state */}
<AlertDialog.Root open={dialogOpen} onOpenChange={setDialogOpen}>
<AlertDialog.Portal>
<AlertDialog.Backdrop className={styles.Backdrop} />
<AlertDialog.Popup className={styles.DialogPopup}>
<div className={styles.Intro}>
<AlertDialog.Title className={styles.Title}>Delete playlist?</AlertDialog.Title>
<AlertDialog.Description className={styles.Description}>
You can't undo this action.
</AlertDialog.Description>
</div>
<div className={styles.Actions}>
<AlertDialog.Close className={styles.Button}>Cancel</AlertDialog.Close>
<AlertDialog.Close data-color="red" className={styles.Button}>
Delete
</AlertDialog.Close>
</div>
</AlertDialog.Popup>
</AlertDialog.Portal>
</AlertDialog.Root>
</React.Fragment>
);
}
function CaretDownIcon(props: React.ComponentProps<'svg'>) {
return (
<svg
width="16"
height="16"
viewBox="0 0 16 16"
fill="currentColor"
{...props}
style={{ display: 'block', ...props.style }}
>
<path d="M12 6H4l4 4.5z" />
</svg>
);
}
닫기 확인 (Close confirmation)
이 예시는 부모 다이얼로그에 입력된 텍스트가 버려질 때 열리는 중첩 확인 다이얼로그를 보여줘요.
이를 구현하려면 두 다이얼로그 모두 제어(controlled) 상태여야 해요. 부모 다이얼로그의 onOpenChange 콜백이 닫기 요청을 받으면 확인 다이얼로그를 열면 돼요. 이렇게 하면 사용자가 백드롭을 클릭하거나 Esc 키를 누르거나 닫기 버튼을 클릭했을 때 확인이 자동으로 표시돼요.
[data-nested-dialog-open] 선택자와 var(--nested-dialogs) CSS 변수를 사용해 부모 다이얼로그의 스타일을 커스터마이즈할 수 있어요. 자식 다이얼로그의 백드롭은 렌더링되지 않으므로, 그 위에 있는 다이얼로그 뒤로 부모 다이얼로그를 깔끔하게 보여줄 수 있어요.
데모 (Demo)
Tailwind
이 예시는 Tailwind CSS로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.tsx */
'use client';
import * as React from 'react';
import { AlertDialog } from '@base-ui/react/alert-dialog';
import { Dialog } from '@base-ui/react/dialog';
export default function ExampleDialog() {
const [dialogOpen, setDialogOpen] = React.useState(false);
const [confirmationOpen, setConfirmationOpen] = React.useState(false);
const [textareaValue, setTextareaValue] = React.useState('');
const titleId = React.useId();
return (
<Dialog.Root
open={dialogOpen}
onOpenChange={(open) => {
// Show the close confirmation if there’s text in the textarea
if (!open && textareaValue) {
setConfirmationOpen(true);
} else {
// Reset the text area value
setTextareaValue('');
// Open or close the dialog normally
setDialogOpen(open);
}
}}
>
<Dialog.Trigger className="flex h-8 items-center justify-center gap-2 border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 dark:text-white select-none hover:not-data-disabled:bg-neutral-100 dark:hover:not-data-disabled:bg-neutral-800 active:not-data-disabled:bg-neutral-200 dark:active:not-data-disabled:bg-neutral-700 data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white">
Tweet
</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Backdrop className="fixed inset-0 min-h-dvh bg-black opacity-20 transition-opacity duration-150 data-ending-style:opacity-0 data-starting-style:opacity-0 dark:opacity-50 supports-[-webkit-touch-callout:none]:absolute" />
<Dialog.Popup className="fixed top-[calc(50%+1.25rem*var(--nested-dialogs))] left-1/2 -mt-8 flex w-96 max-w-[calc(100vw-3rem)] -translate-x-1/2 -translate-y-1/2 flex-col gap-1 scale-[calc(1-0.1*var(--nested-dialogs))] bg-white dark:bg-neutral-950 p-4 text-neutral-950 dark:text-white border border-neutral-950 dark:border-white shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:shadow-none transition-[top,scale,opacity] duration-100 ease-out after:absolute after:inset-0 after:bg-black/5 after:opacity-0 after:transition-opacity after:duration-100 after:ease-out after:pointer-events-none data-ending-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs))] data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-nested-dialog-open:after:opacity-100 data-starting-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs))] data-starting-style:scale-[0.96] data-starting-style:opacity-0">
<Dialog.Title id={titleId} className="text-base font-bold">
New tweet
</Dialog.Title>
<form
className="flex flex-col gap-4"
onSubmit={(event) => {
event.preventDefault();
// Close the dialog when submitting
setDialogOpen(false);
}}
>
<textarea
aria-labelledby={titleId}
required
className="min-h-32 w-full border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 p-2 text-sm any-pointer-coarse:text-base font-normal text-neutral-950 dark:text-white placeholder:text-neutral-500 dark:placeholder:text-neutral-400 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white"
placeholder="What’s on your mind?"
value={textareaValue}
onChange={(event) => setTextareaValue(event.target.value)}
/>
<div className="flex justify-end gap-3">
<Dialog.Close className="flex h-8 items-center justify-center gap-2 border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 dark:text-white select-none hover:not-data-disabled:bg-neutral-100 dark:hover:not-data-disabled:bg-neutral-800 active:not-data-disabled:bg-neutral-200 dark:active:not-data-disabled:bg-neutral-700 data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white">
Cancel
</Dialog.Close>
<button
type="submit"
className="flex h-8 items-center justify-center gap-2 border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 dark:text-white select-none hover:bg-neutral-100 dark:hover:bg-neutral-800 active:bg-neutral-200 dark:active:bg-neutral-700 disabled:border-neutral-500 disabled:text-neutral-500 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
>
Tweet
</button>
</div>
</form>
</Dialog.Popup>
</Dialog.Portal>
{/* Confirmation dialog */}
<AlertDialog.Root open={confirmationOpen} onOpenChange={setConfirmationOpen}>
<AlertDialog.Portal>
<AlertDialog.Popup className="fixed top-[calc(50%+1.25rem*var(--nested-dialogs))] left-1/2 -mt-8 flex w-96 max-w-[calc(100vw-3rem)] -translate-x-1/2 -translate-y-1/2 flex-col gap-4 scale-[calc(1-0.1*var(--nested-dialogs))] bg-white dark:bg-neutral-950 p-4 text-neutral-950 dark:text-white border border-neutral-950 dark:border-white shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:shadow-none transition-[top,scale,opacity] duration-100 ease-out after:absolute after:inset-0 after:bg-black/5 after:opacity-0 after:transition-opacity after:duration-100 after:ease-out after:pointer-events-none data-ending-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs))] data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-nested-dialog-open:after:opacity-100 data-starting-style:top-[calc(50%+0.25rem+1.25rem*var(--nested-dialogs))] data-starting-style:scale-[0.96] data-starting-style:opacity-0">
<div className="flex flex-col gap-1">
<AlertDialog.Title className="text-base font-bold">Discard tweet?</AlertDialog.Title>
<AlertDialog.Description className="text-sm text-neutral-600 dark:text-neutral-400">
Your tweet will be lost.
</AlertDialog.Description>
</div>
<div className="flex justify-end gap-3">
<AlertDialog.Close className="flex h-8 items-center justify-center gap-2 border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 dark:text-white select-none hover:not-data-disabled:bg-neutral-100 dark:hover:not-data-disabled:bg-neutral-800 active:not-data-disabled:bg-neutral-200 dark:active:not-data-disabled:bg-neutral-700 data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white">
Go back
</AlertDialog.Close>
<button
type="button"
className="flex h-8 items-center justify-center gap-2 border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 dark:text-white select-none hover:bg-neutral-100 dark:hover:bg-neutral-800 active:bg-neutral-200 dark:active:bg-neutral-700 disabled:border-neutral-500 disabled:text-neutral-500 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white"
onClick={() => {
setConfirmationOpen(false);
setDialogOpen(false);
}}
>
Discard
</button>
</div>
</AlertDialog.Popup>
</AlertDialog.Portal>
</AlertDialog.Root>
</Dialog.Root>
);
}
CSS Modules
이 예시는 CSS Modules로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.module.css */
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not([data-disabled]) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not([data-disabled]) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&[data-disabled] {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
.Backdrop {
position: fixed;
min-height: 100dvh;
inset: 0;
background-color: black;
opacity: 0.2;
transition: opacity 150ms;
/* iOS 26+: Ensure the backdrop covers the entire visible viewport. */
@supports (-webkit-touch-callout: none) {
position: absolute;
}
@media (prefers-color-scheme: dark) {
opacity: 0.5;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
}
}
.Popup {
box-sizing: border-box;
position: fixed;
top: 50%;
left: 50%;
display: flex;
flex-direction: column;
gap: 1rem;
width: 24rem;
max-width: calc(100vw - 3rem);
margin-top: -2rem;
padding: 1rem;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
translate: -50% calc(-50% + 1.25rem * var(--nested-dialogs));
scale: calc(1 - 0.1 * var(--nested-dialogs));
transition:
translate 100ms ease-out,
scale 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
&::after {
content: '';
position: absolute;
inset: 0;
background-color: rgb(0 0 0 / 0.05);
opacity: 0;
pointer-events: none;
transition: opacity 100ms ease-out;
}
&[data-nested-dialog-open]::after {
opacity: 1;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
translate: -50% calc(-50% + 0.25rem + 1.25rem * var(--nested-dialogs));
scale: 0.96;
}
}
.PopupBody,
.Intro {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.Title {
margin: 0;
font-size: 1rem;
line-height: 1.5rem;
font-weight: 700;
}
.Description {
margin: 0;
font-size: 0.875rem;
line-height: 1.25rem;
color: oklch(43.9% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.Actions {
display: flex;
justify-content: end;
gap: 0.75rem;
}
.TextareaContainer {
display: flex;
flex-direction: column;
gap: 1rem;
}
.Textarea {
box-sizing: border-box;
padding: 0.5rem;
margin: 0;
border-radius: 0;
border: 1px solid oklch(14.5% 0 0deg);
width: 100%;
min-height: 8rem;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1.25rem;
background-color: white;
color: oklch(14.5% 0 0deg);
@media (any-pointer: coarse) {
font-size: 1rem;
line-height: 1.5rem;
}
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
&::placeholder {
color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
&:focus {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
}
/* index.tsx */
'use client';
import * as React from 'react';
import { AlertDialog } from '@base-ui/react/alert-dialog';
import { Dialog } from '@base-ui/react/dialog';
import styles from './index.module.css';
export default function ExampleDialog() {
const [dialogOpen, setDialogOpen] = React.useState(false);
const [confirmationOpen, setConfirmationOpen] = React.useState(false);
const [textareaValue, setTextareaValue] = React.useState('');
const titleId = React.useId();
return (
<Dialog.Root
open={dialogOpen}
onOpenChange={(open) => {
// Show the close confirmation if there’s text in the textarea
if (!open && textareaValue) {
setConfirmationOpen(true);
} else {
// Reset the text area value
setTextareaValue('');
// Open or close the dialog normally
setDialogOpen(open);
}
}}
>
<Dialog.Trigger className={styles.Button}>Tweet</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Backdrop className={styles.Backdrop} />
<Dialog.Popup className={styles.Popup}>
<div className={styles.PopupBody}>
<Dialog.Title id={titleId} className={styles.Title}>
New tweet
</Dialog.Title>
<form
className={styles.TextareaContainer}
onSubmit={(event) => {
event.preventDefault();
// Close the dialog when submitting
setDialogOpen(false);
}}
>
<textarea
aria-labelledby={titleId}
required
className={styles.Textarea}
placeholder="What’s on your mind?"
value={textareaValue}
onChange={(event) => setTextareaValue(event.target.value)}
/>
<div className={styles.Actions}>
<Dialog.Close className={styles.Button}>Cancel</Dialog.Close>
<button type="submit" className={styles.Button}>
Tweet
</button>
</div>
</form>
</div>
</Dialog.Popup>
</Dialog.Portal>
{/* Confirmation dialog */}
<AlertDialog.Root open={confirmationOpen} onOpenChange={setConfirmationOpen}>
<AlertDialog.Portal>
<AlertDialog.Popup className={styles.Popup}>
<div className={styles.Intro}>
<AlertDialog.Title className={styles.Title}>Discard tweet?</AlertDialog.Title>
<AlertDialog.Description className={styles.Description}>
Your tweet will be lost.
</AlertDialog.Description>
</div>
<div className={styles.Actions}>
<AlertDialog.Close className={styles.Button}>Go back</AlertDialog.Close>
<button
type="button"
className={styles.Button}
onClick={() => {
setConfirmationOpen(false);
setDialogOpen(false);
}}
>
Discard
</button>
</div>
</AlertDialog.Popup>
</AlertDialog.Portal>
</AlertDialog.Root>
</Dialog.Root>
);
}
분리 트리거 (Detached triggers)
알림 다이얼로그는 <AlertDialog.Root> 컴포넌트의 안이나 밖에 위치한 트리거로 제어할 수 있어요.
간단한 1회성 상호작용의 경우 이 페이지 상단의 예시처럼 <AlertDialog.Trigger>를 <AlertDialog.Root> 안에 배치하세요.
하지만 다이얼로그의 콘텐츠를 트리거 옆에 정의하는 게 실용적이지 않다면 분리 트리거(detached trigger)를 사용할 수 있어요.
이는 <AlertDialog.Trigger>를 <AlertDialog.Root> 밖에 배치하고, AlertDialog.createHandle() 함수가 만든 handle로 연결하는 방식이에요.
핸들의 명령형 메서드(open(), openWithPayload() 등)는 같은 핸들을 사용하는 <AlertDialog.Root>가 마운트되어 있어야 해요.
핸들에 root가 연결되지 않은 상태(마운트 전 또는 언마운트 후)에서의 호출은 무시돼요. root가 마운트될 때마다 처음 상태로 시작해서, root가 연결되지 않았을 때의 호출은 재생되지 않고 이전 마운트의 열림 상태도 이어지지 않아요.
const demoAlertDialog = AlertDialog.createHandle();
// @highlight
// @highlight-text "handle={demoAlertDialog}"
<AlertDialog.Trigger handle={demoAlertDialog}>Open</AlertDialog.Trigger>
// @highlight
// @highlight-text "handle={demoAlertDialog}"
<AlertDialog.Root handle={demoAlertDialog}>
...
</AlertDialog.Root>
데모 (Demo)
Tailwind
이 예시는 Tailwind CSS로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.tsx */
'use client';
import * as React from 'react';
import { AlertDialog } from '@base-ui/react/alert-dialog';
const demoAlertDialog = AlertDialog.createHandle();
const buttonClasses =
'flex h-8 items-center justify-center gap-2 border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 dark:text-white select-none hover:not-data-disabled:bg-neutral-100 dark:hover:not-data-disabled:bg-neutral-800 active:not-data-disabled:bg-neutral-200 dark:active:not-data-disabled:bg-neutral-700 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400';
const dangerButtonClasses = `${buttonClasses} text-red-700 dark:text-red-400`;
export default function AlertDialogDetachedTriggersSimpleDemo() {
return (
<React.Fragment>
<AlertDialog.Trigger className={dangerButtonClasses} handle={demoAlertDialog}>
Discard draft
</AlertDialog.Trigger>
<AlertDialog.Root handle={demoAlertDialog}>
<AlertDialog.Portal>
<AlertDialog.Backdrop className="fixed inset-0 min-h-dvh bg-black opacity-20 transition-opacity duration-150 data-ending-style:opacity-0 data-starting-style:opacity-0 dark:opacity-50 supports-[-webkit-touch-callout:none]:absolute" />
<AlertDialog.Popup className="fixed top-1/2 left-1/2 -mt-8 flex w-96 max-w-[calc(100vw-3rem)] -translate-x-1/2 -translate-y-1/2 flex-col gap-4 bg-white dark:bg-neutral-950 p-4 text-neutral-950 dark:text-white border border-neutral-950 dark:border-white shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:shadow-none transition-[scale,opacity] duration-100 ease-out data-ending-style:scale-[0.98] data-ending-style:opacity-0 data-starting-style:scale-[0.98] data-starting-style:opacity-0">
<div className="flex flex-col gap-1">
<AlertDialog.Title className="text-base font-bold">Discard draft?</AlertDialog.Title>
<AlertDialog.Description className="text-sm text-neutral-600 dark:text-neutral-400">
This action cannot be undone.
</AlertDialog.Description>
</div>
<div className="flex justify-end gap-3">
<AlertDialog.Close className={buttonClasses}>Cancel</AlertDialog.Close>
<AlertDialog.Close className={dangerButtonClasses}>Discard</AlertDialog.Close>
</div>
</AlertDialog.Popup>
</AlertDialog.Portal>
</AlertDialog.Root>
</React.Fragment>
);
}
CSS Modules
이 예시는 CSS Modules로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.module.css */
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not([data-disabled]) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not([data-disabled]) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
&[data-disabled] {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
}
.DangerButton {
color: oklch(50.5% 0.213 27.518deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.4% 0.191 22.216deg);
}
}
.Backdrop {
position: fixed;
min-height: 100dvh;
inset: 0;
background-color: black;
opacity: 0.2;
transition: opacity 150ms;
/* iOS 26+: Ensure the backdrop covers the entire visible viewport. */
@supports (-webkit-touch-callout: none) {
position: absolute;
}
@media (prefers-color-scheme: dark) {
opacity: 0.5;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
}
}
.Popup {
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: 1rem;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 24rem;
max-width: calc(100vw - 3rem);
margin-top: -2rem;
padding: 1rem;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
transform: translate(-50%, -50%) scale(0.98);
}
}
.Intro {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.Title {
margin: 0;
font-size: 1rem;
line-height: 1.5rem;
font-weight: 700;
}
.Description {
margin: 0;
font-size: 0.875rem;
line-height: 1.25rem;
color: oklch(43.9% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.Actions {
display: flex;
justify-content: end;
gap: 0.75rem;
}
.Container {
display: flex;
gap: 8px;
flex-wrap: wrap;
justify-content: center;
}
/* index.tsx */
'use client';
import * as React from 'react';
import { AlertDialog } from '@base-ui/react/alert-dialog';
import styles from './index.module.css';
const demoAlertDialog = AlertDialog.createHandle();
export default function AlertDialogDetachedTriggersSimpleDemo() {
return (
<React.Fragment>
<AlertDialog.Trigger
className={`${styles.Button} ${styles.DangerButton}`}
handle={demoAlertDialog}
>
Discard draft
</AlertDialog.Trigger>
<AlertDialog.Root handle={demoAlertDialog}>
<AlertDialog.Portal>
<AlertDialog.Backdrop className={styles.Backdrop} />
<AlertDialog.Popup className={styles.Popup}>
<div className={styles.Intro}>
<AlertDialog.Title className={styles.Title}>Discard draft?</AlertDialog.Title>
<AlertDialog.Description className={styles.Description}>
This action cannot be undone.
</AlertDialog.Description>
</div>
<div className={styles.Actions}>
<AlertDialog.Close className={styles.Button}>Cancel</AlertDialog.Close>
<AlertDialog.Close className={`${styles.Button} ${styles.DangerButton}`}>
Discard
</AlertDialog.Close>
</div>
</AlertDialog.Popup>
</AlertDialog.Portal>
</AlertDialog.Root>
</React.Fragment>
);
}
여러 트리거 (Multiple triggers)
하나의 알림 다이얼로그를 여러 트리거 요소로 열 수 있어요.
여러 분리 트리거에 같은 handle을 사용하거나, 하나의 <AlertDialog.Root> 안에 여러 <AlertDialog.Trigger> 컴포넌트를 배치하면 됩니다.
<AlertDialog.Root>
<AlertDialog.Trigger>Trigger 1</AlertDialog.Trigger>
<AlertDialog.Trigger>Trigger 2</AlertDialog.Trigger>
...
</AlertDialog.Root>
const demoAlertDialog = AlertDialog.createHandle();
<AlertDialog.Trigger handle={demoAlertDialog}>Trigger 1</AlertDialog.Trigger>
<AlertDialog.Trigger handle={demoAlertDialog}>Trigger 2</AlertDialog.Trigger>
<AlertDialog.Root handle={demoAlertDialog}>
...
</AlertDialog.Root>
알림 다이얼로그는 어느 트리거가 열었는지에 따라 다른 콘텐츠를 렌더링할 수 있어요.
이는 <AlertDialog.Trigger>에 payload를 전달하고 <AlertDialog.Root>에서 함수-자식 패턴을 사용해 구현해요.
createHandle() 함수에 타입 인자를 제공해 payload를 강타입(strongly typed)으로 만들 수 있어요.
// @highlight
const demoAlertDialog = AlertDialog.createHandle<{ message: string }>();
// @highlight
// @highlight-text "payload"
<AlertDialog.Trigger handle={demoAlertDialog} payload={{ message: 'Trigger 1' }}>
Trigger 1
</AlertDialog.Trigger>
// @highlight
// @highlight-text "payload"
<AlertDialog.Trigger handle={demoAlertDialog} payload={{ message: 'Trigger 2' }}>
Trigger 2
</AlertDialog.Trigger>
<AlertDialog.Root handle={demoAlertDialog}>
{({ payload }) => ( // @highlight-text "payload"
<AlertDialog.Portal>
<AlertDialog.Popup>
<AlertDialog.Title>Alert dialog</AlertDialog.Title>
{payload !== undefined && ( // @highlight-text "payload"
<AlertDialog.Description>
Confirming {payload.message} {/* @highlight-text "payload" */}
</AlertDialog.Description>
)}
</AlertDialog.Popup>
</AlertDialog.Portal>
)}
</AlertDialog.Root>
여러 트리거를 사용한 제어 모드
<AlertDialog.Root>의 open과 onOpenChange prop을 사용해 알림 다이얼로그의 열림 상태를 외부에서 제어할 수 있어요.
이를 통해 애플리케이션 상태에 기반해 알림 다이얼로그의 표시 여부를 관리할 수 있어요.
여러 트리거를 사용할 때는 <AlertDialog.Root>의 triggerId prop과 각 <AlertDialog.Trigger>의 id prop으로 어떤 트리거가 활성인지 관리해야 해요.
별도의 onTriggerIdChange prop은 없다는 점에 유의하세요.
대신 onOpenChange 콜백이 상태 변경을 시작한 트리거 요소를 담은 추가 인자 eventDetails를 받아요.
데모 (Demo)
Tailwind
이 예시는 Tailwind CSS로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.tsx */
'use client';
import * as React from 'react';
import { AlertDialog } from '@base-ui/react/alert-dialog';
type AlertPayload = { message: string };
const demoAlertDialog = AlertDialog.createHandle<AlertPayload>();
const buttonClasses =
'flex h-8 items-center justify-center gap-2 border border-neutral-950 dark:border-white bg-white dark:bg-neutral-950 px-3 text-sm leading-none whitespace-nowrap font-normal text-neutral-950 dark:text-white select-none hover:not-data-disabled:bg-neutral-100 dark:hover:not-data-disabled:bg-neutral-800 active:not-data-disabled:bg-neutral-200 dark:active:not-data-disabled:bg-neutral-700 focus-visible:outline-2 focus-visible:-outline-offset-1 focus-visible:outline-neutral-950 dark:focus-visible:outline-white data-disabled:border-neutral-500 data-disabled:text-neutral-500 disabled:border-neutral-500 disabled:text-neutral-500 dark:data-disabled:border-neutral-400 dark:data-disabled:text-neutral-400';
const dangerButtonClasses = `${buttonClasses} text-red-700 dark:text-red-400`;
export default function AlertDialogDetachedTriggersControlledDemo() {
const [open, setOpen] = React.useState(false);
const [triggerId, setTriggerId] = React.useState<string | null>(null);
const handleOpenChange = (isOpen: boolean, eventDetails: AlertDialog.Root.ChangeEventDetails) => {
setOpen(isOpen);
setTriggerId(eventDetails.trigger?.id ?? null);
};
return (
<React.Fragment>
<div className="flex flex-wrap gap-2 justify-center">
<AlertDialog.Trigger
className={dangerButtonClasses}
handle={demoAlertDialog}
id="alert-trigger-1"
payload={{ message: 'Discard draft?' }}
>
Discard
</AlertDialog.Trigger>
<AlertDialog.Trigger
className={dangerButtonClasses}
handle={demoAlertDialog}
id="alert-trigger-2"
payload={{ message: 'Delete project?' }}
>
Delete
</AlertDialog.Trigger>
<AlertDialog.Trigger
className={buttonClasses}
handle={demoAlertDialog}
id="alert-trigger-3"
payload={{ message: 'Sign out?' }}
>
Sign out
</AlertDialog.Trigger>
<button
type="button"
className={buttonClasses}
onClick={() => {
setTriggerId('alert-trigger-2');
setOpen(true);
}}
>
Open programmatically
</button>
</div>
<AlertDialog.Root<AlertPayload>
handle={demoAlertDialog}
open={open}
onOpenChange={handleOpenChange}
triggerId={triggerId}
>
{({ payload }) => (
<AlertDialog.Portal>
<AlertDialog.Backdrop className="fixed inset-0 min-h-dvh bg-black opacity-20 transition-opacity duration-150 data-ending-style:opacity-0 data-starting-style:opacity-0 dark:opacity-50 supports-[-webkit-touch-callout:none]:absolute" />
<AlertDialog.Popup className="fixed top-1/2 left-1/2 -mt-8 flex w-96 max-w-[calc(100vw-3rem)] -translate-x-1/2 -translate-y-1/2 flex-col gap-4 bg-white dark:bg-neutral-950 p-4 text-neutral-950 dark:text-white border border-neutral-950 dark:border-white shadow-[0.25rem_0.25rem_0] shadow-black/12 dark:shadow-none transition-[scale,opacity] duration-100 ease-out data-ending-style:scale-[0.98] data-ending-style:opacity-0 data-starting-style:scale-[0.98] data-starting-style:opacity-0">
<div className="flex flex-col gap-1">
<AlertDialog.Title className="text-base font-bold">
{payload?.message ?? 'Are you sure?'}
</AlertDialog.Title>
<AlertDialog.Description className="text-sm text-neutral-600 dark:text-neutral-400">
This action cannot be undone.
</AlertDialog.Description>
</div>
<div className="flex justify-end gap-3">
<AlertDialog.Close className={buttonClasses}>Cancel</AlertDialog.Close>
<AlertDialog.Close className={dangerButtonClasses}>Confirm</AlertDialog.Close>
</div>
</AlertDialog.Popup>
</AlertDialog.Portal>
)}
</AlertDialog.Root>
</React.Fragment>
);
}
CSS Modules
이 예시는 CSS Modules로 컴포넌트를 구현하는 방법을 보여줘요.
/* index.module.css */
.Button {
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
gap: 0.5rem;
height: 2rem;
padding: 0 0.75rem;
margin: 0;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
font-family: inherit;
font-size: 0.875rem;
font-weight: 400;
line-height: 1;
white-space: nowrap;
color: oklch(14.5% 0 0deg);
-webkit-user-select: none;
user-select: none;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
}
@media (hover: hover) {
&:hover:not([data-disabled]) {
background-color: oklch(97% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(26.9% 0 0deg);
}
}
}
&:active:not([data-disabled]) {
background-color: oklch(92.2% 0 0deg);
@media (prefers-color-scheme: dark) {
background-color: oklch(37.1% 0 0deg);
}
}
&:focus-visible {
outline: 2px solid oklch(14.5% 0 0deg);
outline-offset: -1px;
@media (prefers-color-scheme: dark) {
outline-color: white;
}
}
&[data-disabled] {
color: oklch(55.6% 0 0deg);
border-color: oklch(55.6% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
border-color: oklch(70.8% 0 0deg);
}
}
}
.DangerButton {
color: oklch(50.5% 0.213 27.518deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.4% 0.191 22.216deg);
}
}
.Backdrop {
position: fixed;
min-height: 100dvh;
inset: 0;
background-color: black;
opacity: 0.2;
transition: opacity 150ms;
/* iOS 26+: Ensure the backdrop covers the entire visible viewport. */
@supports (-webkit-touch-callout: none) {
position: absolute;
}
@media (prefers-color-scheme: dark) {
opacity: 0.5;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
}
}
.Popup {
box-sizing: border-box;
display: flex;
flex-direction: column;
gap: 1rem;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 24rem;
max-width: calc(100vw - 3rem);
margin-top: -2rem;
padding: 1rem;
border: 1px solid oklch(14.5% 0 0deg);
background-color: white;
color: oklch(14.5% 0 0deg);
box-shadow: 0.25rem 0.25rem 0 rgb(0 0 0 / 12%);
transition:
transform 100ms ease-out,
opacity 100ms ease-out;
@media (prefers-color-scheme: dark) {
border: 1px solid white;
background-color: oklch(14.5% 0 0deg);
color: white;
box-shadow: none;
}
&[data-starting-style],
&[data-ending-style] {
opacity: 0;
transform: translate(-50%, -50%) scale(0.98);
}
}
.Intro {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.Title {
margin: 0;
font-size: 1rem;
line-height: 1.5rem;
font-weight: 700;
}
.Description {
margin: 0;
font-size: 0.875rem;
line-height: 1.25rem;
color: oklch(43.9% 0 0deg);
@media (prefers-color-scheme: dark) {
color: oklch(70.8% 0 0deg);
}
}
.Actions {
display: flex;
justify-content: end;
gap: 0.75rem;
}
.Container {
display: flex;
gap: 8px;
flex-wrap: wrap;
justify-content: center;
}
/* index.tsx */
'use client';
import * as React from 'react';
import { AlertDialog } from '@base-ui/react/alert-dialog';
import styles from './index.module.css';
type AlertPayload = { message: string };
const demoAlertDialog = AlertDialog.createHandle<AlertPayload>();
export default function AlertDialogDetachedTriggersControlledDemo() {
const [open, setOpen] = React.useState(false);
const [triggerId, setTriggerId] = React.useState<string | null>(null);
const handleOpenChange = (isOpen: boolean, eventDetails: AlertDialog.Root.ChangeEventDetails) => {
setOpen(isOpen);
setTriggerId(eventDetails.trigger?.id ?? null);
};
return (
<React.Fragment>
<div className={styles.Container}>
<AlertDialog.Trigger
className={`${styles.Button} ${styles.DangerButton}`}
handle={demoAlertDialog}
id="alert-trigger-1"
payload={{ message: 'Discard draft?' }}
>
Discard
</AlertDialog.Trigger>
<AlertDialog.Trigger
className={`${styles.Button} ${styles.DangerButton}`}
handle={demoAlertDialog}
id="alert-trigger-2"
payload={{ message: 'Delete project?' }}
>
Delete
</AlertDialog.Trigger>
<AlertDialog.Trigger
className={styles.Button}
handle={demoAlertDialog}
id="alert-trigger-3"
payload={{ message: 'Sign out?' }}
>
Sign out
</AlertDialog.Trigger>
<button
className={styles.Button}
type="button"
onClick={() => {
setTriggerId('alert-trigger-2');
setOpen(true);
}}
>
Open programmatically
</button>
</div>
<AlertDialog.Root<AlertPayload>
handle={demoAlertDialog}
open={open}
onOpenChange={handleOpenChange}
triggerId={triggerId}
>
{({ payload }) => (
<AlertDialog.Portal>
<AlertDialog.Backdrop className={styles.Backdrop} />
<AlertDialog.Popup className={styles.Popup}>
<div className={styles.Intro}>
<AlertDialog.Title className={styles.Title}>
{payload?.message ?? 'Are you sure?'}
</AlertDialog.Title>
<AlertDialog.Description className={styles.Description}>
This action cannot be undone.
</AlertDialog.Description>
</div>
<div className={styles.Actions}>
<AlertDialog.Close className={styles.Button}>Cancel</AlertDialog.Close>
<AlertDialog.Close className={`${styles.Button} ${styles.DangerButton}`}>
Confirm
</AlertDialog.Close>
</div>
</AlertDialog.Popup>
</AlertDialog.Portal>
)}
</AlertDialog.Root>
</React.Fragment>
);
}
API 레퍼런스 (API reference)
Root
알림 다이얼로그의 모든 파트를 그룹화해요.
자체 HTML 요소를 렌더링하지 않아요.
Root Props:
| Prop | Type | Default | Description |
| :--- | :--- | :------ | :--- |
| defaultOpen | boolean | false | 다이얼로그가 처음에 열려 있는지 여부예요. 제어되는 다이얼로그를 렌더링하려면 open prop을 대신 사용하세요. |
| open | boolean | - | 다이얼로그가 현재 열려 있는지 여부예요. |
| onOpenChange | ((open: boolean, eventDetails: AlertDialog.Root.ChangeEventDetails) => void) | - | 알림 다이얼로그가 열리거나 닫힐 때 호출되는 이벤트 핸들러예요. |
| actionsRef | React.RefObject<AlertDialog.Root.Actions \| null> | - | 명령형 액션에 대한 ref예요. unmount: 알림 다이얼로그를 수동으로 언마운트해요.
외부에서 제어하는 닫기 애니메이션이 끝난 후 호출하세요.close: 호출 시 알림 다이얼로그를 명령형으로 닫아요. |
| defaultTriggerId | string \| null | - | 다이얼로그와 연결되는 트리거의 ID예요.
이 값은 defaultOpen prop과 함께 사용해 처음부터 열린 다이얼로그를 만들 때 유용해요. |
| handle | AlertDialog.Handle<Payload> | - | 알림 다이얼로그를 트리거와 연결하는 핸들이에요.
지정하면 외부 트리거가 알림 다이얼로그의 열림 상태를 제어할 수 있게 해줘요.
AlertDialog.createHandle() 메서드로 만들 수 있어요. |
| onOpenChangeComplete | ((open: boolean) => void) | - | 다이얼로그가 열리거나 닫힐 때 애니메이션이 모두 끝난 후 호출되는 이벤트 핸들러예요. |
| triggerId | string \| null | - | 다이얼로그와 연결되는 트리거의 ID예요.
이 값은 open prop과 함께 사용해 제어되는 다이얼로그를 만들 때 유용해요.
다이얼로그가 비제어일 때(open prop이 설정되지 않았을 때)는 이 prop을 지정할 필요가 없어요. |
| children | React.ReactNode \| PayloadChildRenderFunction<Payload> | - | 다이얼로그의 콘텐츠예요.
일반 React 노드이거나 활성 트리거의 payload를 받는 렌더 함수일 수 있어요. |
Root.Props
Root props의 재내보내기(re-export)예요.
Root.State
type AlertDialogRootState = {};
Root.Actions
type AlertDialogRootActions = { unmount: () => void; close: () => void };
Root.ChangeEventReason
type AlertDialogRootChangeEventReason =
| 'trigger-press'
| 'outside-press'
| 'escape-key'
| 'close-press'
| 'focus-out'
| 'imperative-action'
| 'none';
Root.ChangeEventDetails
type AlertDialogRootChangeEventDetails = (
| { reason: 'trigger-press'; event: MouseEvent | PointerEvent | TouchEvent | KeyboardEvent }
| { reason: 'outside-press'; event: MouseEvent | PointerEvent | TouchEvent }
| { reason: 'escape-key'; event: KeyboardEvent }
| { reason: 'close-press'; event: MouseEvent | PointerEvent | KeyboardEvent }
| { reason: 'focus-out'; event: KeyboardEvent | FocusEvent }
| { reason: 'imperative-action'; event: Event }
| { reason: 'none'; event: Event }
) & {
/** Cancels Base UI from handling the event. */
cancel: () => void;
/** Allows the event to propagate in cases where Base UI will stop the propagation. */
allowPropagation: () => void;
/** Indicates whether the event has been canceled. */
isCanceled: boolean;
/** Indicates whether the event is allowed to propagate. */
isPropagationAllowed: boolean;
/** The element that triggered the event, if applicable. */
trigger: Element | undefined;
preventUnmountOnClose: preventUnmountOnClose;
};
Trigger
알림 다이얼로그를 여는 버튼이에요.
<button> 요소를 렌더링해요.
Trigger Props:
| Prop | Type | Default | Description |
| :--- | :--- | :------ | :--- |
| handle | AlertDialog.Handle<Payload> | - | 트리거를 알림 다이얼로그와 연결하는 핸들이에요.
AlertDialog.createHandle() 메서드로 만들 수 있어요. |
| nativeButton | boolean | true | render prop으로 요소를 대체할 때 네이티브 <button> 요소를 렌더링할지 여부예요.
렌더링되는 요소가 버튼이 아닐 때(예: <div>) false로 설정하세요. |
| payload | Payload | - | 다이얼로그가 열릴 때 전달할 payload예요. |
| id | string | - | 트리거의 ID예요. 렌더링되는 요소에 전달되는 것 외에도,
제어 모드에서 다이얼로그의 활성 트리거를 지정하는 데(DialogRoot triggerId prop) 사용돼요. |
| className | string \| ((state: AlertDialog.Trigger.State) => string \| undefined) | - | 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태를 기반으로 클래스를 반환하는 함수예요. |
| style | React.CSSProperties \| ((state: AlertDialog.Trigger.State) => React.CSSProperties \| undefined) | - | 요소에 적용되는 스타일, 또는 컴포넌트 상태를 기반으로 스타일 객체를 반환하는 함수예요. |
| render | ReactElement \| ((props: HTMLProps, state: AlertDialog.Trigger.State) => ReactElement) | - | 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요. |
Trigger Data Attributes:
| Attribute | Type | Description |
| :--- | :--- | :--- |
| data-popup-open | - | 해당 알림 다이얼로그가 열려 있을 때 존재해요. |
| data-disabled | - | 트리거가 비활성화됐을 때 존재해요. |
Trigger.Props
Trigger props의 재내보내기(re-export)예요.
Trigger.State
type AlertDialogTriggerState = {
/** Whether the trigger is currently disabled. */
disabled: boolean;
/** Whether the dialog is currently open and was opened by this trigger. */
open: boolean;
};
Portal
팝업을 DOM의 다른 부분으로 옮기는 포털 요소예요.
기본적으로 포털 요소는 <body>에 추가돼요.
<div> 요소를 렌더링해요.
Portal Props:
| Prop | Type | Default | Description |
| :--- | :--- | :------ | :--- |
| container | HTMLElement \| ShadowRoot \| React.RefObject<HTMLElement \| ShadowRoot \| null> \| null | - | 포털 요소를 렌더링할 부모 요소예요. |
| className | string \| ((state: AlertDialog.Portal.State) => string \| undefined) | - | 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태를 기반으로 클래스를 반환하는 함수예요. |
| style | React.CSSProperties \| ((state: AlertDialog.Portal.State) => React.CSSProperties \| undefined) | - | 요소에 적용되는 스타일, 또는 컴포넌트 상태를 기반으로 스타일 객체를 반환하는 함수예요. |
| keepMounted | boolean | false | 팝업이 숨겨져 있는 동안 포털을 DOM에 유지할지 여부예요. |
| render | ReactElement \| ((props: HTMLProps, state: AlertDialog.Portal.State) => ReactElement) | - | 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요. |
Portal.Props
Portal props의 재내보내기(re-export)예요.
Portal.State
type AlertDialogPortalState = {};
Backdrop
팝업 아래에 표시되는 오버레이예요.
<div> 요소를 렌더링해요.
Backdrop Props:
| Prop | Type | Default | Description |
| :--- | :--- | :------ | :--- |
| forceRender | boolean | false | 중첩된 경우에도 백드롭을 강제로 렌더링할지 여부예요. |
| className | string \| ((state: AlertDialog.Backdrop.State) => string \| undefined) | - | 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태를 기반으로 클래스를 반환하는 함수예요. |
| style | React.CSSProperties \| ((state: AlertDialog.Backdrop.State) => React.CSSProperties \| undefined) | - | 요소에 적용되는 스타일, 또는 컴포넌트 상태를 기반으로 스타일 객체를 반환하는 함수예요. |
| render | ReactElement \| ((props: HTMLProps, state: AlertDialog.Backdrop.State) => ReactElement) | - | 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요. |
Backdrop Data Attributes:
| Attribute | Type | Description |
| :--- | :--- | :--- |
| data-open | - | 다이얼로그가 열려 있을 때 존재해요. |
| data-closed | - | 다이얼로그가 닫혀 있을 때 존재해요. |
| data-starting-style | - | 다이얼로그가 들어오는 애니메이션을 시작할 때 존재해요. |
| data-ending-style | - | 다이얼로그가 나가는 애니메이션 중일 때 존재해요. |
Backdrop.Props
Backdrop props의 재내보내기(re-export)예요.
Backdrop.State
type AlertDialogBackdropState = {
/** Whether the dialog is currently open. */
open: boolean;
/** The transition status of the component. */
transitionStatus: TransitionStatus;
};
Popup
다이얼로그 콘텐츠를 위한 컨테이너예요.
<div> 요소를 렌더링해요.
Popup Props:
| Prop | Type | Default | Description |
| :--- | :--- | :------ | :--- |
| initialFocus | boolean \| React.RefObject<HTMLElement \| null> \| ((openType: InteractionType) => boolean \| void \| HTMLElement \| null) | - | 다이얼로그가 열릴 때 포커스할 요소를 결정해요.
기본적으로 포커스는 팝업 안의 첫 tabbable 요소로 이동하지만, 다이얼로그가 터치로 열리면 팝업 자체가 포커스되어 가상 키보드가 열리는 것을 피해요. false: 포커스를 옮기지 않아요.true: 기본 동작(첫 tabbable 요소 또는 팝업)에 따라 포커스를 옮겨요.RefObject: ref 요소로 포커스를 옮겨요.function: 상호작용 유형(mouse, touch, pen, keyboard)과 함께 호출돼요.
포커스할 요소를 반환하거나, 기본 동작을 사용하려면 true, 기본 동작으로 폴백하려면 null, 아무것도 하지 않으려면 false/undefined를 반환하세요. |
| finalFocus | boolean \| React.RefObject<HTMLElement \| null> \| ((closeType: InteractionType) => boolean \| void \| HTMLElement \| null) | - | 다이얼로그가 닫힐 때 포커스할 요소를 결정해요. false: 포커스를 옮기지 않아요.true: 기본 동작(트리거 또는 이전에 포커스된 요소)에 따라 포커스를 옮겨요.RefObject: ref 요소로 포커스를 옮겨요.function: 상호작용 유형(mouse, touch, pen, keyboard)과 함께 호출돼요.
포커스할 요소를 반환하거나, 기본 동작을 사용하려면 true, 기본 동작으로 폴백하려면 null, 아무것도 하지 않으려면 false/undefined를 반환하세요. |
| className | string \| ((state: AlertDialog.Popup.State) => string \| undefined) | - | 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태를 기반으로 클래스를 반환하는 함수예요. |
| style | React.CSSProperties \| ((state: AlertDialog.Popup.State) => React.CSSProperties \| undefined) | - | 요소에 적용되는 스타일, 또는 컴포넌트 상태를 기반으로 스타일 객체를 반환하는 함수예요. |
| render | ReactElement \| ((props: HTMLProps, state: AlertDialog.Popup.State) => ReactElement) | - | 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요. |
Popup Data Attributes:
| Attribute | Type | Description |
| :--- | :--- | :--- |
| data-open | - | 다이얼로그가 열려 있을 때 존재해요. |
| data-closed | - | 다이얼로그가 닫혀 있을 때 존재해요. |
| data-nested | - | 다이얼로그가 다른 다이얼로그 안에 중첩되어 있을 때 존재해요. |
| data-nested-dialog-open | - | 다이얼로그 안에 다른 열린 다이얼로그가 중첩되어 있을 때 존재해요. |
| data-starting-style | - | 다이얼로그가 들어오는 애니메이션을 시작할 때 존재해요. |
| data-ending-style | - | 다이얼로그가 나가는 애니메이션 중일 때 존재해요. |
Popup CSS Variables:
| Variable | Type | Description |
| :--- | :--- | :--- |
| --nested-dialogs | number | 얼마나 많은 다이얼로그가 중첩되어 있는지 나타내요. |
Popup.Props
Popup props의 재내보내기(re-export)예요.
Popup.State
type AlertDialogPopupState = {
/** Whether the dialog is currently open. */
open: boolean;
/** The transition status of the component. */
transitionStatus: TransitionStatus;
/** Whether the dialog is nested within a parent dialog. */
nested: boolean;
/** Whether the dialog has nested dialogs open. */
nestedDialogOpen: boolean;
};
Title
다이얼로그에 라벨을 붙이는 제목이에요.
<h2> 요소를 렌더링해요.
Title Props:
| Prop | Type | Default | Description |
| :--- | :--- | :------ | :--- |
| className | string \| ((state: AlertDialog.Title.State) => string \| undefined) | - | 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태를 기반으로 클래스를 반환하는 함수예요. |
| style | React.CSSProperties \| ((state: AlertDialog.Title.State) => React.CSSProperties \| undefined) | - | 요소에 적용되는 스타일, 또는 컴포넌트 상태를 기반으로 스타일 객체를 반환하는 함수예요. |
| render | ReactElement \| ((props: HTMLProps, state: AlertDialog.Title.State) => ReactElement) | - | 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요. |
Title.Props
Title props의 재내보내기(re-export)예요.
Title.State
type AlertDialogTitleState = {};
Description
다이얼로그에 대한 추가 정보를 담은 문단이에요.
<p> 요소를 렌더링해요.
Description Props:
| Prop | Type | Default | Description |
| :--- | :--- | :------ | :--- |
| className | string \| ((state: AlertDialog.Description.State) => string \| undefined) | - | 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태를 기반으로 클래스를 반환하는 함수예요. |
| style | React.CSSProperties \| ((state: AlertDialog.Description.State) => React.CSSProperties \| undefined) | - | 요소에 적용되는 스타일, 또는 컴포넌트 상태를 기반으로 스타일 객체를 반환하는 함수예요. |
| render | ReactElement \| ((props: HTMLProps, state: AlertDialog.Description.State) => ReactElement) | - | 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요. |
Description.Props
Description props의 재내보내기(re-export)예요.
Description.State
type AlertDialogDescriptionState = {};
Close
다이얼로그를 닫는 버튼이에요.
<button> 요소를 렌더링해요.
Close Props:
| Prop | Type | Default | Description |
| :--- | :--- | :------ | :--- |
| nativeButton | boolean | true | render prop으로 요소를 대체할 때 네이티브 <button> 요소를 렌더링할지 여부예요.
렌더링되는 요소가 버튼이 아닐 때(예: <div>) false로 설정하세요. |
| className | string \| ((state: AlertDialog.Close.State) => string \| undefined) | - | 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태를 기반으로 클래스를 반환하는 함수예요. |
| style | React.CSSProperties \| ((state: AlertDialog.Close.State) => React.CSSProperties \| undefined) | - | 요소에 적용되는 스타일, 또는 컴포넌트 상태를 기반으로 스타일 객체를 반환하는 함수예요. |
| render | ReactElement \| ((props: HTMLProps, state: AlertDialog.Close.State) => ReactElement) | - | 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요. |
Close Data Attributes:
| Attribute | Type | Description |
| :--- | :--- | :--- |
| data-disabled | - | 버튼이 비활성화됐을 때 존재해요. |
Close.Props
Close props의 재내보내기(re-export)예요.
Close.State
type AlertDialogCloseState = {
/** Whether the button is currently disabled. */
disabled: boolean;
};
Viewport
다이얼로그 팝업을 위한 포지셔닝 컨테이너로, 스크롤 가능하게 만들 수 있어요.
<div> 요소를 렌더링해요.
Viewport Props:
| Prop | Type | Default | Description |
| :--- | :--- | :------ | :--- |
| className | string \| ((state: AlertDialog.Viewport.State) => string \| undefined) | - | 요소에 적용되는 CSS 클래스, 또는 컴포넌트 상태를 기반으로 클래스를 반환하는 함수예요. |
| style | React.CSSProperties \| ((state: AlertDialog.Viewport.State) => React.CSSProperties \| undefined) | - | 요소에 적용되는 스타일, 또는 컴포넌트 상태를 기반으로 스타일 객체를 반환하는 함수예요. |
| render | ReactElement \| ((props: HTMLProps, state: AlertDialog.Viewport.State) => ReactElement) | - | 컴포넌트의 HTML 요소를 다른 태그로 대체하거나 다른 컴포넌트와 합성할 수 있게 해줘요. ReactElement 또는 렌더링할 요소를 반환하는 함수를 받아요. |
Viewport Data Attributes:
| Attribute | Type | Description |
| :--- | :--- | :--- |
| data-open | - | 다이얼로그가 열려 있을 때 존재해요. |
| data-closed | - | 다이얼로그가 닫혀 있을 때 존재해요. |
| data-nested | - | 다이얼로그가 다른 다이얼로그 안에 중첩되어 있을 때 존재해요. |
| data-nested-dialog-open | - | 다이얼로그 안에 다른 열린 다이얼로그가 중첩되어 있을 때 존재해요. |
| data-starting-style | - | 다이얼로그가 들어오는 애니메이션을 시작할 때 존재해요. |
| data-ending-style | - | 다이얼로그가 나가는 애니메이션 중일 때 존재해요. |
Viewport.Props
Viewport props의 재내보내기(re-export)예요.
Viewport.State
type AlertDialogViewportState = {
/** Whether the dialog is currently open. */
open: boolean;
/** The transition status of the component. */
transitionStatus: TransitionStatus;
/** Whether the dialog is nested within another dialog. */
nested: boolean;
/** Whether the dialog has nested dialogs open. */
nestedDialogOpen: boolean;
};
createHandle
AlertDialog.Root를 분리된 AlertDialog.Trigger 컴포넌트와 연결하기 위한 새 핸들을 만들어요.
반환 값 (Return Value):
type ReturnValue = AlertDialog.Handle<Payload>;
Handle
Alert Dialog를 명령형으로 제어하고, 분리된 AlertDialog.Trigger 컴포넌트를
AlertDialog.Root와 연결해요. AlertDialog.createHandle()로 만들고, root의 handle
prop과 root 밖에 렌더링되는 트리거들에 전달하세요.
명령형 메서드는 이 핸들을 사용하는 root가 마운트되어 있을 때만 적용되며, root가
연결되기 전(또는 언마운트 후)에 호출된 호출은 무시돼요.
Properties:
| Property | Type | Modifiers | Description |
| :--- | :--- | :--- | :--- |
| isOpen | boolean | readonly | 다이얼로그가 현재 열려 있는지 여부예요. 핸들에 root가 연결되지 않았으면 false를 반환해요. |
Methods:
function open(triggerId: string | null): void;
선택적으로 트리거와 연결하면서 다이얼로그를 열어요.
이 메서드는 이벤트 핸들러나 이펙트 안에서만 호출해야 해요(렌더링 중에는 안 됨).
function openWithPayload(payload: Payload): void;
트리거와 연결하지 않고 주어진 payload로 다이얼로그를 열어요.
이 메서드는 이벤트 핸들러나 이펙트 안에서만 호출해야 해요(렌더링 중에는 안 됨).
function close(): void;
다이얼로그를 닫아요.
이 메서드는 이벤트 핸들러나 이펙트 안에서만 호출해야 해요(렌더링 중에는 안 됨).
type PayloadChildRenderFunction = (arg: { payload: unknown | undefined }) => ReactNode;
preventUnmountOnClose
type preventUnmountOnClose = () => void;
InteractionType
type InteractionType = 'mouse' | 'touch' | 'pen' | 'keyboard' | '';
Export Groups
AlertDialog.Root:AlertDialog.Root,AlertDialog.Root.State,AlertDialog.Root.Props,AlertDialog.Root.Actions,AlertDialog.Root.ChangeEventReason,AlertDialog.Root.ChangeEventDetailsAlertDialog.Backdrop:AlertDialog.Backdrop,AlertDialog.Backdrop.Props,AlertDialog.Backdrop.StateAlertDialog.Close:AlertDialog.Close,AlertDialog.Close.Props,AlertDialog.Close.StateAlertDialog.Description:AlertDialog.Description,AlertDialog.Description.Props,AlertDialog.Description.StateAlertDialog.Popup:AlertDialog.Popup,AlertDialog.Popup.Props,AlertDialog.Popup.StateAlertDialog.Portal:AlertDialog.Portal,AlertDialog.Portal.State,AlertDialog.Portal.PropsAlertDialog.Title:AlertDialog.Title,AlertDialog.Title.Props,AlertDialog.Title.StateAlertDialog.Trigger:AlertDialog.Trigger,AlertDialog.Trigger.Props,AlertDialog.Trigger.StateAlertDialog.Viewport:AlertDialog.Viewport,AlertDialog.Viewport.State,AlertDialog.Viewport.PropsAlertDialog.HandleAlertDialog.createHandleDefault:AlertDialogBackdropProps,AlertDialogBackdropState,AlertDialogCloseProps,AlertDialogCloseState,AlertDialogDescriptionProps,AlertDialogDescriptionState,AlertDialogPopupProps,AlertDialogPopupState,AlertDialogPortalProps,AlertDialogPortalState,AlertDialogTitleProps,AlertDialogTitleState,AlertDialogViewportProps,AlertDialogViewportState,AlertDialogRootState,AlertDialogRootProps,AlertDialogRootActions,AlertDialogRootChangeEventReason,AlertDialogRootChangeEventDetails,AlertDialogTriggerProps,AlertDialogTriggerState
Canonical Types
Canonical: Alias 매핑 — Canonical의 네임스페이스가 이미 임포트되어 있으면 Canonical을, 아니면 Alias를 사용하세요.
AlertDialog.Root.State:AlertDialogRootStateAlertDialog.Root.Props:AlertDialogRootPropsAlertDialog.Root.Actions:AlertDialogRootActionsAlertDialog.Root.ChangeEventReason:AlertDialogRootChangeEventReasonAlertDialog.Root.ChangeEventDetails:AlertDialogRootChangeEventDetailsAlertDialog.Backdrop.Props:AlertDialogBackdropPropsAlertDialog.Backdrop.State:AlertDialogBackdropStateAlertDialog.Close.Props:AlertDialogClosePropsAlertDialog.Close.State:AlertDialogCloseStateAlertDialog.Description.Props:AlertDialogDescriptionPropsAlertDialog.Description.State:AlertDialogDescriptionStateAlertDialog.Popup.Props:AlertDialogPopupPropsAlertDialog.Popup.State:AlertDialogPopupStateAlertDialog.Portal.State:AlertDialogPortalStateAlertDialog.Portal.Props:AlertDialogPortalPropsAlertDialog.Title.Props:AlertDialogTitlePropsAlertDialog.Title.State:AlertDialogTitleStateAlertDialog.Trigger.Props:AlertDialogTriggerPropsAlertDialog.Trigger.State:AlertDialogTriggerStateAlertDialog.Viewport.State:AlertDialogViewportStateAlertDialog.Viewport.Props:AlertDialogViewportProps