A modal that interrupts the user to confirm an action that is hard to undo. It is a native dialog with role=alertdialog: the trigger is a button with Invoker Commands, Cancel and Action are command=close buttons, and Escape, the top layer, the backdrop, inert page content and focus return come from the browser. Cancel gets focus when the dialog opens, and a click on the backdrop does not close it. A tiny client script moves focus to Cancel, reports the state and wires the buttons in browsers without Invoker Commands.
Browser Support
Baseline Newly available4 featuresDetails
Opens the modal in the top layer with a backdrop and Escape to close.
Opens and closes the dialog from buttons without JavaScript. Without it a small script does the same.
Fades the dialog in when it opens. Without it the dialog appears at once.
Fades the dialog out when it closes. Without it the dialog disappears at once.
Preview
Installation
Usage
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"
export default function Example() {
return (
<AlertDialog>
<AlertDialogTrigger asChild>
<Button variant="outline">Delete account</Button>
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
This action cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction>Continue</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}Examples
Destructive action
With asChild a Button child keeps its own variant, so the confirmation can be red. The Action and Cancel buttons both close the dialog.
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction asChild>
<Button variant="destructive">Remove</Button>
</AlertDialogAction>
</AlertDialogFooter>Controlled and with a handler
open and onOpenChange from a client component; the onClick of the Action runs the confirmed action. The browser still closes the dialog on its own.
State: closed
"use client"
const [open, setOpen] = React.useState(false)
<AlertDialog open={open} onOpenChange={setOpen}>
<AlertDialogTrigger asChild>
<Button variant="destructive">Delete account</Button>
</AlertDialogTrigger>
<AlertDialogContent>
…
<AlertDialogAction asChild onClick={deleteAccount}>
<Button variant="destructive">Delete</Button>
</AlertDialogAction>
</AlertDialogContent>
</AlertDialog>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| open | boolean | - | Controlled open state. Client components only |
| defaultOpen | boolean | false | Open right after hydration |
| onOpenChange | (open: boolean) => void | - | Called with the new state on every open and close. Client components only: functions cannot cross the server boundary |
| id | string | - | id of the dialog element. Generated when omitted |
| asChild | boolean | false | Merge the props into the only child, for example a Button (AlertDialogTrigger, AlertDialogAction, AlertDialogCancel) |
| className | string | - | Additional CSS classes |
Features
- role="alertdialog" on a native dialog, labelled by AlertDialogTitle and described by AlertDialogDescription.
- Escape closes the dialog; a click on the backdrop does not, because an alert dialog needs an explicit answer.
- Focus moves to Cancel on open, so Enter never confirms by accident, and returns to the trigger on close.
- The page behind the dialog is inert and does not scroll.
- Parts are linked without React context, so AlertDialog and its parts stay server components. A part rendered inside your own component is not reached: keep the parts in the JSX passed to AlertDialog.