Alert Dialog
A modal dialog for confirmations that requires explicit user action
Installation
pnpm dlx shadcn@latest add https://prototyper-ui.com/r/alert-dialog.jsonnpx shadcn@latest add https://prototyper-ui.com/r/alert-dialog.jsonyarn dlx shadcn@latest add https://prototyper-ui.com/r/alert-dialog.jsonbunx --bun shadcn@latest add https://prototyper-ui.com/r/alert-dialog.jsonThis will add the following files to your project:
components/ui/alert-dialog.tsx
Usage
import {
AlertDialog,
AlertDialogTrigger,
AlertDialogContent,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogAction,
AlertDialogCancel,
} from "@/components/ui/alert-dialog"
<AlertDialog>
<AlertDialogTrigger>Delete...</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you sure?</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction>Confirm</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>Anatomy
<AlertDialog>
<AlertDialogTrigger />
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogMedia />
<AlertDialogTitle />
<AlertDialogDescription />
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel />
<AlertDialogAction />
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>| Sub-component | data-slot | Purpose | Required |
|---|---|---|---|
AlertDialog | alert-dialog | Root provider, manages open/close state | Yes |
AlertDialogTrigger | alert-dialog-trigger | Button that opens the alert dialog | Yes |
AlertDialogContent | alert-dialog-content | The alert dialog popup panel | Yes |
AlertDialogHeader | alert-dialog-header | Container for media, title, and description | No |
↳ AlertDialogMedia | alert-dialog-media | Icon or illustration container (child of Header) | No |
↳ AlertDialogTitle | alert-dialog-title | Accessible title for the alert dialog (child of Header) | Yes |
↳ AlertDialogDescription | alert-dialog-description | Accessible description (child of Header) | No |
AlertDialogFooter | alert-dialog-footer | Container for action buttons | No |
↳ AlertDialogCancel | alert-dialog-cancel | Cancel button (closes the dialog) | No |
↳ AlertDialogAction | alert-dialog-action | Confirm button (does not auto-close) | No |
AlertDialogPortal | alert-dialog-portal | Renders children into a portal | No |
AlertDialogOverlay | alert-dialog-overlay | Backdrop behind the alert dialog | No |
Examples
Destructive
Use the variant="destructive" prop on AlertDialogAction to indicate a dangerous action.
Styling
Data Slots
Use data-slot attributes to target specific parts of the alert dialog:
| Slot name | Element |
|---|---|
alert-dialog | Root provider (no DOM element rendered) |
alert-dialog-trigger | The trigger button |
alert-dialog-portal | Portal wrapper |
alert-dialog-overlay | Backdrop overlay |
alert-dialog-content | The popup panel |
alert-dialog-header | Header container (title + description) |
alert-dialog-media | Icon or illustration container |
alert-dialog-title | Title heading |
alert-dialog-description | Description paragraph |
alert-dialog-footer | Footer container (action buttons) |
alert-dialog-action | Confirm action button |
alert-dialog-cancel | Cancel button |
Customization Examples
/* Custom overlay color */
[data-slot="alert-dialog-overlay"] {
@apply bg-black/30;
}
/* Wider alert dialog */
[data-slot="alert-dialog-content"] {
@apply sm:max-w-lg;
}{/* Use the small size variant */}
<AlertDialogContent size="sm">
{/* ... */}
</AlertDialogContent>API Reference
AlertDialog
Root component that manages open/close state.
Prop
Type
All Base UI AlertDialog.Root props are forwarded via ...props.
AlertDialogTrigger
Button that opens the alert dialog when clicked.
Prop
Type
All Base UI AlertDialog.Trigger props are forwarded via ...props.
AlertDialogContent
The popup panel rendered inside a portal with an overlay backdrop.
Prop
Type
All Base UI AlertDialog.Popup props are forwarded via ...props.
AlertDialogHeader
Container for media, title, and description. Layout adjusts based on the size prop on AlertDialogContent.
Prop
Type
All standard div props are forwarded via ...props.
AlertDialogMedia
Container for an icon or illustration displayed in the header.
Prop
Type
All standard div props are forwarded via ...props.
AlertDialogFooter
Container for action buttons. Uses a two-column grid at the sm size, and a row layout at default.
Prop
Type
All standard div props are forwarded via ...props.
AlertDialogTitle
Accessible title for the alert dialog, rendered as a heading.
Prop
Type
All Base UI AlertDialog.Title props are forwarded via ...props.
AlertDialogDescription
Accessible description rendered below the title.
Prop
Type
All Base UI AlertDialog.Description props are forwarded via ...props.
AlertDialogAction
Confirm action button. Accepts all Button props including variant and size.
Prop
Type
All Button props are forwarded via ...props.
AlertDialogCancel
Cancel button that closes the alert dialog when clicked. Renders as a Button with variant="outline" by default.
Prop
Type
All Base UI AlertDialog.Close props are forwarded via ...props.
AlertDialogOverlay
Backdrop overlay behind the alert dialog.
Prop
Type
All Base UI AlertDialog.Backdrop props are forwarded via ...props.
AlertDialogPortal
Renders children into a React portal.
All Base UI AlertDialog.Portal props are forwarded via ...props.
Accessibility
Unlike a regular Dialog, an Alert Dialog is intended for confirmations and destructive actions. It does not close when clicking outside or pressing Escape by default, ensuring the user must make an explicit choice.
Keyboard Interactions
| Key | Action |
|---|---|
Tab | Moves focus to the next focusable element within the alert dialog |
Shift+Tab | Moves focus to the previous focusable element within the alert dialog |
ARIA Attributes
AlertDialogContentreceivesrole="alertdialog".aria-modal="true"is set automatically.aria-labelledbyis automatically linked toAlertDialogTitle.aria-describedbyis automatically linked toAlertDialogDescription.- Focus is trapped inside the alert dialog while it is open.
- Focus returns to the trigger element when the alert dialog closes.