videorc/ui

Alert Dialog

The question before something destructive, on the dialog tier. No close button, Cancel has focus on open, and Escape is the safe answer.

Installation

$ pnpm dlx shadcn@latest add @videorc/alert-dialog

Usage

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/components/ui/alert-dialog";
<AlertDialog onOpenChange={setOpen} open={open}>
  <AlertDialogContent>
    <AlertDialogHeader>
      <AlertDialogTitle>Delete saved scene?</AlertDialogTitle>
      <AlertDialogDescription>
        Only the saved snapshot is deleted. Your current scene and recording
        continue.
      </AlertDialogDescription>
    </AlertDialogHeader>
    <AlertDialogFooter>
      <AlertDialogCancel>Cancel</AlertDialogCancel>
      <AlertDialogAction onClick={deleteScene} variant="destructive">
        Delete scene
      </AlertDialogAction>
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>

The title is "Verb object?", and one sentence names exactly what is lost. The safe answer ("Cancel", or "Keep …") is a ghost button; the destructive one reads verb and object. A consequential step that destroys nothing, such as a replace, keeps variant="default".

Anatomy

<AlertDialog>
  <AlertDialogTrigger />
  <AlertDialogContent>
    <AlertDialogHeader>
      <AlertDialogTitle />
      <AlertDialogDescription />
    </AlertDialogHeader>
    <AlertDialogFooter>
      <AlertDialogCancel />
      <AlertDialogAction />
    </AlertDialogFooter>
  </AlertDialogContent>
</AlertDialog>

Examples

Pending action

AlertDialogAction doesn't close the dialog: the owner drops open once the action is done. While async work runs, the dialog stays up and says so ("Deleting…"), and the answers are disabled.

Theming

Token or attributeMeaning
dialogContentVariants({ size })The same surface, widths and height rule as Dialog
data-sizesm, md (the default), lg or xl
bg-black/30The overlay, with no backdrop blur

Keyboard

KeyAction
EscapeCloses the dialog: the same answer as Cancel
Tab, Shift+TabMoves between the answers; focus stays inside
Enter, SpacePresses the focused answer

Accessibility

The dialog is an alertdialog, named by its title and described by its description. Cancel has focus on open, even when the body holds a control before it, so a stray Enter or Space never destroys anything. A click outside doesn't close it.

API reference

Every Base UI Alert Dialog prop, plus:

AlertDialogContent

Prop

Type

AlertDialogAction

A Button that doesn't close the dialog.

Prop

Type

AlertDialogCancel

Prop

Type