Components
Alert Dialog
A dialog that asks to confirm or cancel.
<AlertDialog open={open} title="Discard this draft?" onConfirm={discard} onCancel={close} />Installation
npx shadcn@latest add https://ui.zjkm.tech/r/alert-dialog.jsonimport { AlertDialog } from "@/components/alert-dialog"Using JavaScript? Set tsx: false in components.json and the CLI converts it.
New to shadcn? Run npx shadcn@latest init first. The CLI needs components.json and the @/ import alias.
Usage
Asks someone to confirm or cancel. It's a native modal <dialog>: focus moves in and stays there, Escape closes it, and the page behind can't be reached. It's an alertdialog, and Cancel takes focus first, so a stray Enter never confirms.
import { AlertDialog } from "@/components/alert-dialog";
export function DiscardButton({ discard }: { discard: () => void }) {
const [open, setOpen] = useState(false);
return (
<>
<button onClick={() => setOpen(true)}>Discard draft</button>
<AlertDialog
open={open}
tone="danger"
title="Discard this draft?"
description="Your changes will be lost."
confirmLabel="Discard"
onConfirm={() => { discard(); setOpen(false); }}
onCancel={() => setOpen(false)}
/>
</>
);
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | Shows the dialog. | |
title | string | The question. | |
description | string | What happens if you confirm. | |
confirmLabel | string | "Confirm" | Confirm button text. |
cancelLabel | string | "Cancel" | Cancel button text. |
tone | "default" | "danger" | "default" | Danger turns the confirm button red. |
onConfirm | () => void | Called on confirm. | |
onCancel | () => void | Called on Cancel or Escape. |
With reduced motion, it appears without scaling in.