Skip to content

Alert Dialog

Download .md

An alert dialog asks people to confirm an action that cannot be undone, such as deleting a release, before it runs.

  • Use it only for actions that cannot be undone: deleting, cancelling a subscription, withdrawing a contract.
  • Use a Dialog for a short form, and an undo Toast when the action can be reversed.
  • Write the title as a question (“Hapus rilis?”) and name what goes away in the description.
  • Label the action with its verb (“Hapus”), never “Ya” or “OK”.
React
Vue
DoName the action in the title and on the button, and focus Cancel first.
Don'tDon't ask a vague question with a lone OK button.
1234
  1. 1Warning icon
  2. 2Title that names the action
  3. 3Cancel, focused first
  4. 4Destructive action

A title, a description, and a footer band with Batal and the action. The window sits on popover with a border, radius-xl and shadow-lg, over the overlay token. The focus starts on Batal, and the destructive action sits last, on the right.

There is one style. Use the destructive button for the action; keep the default button only for actions that don’t delete, such as signing out of every device.

React Vue Flutter Type Default Use
open / onOpenChange v-model:open showShadDialog boolean false Opens it from code.
AlertDialogAction variant AlertDialogAction variant ShadButton.destructive text default The action button.
AlertDialogCancel autoFocus AlertDialogCancel (focused first) ShadButton.outline(autofocus: true) boolean none Starts the focus on Cancel.
AlertDialogTrigger render AlertDialogTrigger as-child the button that calls showShadDialog element none The button that opens it.
import { useState } from "react"
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"
export default function AlertDialogDemo() {
const [deleted, setDeleted] = useState(false)
if (deleted) return <p className="text-sm text-muted-foreground">"Senja di Kota" dihapus.</p>
return (
<AlertDialog>
<AlertDialogTrigger render={<Button variant="outline" />}>Hapus rilis</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Hapus rilis?</AlertDialogTitle>
<AlertDialogDescription>"Senja di Kota" dan semua datanya akan dihapus. Kamu tidak bisa mengembalikannya.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel autoFocus>Batal</AlertDialogCancel>
<AlertDialogAction variant="destructive" onClick={() => setDeleted(true)}>
Hapus
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}
Terminal window
npx shadcn@latest add https://design.sosialoka.tech/r/react/alert-dialog.json
  • It has role="alertdialog", named by its title and described by its description.
  • The focus starts on Cancel, so pressing Enter by habit does not delete anything.
  • Escape cancels; a click outside does not close it.
  • The focus stays inside until it closes, then goes back to the trigger.