Skip to Content
zjkm-ui
Components

Alert Dialog

A dialog that asks to confirm or cancel.

Discard this draft?

Your changes will be lost. You can't undo this.

Props
<AlertDialog open={open} title="Discard this draft?" onConfirm={discard} onCancel={close} />

Installation

npx shadcn@latest add https://ui.zjkm.tech/r/alert-dialog.json
import { 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

PropTypeDefaultDescription
openbooleanShows the dialog.
titlestringThe question.
descriptionstringWhat happens if you confirm.
confirmLabelstring"Confirm"Confirm button text.
cancelLabelstring"Cancel"Cancel button text.
tone"default" | "danger""default"Danger turns the confirm button red.
onConfirm() => voidCalled on confirm.
onCancel() => voidCalled on Cancel or Escape.

With reduced motion, it appears without scaling in.

On this page