Alert Dialog
A modal confirmation that interrupts the user and needs an explicit answer: delete, sign out, discard. Focus starts on the safe choice, and clicking outside does nothing.
Installation
Add Alert Dialog to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.
- Adds
AlertDialog.tsxButton.tsxThemeScope.tsxto your CK-UI folder (src/components/ck-ui by default)- Installs
@radix-ui/react-dialog@radix-ui/react-slotclass-variance-authorityif your project does not have them yet
First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.
Manual installationInstall the packages and copy the files yourself.
Install the following dependencies
Run these commands in your terminal to install the required packages
Add the Button component
AlertDialog imports ./Button, so add this file next to it
Add the ThemeScope component
AlertDialog imports ./ThemeScope, so add this file next to it
Create or update your AlertDialog component
Copy this code to create your AlertDialog component
Test Your Component
- 1. Add the component to a test page
- 2. Check all props and variants work as expected
- 3. Verify responsive behavior
- 4. Test with different content/data
- 5. Ensure accessibility features work correctly
Sign Out
A non-destructive confirmation uses the default (primary) action color.
Irreversible Action
For actions that cannot be undone, ask the user to type the name of what will be deleted before the action is enabled. Focus still starts on Cancel.
Controlled
Control open to keep the dialog up while a request runs: call event.preventDefault() in the action's onClick, then close it yourself when the request finishes.
Modal vs Alert Dialog
- Use Alert Dialog when the user must answer before continuing (confirmations, destructive actions). It has role alertdialog, cannot be dismissed by clicking outside and focuses Cancel first.
- Use Modal for tasks and content (forms, details, settings). It can be dismissed by clicking outside and has more layout options.
Accessibility
- The panel has
role="alertdialog", is named byAlertDialog.Titleand described byAlertDialog.Description. Both are required. - Focus moves to
AlertDialog.Cancelwhen it opens, so pressing Enter by reflex never confirms a destructive action. Without a Cancel, the first focusable element is focused. - Tab and Shift+Tab stay inside the dialog. Escape closes it like Cancel. Focus returns to the trigger when it closes.
- It is always modal: page scrolling is locked and the rest of the page is hidden from assistive technology while it is open.
- Clicks on the backdrop are ignored; the user has to choose an answer.
- Destructive actions use
color="destructive"and a label that names the action ("Delete project"), not "OK".
API Reference
Built on the Radix UI Dialog primitive with the same behaviour as Radix Alert Dialog. Import with import * as AlertDialog from "@/components/ui/AlertDialog".
AlertDialog.Root
Holds the open state. Always modal.
See Radix UI docs for base props.
| Prop | Type | Default | Description |
|---|---|---|---|
open | "boolean" | "undefined" | Controlled open state. |
defaultOpen | "boolean" | "false" | Initial open state when uncontrolled. |
onOpenChange | "(open: boolean) => void" | "undefined" | Called when the dialog opens or closes. |
AlertDialog.Trigger
Opens the dialog.
See Radix UI docs for base props.
| Prop | Type | Default | Description |
|---|---|---|---|
asChild | "boolean" | "false" | Render the single child element instead, merging this part's props and behaviour onto it. |
className | "string" | "undefined" | Additional classes, merged last. |
AlertDialog.Content
The panel (role alertdialog), rendered in a portal over a backdrop that also scrolls tall content.
| Prop | Type | Default | Description |
|---|---|---|---|
size | "sm""md""lg" | "md" | Maximum width. |
onOpenAutoFocus | "(event: Event) => void" | "undefined" | Call event.preventDefault() to focus something other than Cancel yourself. |
onEscapeKeyDown | "(event: KeyboardEvent) => void" | "undefined" | Call event.preventDefault() to keep the dialog open on Escape. |
className | "string" | "undefined" | Classes for the panel. |
AlertDialog.Header
Stacks the title and description.
| Prop | Type | Default | Description |
|---|---|---|---|
className | "string" | "undefined" | Additional classes, merged last. |
AlertDialog.Title
Names the dialog. Required.
| Prop | Type | Default | Description |
|---|---|---|---|
className | "string" | "undefined" | Additional classes, merged last. |
AlertDialog.Description
Describes the consequence. Required.
| Prop | Type | Default | Description |
|---|---|---|---|
className | "string" | "undefined" | Additional classes, merged last. |
AlertDialog.Footer
Holds Cancel and Action: stacked full-width on small screens (action on top), right-aligned from the sm breakpoint.
| Prop | Type | Default | Description |
|---|---|---|---|
className | "string" | "undefined" | Additional classes, merged last. |
AlertDialog.Cancel
Closes without acting and receives focus on open. Renders a neutral stroke Button.
| Prop | Type | Default | Description |
|---|---|---|---|
asChild | "boolean" | "false" | Use your own element; it still closes the dialog and receives initial focus. |
size | "xs""sm""md""lg" | "md" | Button size. |
disabled | "boolean" | "false" | Disables the button. |
className | "string" | "undefined" | Additional classes, merged last. |
AlertDialog.Action
Confirms and closes. Renders a filled Button.
| Prop | Type | Default | Description |
|---|---|---|---|
color | "primary""destructive" | "primary" | destructive for irreversible actions. |
loading | "boolean" | "false" | Shows a spinner, disables the button and sets aria-busy. |
onClick | "(event) => void" | "undefined" | Call event.preventDefault() to keep the dialog open. |
asChild | "boolean" | "false" | Render the single child element instead, merging this part's props and behaviour onto it. |
className | "string" | "undefined" | Additional classes, merged last. |
Used in these blocks
Block families with at least one block built on this component. Each page previews its blocks and lists the components and packages they use.