Alert Dialog

A modal decision; an agent's confirm waits for a person.

Source
$npx @dinachi/cli@latest add alert-dialog
AlertDialogTitle
AlertDialogDescription
AlertDialogAction
variant

Usage

import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
  AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"

export function DeleteProject() {
  return (
    <AlertDialog>
      <AlertDialogTrigger render={<Button variant="destructive" />}>
        Delete project
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>Delete this project?</AlertDialogTitle>
          <AlertDialogDescription>This cannot be undone.</AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction>Delete</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  )
}

Examples

A confirmation before a destructive action.
import {
  AlertDialog,
  AlertDialogTrigger,
  AlertDialogContent,
  AlertDialogTitle,
  AlertDialogDescription,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogHeader,
  AlertDialogFooter,
} from '@/components/ui/alert-dialog';
import { Button } from '@/components/ui/button';

export function Example() {
  return (
    <AlertDialog>
      <AlertDialogTrigger render={<Button variant="destructive" />}>
        Delete Account
      </AlertDialogTrigger>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
          <AlertDialogDescription>
            This action cannot be undone. This will permanently delete your account.
          </AlertDialogDescription>
        </AlertDialogHeader>
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction>Continue</AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}

Notes

Your own layout

AlertDialogContent combines AlertDialogPortal, AlertDialogBackdrop, AlertDialogViewport and AlertDialogPopup. Compose them directly to change one.

Dismissal

Backdrop clicks do not close it. Only its buttons or Esc do.

API

openboolean
Controlled open state.
defaultOpenboolean = false
Starting open state when uncontrolled.
onOpenChange
agent
(open, details) => void
Runs for people and agents. details.cancel() refuses the change.
onOpenChangeComplete(open) => void
Runs once the open or close animation ends.
actionsRefRefObject<{ close, unmount }>
Closes the dialog imperatively.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as dialog.

Reads

label

The trigger's text while closed, the title while open.

open

Whether the dialog is showing.

Actions

open()

Opens the dialog. Only while closed.

close()

Closes it. Only while open; needs no approval.

Open and close run through onOpenChange. isAgentChange(details) is true for an agent's change; details.cancel() refuses it and the agent is told. While open, every control inside except AlertDialogCancel needs a person's approval. Without an allow function on the agent, those presses are refused.

await agent.act({
  steps: [{ ref: "Delete Account", action: "open" }],
})

Accessibility

  • Role alertdialog, labelled by AlertDialogTitle, described by AlertDialogDescription.
  • Focus moves in on open and is trapped until close.
  • While open, the page behind is hidden from assistive tech and the agent.
  • Focus returns to the trigger on close.