Alert Dialog
A component used as a strong expression and warning means when user confirmation is essential.
import { ResponsivePair } from "@seed-design/react";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
const AlertDialogSingle = () => {
return (
// You can set z-index dialog with "--layer-index" custom property. useful for stackflow integration.
<AlertDialogRoot>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">Open</ActionButton>
</AlertDialogTrigger>
<AlertDialogContent layerIndex={50}>
<AlertDialogHeader>
<AlertDialogTitle>Caution</AlertDialogTitle>
<AlertDialogDescription>This action cannot be undone.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<ResponsivePair gap="x2">
<AlertDialogAction variant="neutralWeak">Cancel</AlertDialogAction>
<AlertDialogAction variant="neutralSolid">OK</AlertDialogAction>
</ResponsivePair>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
);
};
export default AlertDialogSingle;Stackflow
Learn how to use Stackflow and Alert Dialog together.
Installation
npx @seed-design/cli@latest add ui:alert-dialogpnpm dlx @seed-design/cli@latest add ui:alert-dialogyarn dlx @seed-design/cli@latest add ui:alert-dialogbun x @seed-design/cli@latest add ui:alert-dialogInstall dependencies
npm install @seed-design/reactyarn add @seed-design/reactpnpm add @seed-design/reactbun add @seed-design/reactCopy the code below and paste it in
/**
* @file ui:alert-dialog
* @requires @seed-design/react@^2.0.0
* @requires @seed-design/css@^2.0.0
**/
"use client";
import { Dialog } from "@seed-design/react";
import { forwardRef } from "react";
import { ActionButton, type ActionButtonProps } from "./action-button";
import type * as React from "react";
export interface AlertDialogRootProps extends Dialog.RootProps {
/**
* @default "alertdialog"
*/
role?: Dialog.RootProps["role"];
/**
* @default false
*/
closeOnInteractOutside?: Dialog.RootProps["closeOnInteractOutside"];
}
/**
* @see https://crispy.carrot-soft.tech/react/components/alert-dialog
*/
export const AlertDialogRoot = ({ children, ...otherProps }: AlertDialogRootProps) => {
return (
<Dialog.Root role="alertdialog" closeOnInteractOutside={false} {...otherProps}>
{children}
</Dialog.Root>
);
};
AlertDialogRoot.displayName = "AlertDialogRoot";
export interface AlertDialogContentProps extends Dialog.ContentProps {
layerIndex?: number;
}
export const AlertDialogContent = forwardRef<HTMLDivElement, AlertDialogContentProps>(
({ children, layerIndex, ...otherProps }, ref) => {
return (
<Dialog.Positioner style={{ "--layer-index": layerIndex } as React.CSSProperties}>
<Dialog.Backdrop />
<Dialog.Content ref={ref} {...otherProps}>
{children}
</Dialog.Content>
</Dialog.Positioner>
);
},
);
export interface AlertDialogTriggerProps extends Dialog.TriggerProps {}
export const AlertDialogTrigger = Dialog.Trigger;
export interface AlertDialogHeaderProps extends Dialog.HeaderProps {}
export const AlertDialogHeader = Dialog.Header;
export interface AlertDialogTitleProps extends Dialog.TitleProps {}
export const AlertDialogTitle = Dialog.Title;
export interface AlertDialogDescriptionProps extends Dialog.DescriptionProps {}
export const AlertDialogDescription = Dialog.Description;
export interface AlertDialogFooterProps extends Dialog.FooterProps {}
export const AlertDialogFooter = Dialog.Footer;
export interface AlertDialogActionProps
extends Omit<Dialog.ActionProps, "color">,
ActionButtonProps {}
export const AlertDialogAction = forwardRef<HTMLButtonElement, AlertDialogActionProps>(
(props, ref) => {
return (
<Dialog.Action asChild>
<ActionButton {...props} ref={ref} />
</Dialog.Action>
);
},
);
/**
* This file is a snippet from Crispy, helping you get started quickly with @seed-design/* packages.
* You can extend this snippet however you want.
*/
Props
AlertDialogRoot
Prop
Type
childrenReact.ReactNodeopen?boolean | undefineddefaultOpen?boolean | undefinedonOpenChange?((open: boolean, details?: DialogChangeDetails) => void) | undefinedAlertDialogTrigger
Prop
Type
AlertDialogContent
Prop
Type
layerIndex?number | undefinedAlertDialogHeader
Prop
Type
AlertDialogTitle
Prop
Type
AlertDialogDescription
Prop
Type
AlertDialogFooter
Prop
Type
Examples
Trigger
<AlertDialogTrigger> sets the aria-haspopup="dialog" attribute and automatically sets the aria-expanded attribute based on AlertDialog's open state. This attribute is useful for assistive technologies such as screen readers.
import { ResponsivePair } from "@seed-design/react";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
const AlertDialogSingle = () => {
return (
// You can set z-index dialog with "--layer-index" custom property. useful for stackflow integration.
<AlertDialogRoot>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">Open</ActionButton>
</AlertDialogTrigger>
<AlertDialogContent layerIndex={50}>
<AlertDialogHeader>
<AlertDialogTitle>Caution</AlertDialogTitle>
<AlertDialogDescription>This action cannot be undone.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<ResponsivePair gap="x2">
<AlertDialogAction variant="neutralWeak">Cancel</AlertDialogAction>
<AlertDialogAction variant="neutralSolid">OK</AlertDialogAction>
</ResponsivePair>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
);
};
export default AlertDialogSingle;Responsive Wrapping
Using the <ResponsivePair> component, when button content becomes long, you can fold the layout vertically.
import { PrefixIcon, ResponsivePair } from "@seed-design/react";
import { IconCheckFill } from "@seed-design/react-icon";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
const AlertDialogWrap = () => {
return (
// You can set z-index dialog with "--layer-index" custom property. useful for stackflow integration.
<AlertDialogRoot>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">열기</ActionButton>
</AlertDialogTrigger>
<AlertDialogContent layerIndex={50}>
<AlertDialogHeader>
<AlertDialogTitle>Wrapping</AlertDialogTitle>
<AlertDialogDescription>
ResponsivePair 컴포넌트를 사용해 버튼 컨텐츠가 길어지는 경우 레이아웃을 세로로 접을 수
있습니다.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<ResponsivePair gap="x2">
<AlertDialogAction variant="neutralWeak">취소</AlertDialogAction>
<AlertDialogAction variant="neutralSolid">
<PrefixIcon svg={<IconCheckFill />} />긴 레이블 예시
</AlertDialogAction>
</ResponsivePair>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
);
};
export default AlertDialogWrap;Single Action
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
const AlertDialogSingle = () => {
// You can set z-index dialog with "--layer-index" custom property. useful for stackflow integration.
return (
<AlertDialogRoot>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">열기</ActionButton>
</AlertDialogTrigger>
<AlertDialogContent layerIndex={50}>
<AlertDialogHeader>
<AlertDialogTitle>제목</AlertDialogTitle>
<AlertDialogDescription>단일 선택지를 제공합니다.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogAction variant="neutralSolid">확인</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
);
};
export default AlertDialogSingle;Neutral Secondary Action
import { ResponsivePair } from "@seed-design/react";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
const AlertDialogNeutral = () => {
return (
// You can set z-index dialog with "--layer-index" custom property. useful for stackflow integration.
<AlertDialogRoot>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">열기</ActionButton>
</AlertDialogTrigger>
<AlertDialogContent layerIndex={50}>
<AlertDialogHeader>
<AlertDialogTitle>제목</AlertDialogTitle>
<AlertDialogDescription>중립적인 선택지를 제공합니다.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
{/* ResponsivePair component wraps layout if button content is too long. */}
<ResponsivePair gap="x2">
<AlertDialogAction variant="neutralWeak">취소</AlertDialogAction>
<AlertDialogAction variant="neutralSolid">확인</AlertDialogAction>
</ResponsivePair>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
);
};
export default AlertDialogNeutral;Nonpreferred
import { VStack } from "@seed-design/react";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
const AlertDialogNonpreferred = () => {
return (
// You can set z-index dialog with "--layer-index" custom property. useful for stackflow integration.
<AlertDialogRoot>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">열기</ActionButton>
</AlertDialogTrigger>
<AlertDialogContent layerIndex={50}>
<AlertDialogHeader>
<AlertDialogTitle>제목</AlertDialogTitle>
<AlertDialogDescription>중립적인 선택지를 제공합니다.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<VStack gap="x4" alignSelf="stretch">
<AlertDialogAction size="medium" variant="neutralSolid" layout="withText">
라벨
</AlertDialogAction>
<AlertDialogAction
size="medium"
variant="ghost"
layout="withText"
color="fg.neutralMuted"
fontWeight="bold"
bleedY="asPadding"
>
라벨
</AlertDialogAction>
</VStack>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
);
};
export default AlertDialogNonpreferred;Critical Action
import { ResponsivePair } from "@seed-design/react";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
const AlertDialogCritical = () => {
return (
// You can set z-index dialog with "--layer-index" custom property. useful for stackflow integration.
<AlertDialogRoot>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">열기</ActionButton>
</AlertDialogTrigger>
<AlertDialogContent layerIndex={50}>
<AlertDialogHeader>
<AlertDialogTitle>제목</AlertDialogTitle>
<AlertDialogDescription>파괴적, 비가역적 작업을 경고합니다.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
{/* ResponsivePair component wraps layout if button content is too long. */}
<ResponsivePair gap="x2">
<AlertDialogAction variant="neutralWeak">취소</AlertDialogAction>
<AlertDialogAction variant="criticalSolid">확인</AlertDialogAction>
</ResponsivePair>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
);
};
export default AlertDialogCritical;Controlled
You can open and close AlertDialog in ways other than Trigger. In this case, use the open prop to control AlertDialog's state.
import { ResponsivePair } from "@seed-design/react";
import { useState } from "react";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
} from "seed-design/ui/alert-dialog";
const AlertDialogControlled = () => {
const [open, setOpen] = useState(false);
return (
<>
<ActionButton variant="neutralSolid" onClick={() => setOpen(true)}>
열기
</ActionButton>
<AlertDialogRoot open={open} onOpenChange={setOpen}>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>주의</AlertDialogTitle>
<AlertDialogDescription>이 작업은 되돌릴 수 없습니다.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<ResponsivePair gap="x2">
<AlertDialogAction variant="neutralWeak" onClick={() => setOpen(false)}>
취소
</AlertDialogAction>
<AlertDialogAction variant="neutralSolid" onClick={() => setOpen(false)}>
확인
</AlertDialogAction>
</ResponsivePair>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
</>
);
};
export default AlertDialogControlled;Prevent Close
Calling e.preventDefault() in AlertDialogAction's onClick prevents the dialog from closing.
import { Box, VStack } from "@seed-design/react";
import { useState } from "react";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
import { Switch } from "seed-design/ui/switch";
export default function AlertDialogPreventClose() {
const [preventClose, setPreventClose] = useState(true);
return (
<AlertDialogRoot>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">열기</ActionButton>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>닫기 방지</AlertDialogTitle>
<AlertDialogDescription>
확인 버튼을 눌러도 다이얼로그가 닫히지 않도록 설정할 수 있습니다.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter asChild>
<VStack gap="x4">
<Box alignSelf="flex-start">
<Switch
size="16"
tone="neutral"
label="preventDefault 사용"
checked={preventClose}
onCheckedChange={setPreventClose}
/>
</Box>
<AlertDialogAction
variant="neutralSolid"
onClick={(e) => {
if (preventClose) {
e.preventDefault();
}
}}
>
확인
</AlertDialogAction>
</VStack>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
);
}onOpenChange Details
The details is provided as the second argument to onOpenChange.
reason
When opening (open: true)
"trigger": Opened byAlertDialogTrigger(Dialog.Trigger)
When closing (open: false)
"closeButton": Closed byAlertDialogAction"escapeKeyDown": ESC key pressed"interactOutside": Clicked outside areaAlertDialogRootdefaults tocloseOnInteractOutside={false}.interactOutsidecan only occur when this option is set totrue.
"cascadeDismiss": Cascade closing due to parent layer closing
import { HStack, ResponsivePair, Text, VStack } from "@seed-design/react";
import { useState } from "react";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
export default function AlertDialogOnOpenChangeReason() {
const [open, setOpen] = useState(false);
const [openReason, setOpenReason] = useState<string | null>(null);
const [closeReason, setCloseReason] = useState<string | null>(null);
return (
<VStack gap="x4" align="center">
<AlertDialogRoot
open={open}
onOpenChange={(open, details) => {
setOpen(open);
(open ? setOpenReason : setCloseReason)(details?.reason ?? null);
}}
>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">열기</ActionButton>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>알림</AlertDialogTitle>
<AlertDialogDescription>
ESC 키를 누르거나 버튼을 클릭하여 닫아보세요.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<ResponsivePair gap="x2">
<AlertDialogAction variant="neutralWeak">취소</AlertDialogAction>
<AlertDialogAction variant="neutralSolid">확인</AlertDialogAction>
</ResponsivePair>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
<HStack gap="x4">
<Text fontSize="t3" color="fg.neutralMuted">
마지막 열림 이유: {openReason ?? "-"}
</Text>
<Text fontSize="t3" color="fg.neutralMuted">
마지막 닫힘 이유: {closeReason ?? "-"}
</Text>
</HStack>
</VStack>
);
}Portalled
Portal renders by default on document.body.
import { ResponsivePair, Portal } from "@seed-design/react";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
const AlertDialogPortalled = () => {
return (
// You can set z-index dialog with "--layer-index" custom property. useful for stackflow integration.
<AlertDialogRoot>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">열기</ActionButton>
</AlertDialogTrigger>
<Portal>
<AlertDialogContent layerIndex={50}>
<AlertDialogHeader>
<AlertDialogTitle>주의</AlertDialogTitle>
<AlertDialogDescription>이 작업은 되돌릴 수 없습니다.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<ResponsivePair gap="x2">
<AlertDialogAction variant="neutralWeak">취소</AlertDialogAction>
<AlertDialogAction variant="neutralSolid">확인</AlertDialogAction>
</ResponsivePair>
</AlertDialogFooter>
</AlertDialogContent>
</Portal>
</AlertDialogRoot>
);
};
export default AlertDialogPortalled;Skip Animation
You can skip AlertDialog's enter/exit animation using the skipAnimation prop.
import { ResponsivePair } from "@seed-design/react";
import { ActionButton } from "seed-design/ui/action-button";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
AlertDialogTrigger,
} from "seed-design/ui/alert-dialog";
const AlertDialogSkipAnimation = () => {
return (
<AlertDialogRoot skipAnimation>
<AlertDialogTrigger asChild>
<ActionButton variant="neutralSolid">열기</ActionButton>
</AlertDialogTrigger>
<AlertDialogContent layerIndex={50}>
<AlertDialogHeader>
<AlertDialogTitle>주의</AlertDialogTitle>
<AlertDialogDescription>이 작업은 되돌릴 수 없습니다.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<ResponsivePair gap="x2">
<AlertDialogAction variant="neutralWeak">취소</AlertDialogAction>
<AlertDialogAction variant="neutralSolid">확인</AlertDialogAction>
</ResponsivePair>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
);
};
export default AlertDialogSkipAnimation;Stackflow
import { useActivityZIndexBase } from "@seed-design/stackflow";
import { type StaticActivityComponentType, useFlow } from "@stackflow/react/future";
import {
AlertDialogAction,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogRoot,
AlertDialogTitle,
} from "seed-design/ui/alert-dialog";
declare module "@stackflow/config" {
interface Register {
ActivityAlertDialogStackflow: {};
}
}
const ActivityAlertDialogStackflow: StaticActivityComponentType<
"ActivityAlertDialogStackflow"
> = () => {
const { pop } = useFlow();
return (
<AlertDialogRoot defaultOpen onOpenChange={(open) => !open && pop()}>
<AlertDialogContent layerIndex={useActivityZIndexBase()}>
<AlertDialogHeader>
<AlertDialogTitle>제목</AlertDialogTitle>
<AlertDialogDescription>Stackflow</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogAction variant="neutralSolid">확인</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialogRoot>
);
};
export default ActivityAlertDialogStackflow;