Alert Dialog
Ask for a decision before something that can’t be undone.
When to use it
Use Alert Dialog only before destructive or irreversible actions, such as deleting a project. Name the action in the title and on the confirm button.
For anything people can undo, skip the question and offer Undo afterward in a Toast.
Alert DialogThis page
The page needs an answer.
Actions that can’t be undone.
How it behaves
Base UI traps focus, ignores outside presses, closes with Escape, and returns focus to the trigger. Focus starts on the first button, so place AlertDialogCancel before AlertDialogAction.
Name the consequence in AlertDialogTitle and AlertDialogDescription, and label the action with the verb, such as Delete project.
AlertDialogAction is a Button; control open/onOpenChange to close after the action completes. AlertDialogCancel closes the dialog and defaults to the outline variant.
size="sm" places the buttons side by side. AlertDialogMedia adds an optional icon tile beside the title.
Build it
Add it with the shadcn CLI:
1pnpm dlx shadcn@latest add https://jzbui.com/r/alert-dialog.jsonA complete example. Change the values to fit your product:
123456789101112131415161718192021222324252627282930313233343536import { useState } from "react"import { Button } from "@/components/ui/button"import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger} from "@/components/ui/alert-dialog"
export function DeleteProject({ onDelete }: { onDelete: () => void }) { const [open, setOpen] = useState(false) return ( <AlertDialog open={open} onOpenChange={setOpen}> <AlertDialogTrigger render={<Button variant="outline" />}>Delete project</AlertDialogTrigger> <AlertDialogContent> <AlertDialogHeader> <AlertDialogTitle>Delete Website refresh?</AlertDialogTitle> <AlertDialogDescription> This permanently deletes the project and its 24 issues. This can’t be undone. </AlertDialogDescription> </AlertDialogHeader> <AlertDialogFooter> <AlertDialogCancel>Cancel</AlertDialogCancel> <AlertDialogAction variant="destructive" onClick={() => { onDelete(); setOpen(false) }}> Delete project </AlertDialogAction> </AlertDialogFooter> </AlertDialogContent> </AlertDialog> )}The full source is yours to edit once installed: