Alert dialog
A confirmation dialog on Base UI AlertDialog.
For a decision the person has to make before anything else, such as deleting something. A click outside does not close it.
Default
"use client"
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 AlertDialogDefault() {
const [open, setOpen] = useState(false)
return (
<AlertDialog open={open} onOpenChange={setOpen}>
<AlertDialogTrigger render={<Button variant="outline" />}>Delete project</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete Website refresh?</AlertDialogTitle>
<AlertDialogDescription>
Its 48 tasks and 12 files are deleted with it. This cannot be undone.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction variant="destructive" onClick={() => setOpen(false)}>
Delete project
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}
Small
"use client"
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 AlertDialogSmall() {
const [open, setOpen] = useState(false)
return (
<AlertDialog open={open} onOpenChange={setOpen}>
<AlertDialogTrigger render={<Button variant="outline" />}>Discard draft</AlertDialogTrigger>
<AlertDialogContent size="sm">
<AlertDialogHeader>
<AlertDialogTitle>Discard this draft?</AlertDialogTitle>
<AlertDialogDescription>The invoice has not been sent. Its line items are lost.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Keep editing</AlertDialogCancel>
<AlertDialogAction onClick={() => setOpen(false)}>Discard</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}
With media
"use client"
import { useState } from "react"
import { TriangleAlertIcon } from "@keyline-icons/react"
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogMedia,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"
export default function AlertDialogWithMedia() {
const [open, setOpen] = useState(false)
return (
<AlertDialog open={open} onOpenChange={setOpen}>
<AlertDialogTrigger render={<Button variant="outline" />}>Publish release</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogMedia>
<TriangleAlertIcon />
</AlertDialogMedia>
<AlertDialogTitle>Publish release 4.2 now?</AlertDialogTitle>
<AlertDialogDescription>
Two checks are still running. Publishing now sends the release notes to every subscriber.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Wait for the checks</AlertDialogCancel>
<AlertDialogAction onClick={() => setOpen(false)}>Publish</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)
}
Installation
npx shadcn@latest add @khaeli/alert-dialogbunx --bun shadcn@latest add @khaeli/alert-dialogInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
Keyboard
EnterSpace- On the trigger, opens the dialog. Focus moves to the first button in the footer, so put the safe choice first:
AlertDialogCancel. TabShift Tab- Move between the buttons and wrap at either end. Focus cannot leave the dialog while it is open.
Escape- Closes the dialog, as Cancel does, and returns focus to the trigger.
Accessible name
- The dialog has the role
alertdialog. It is named byAlertDialogTitleand described byAlertDialogDescription: write the consequence in the description, because it is read out when the dialog opens. AlertDialogActionis a plain button and does not close the dialog. Close it yourself when the action has run, as the examples do withopenandonOpenChange.- Name the action by what it does (Delete project), not OK or Yes.
- The icon in
AlertDialogMediais decoration: the title carries the meaning.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/alert-dialog.json