Demos

A confirmation before an appointment is cancelled.

Usage

Use an alert dialog before an action that is hard to undo, such as deleting a report or cancelling an appointment. Unlike a Dialog, it does not close when the reader clicks the backdrop. They must choose an action.

Import it from @workspace/ui/components/alert-dialog.

Anatomy

  • AlertDialog — Holds the open state.
  • AlertDialogTrigger — Opens the dialog. Pass a button through render.
  • AlertDialogContent — The window and its backdrop.
  • AlertDialogHeader — Stacks the media, title, and description.
  • AlertDialogMedia — An optional icon tile beside the title.
  • AlertDialogTitle — The question. Always include it.
  • AlertDialogDescription — What will happen if the reader goes ahead.
  • AlertDialogFooter — The actions. Stacks on small screens.
  • AlertDialogCancel — Closes the dialog. An outline button by default.
  • AlertDialogAction — The action. A plain Button, so close the dialog yourself after it runs, or wrap the work in a controlled open state.

Notes

  • Write the title as a question and name the thing: "Delete this report?", not "Are you sure?".
  • Name the action button with a verb, such as "Delete". Avoid "OK" and "Yes".
  • Focus starts on the first button, which is the cancel button, so Enter does not run the action by accident.

Examples

Destructive

Add AlertDialogMedia for an icon and pass variant="destructive" to the action.

Small

size="sm" narrows the dialog and places the two buttons side by side.

Props

These are the props you set most often. Unlisted props pass through to the underlying element.

AlertDialog

PropTypeDefaultDescription
defaultOpenbooleanfalseOpen on first render.
openboolean—Open state when controlled.
onOpenChange(open) => void—Called when the dialog opens or closes.

AlertDialogContent

PropTypeDefaultDescription
size"default" or "sm"defaultWidth of the dialog. sm also puts the footer in two columns.

AlertDialogAction and AlertDialogCancel

Both take the Button variant and size props. AlertDialogCancel defaults to variant="outline".