shadcn.io is an independent project built on shadcn/ui
On this page9 sections
An alert dialog stops the page to ask one question before something that can't be undone, such as deleting an account or leaving with unsaved changes. A click outside doesn't dismiss it, so the answer is always Cancel or the action, chosen on purpose.
When people have fields to fill in or options to pick, use a Dialog. A message that needs no answer belongs in an Alert on the page.
Installation
Anatomy

Usage
<script lang="ts">
import {
AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent,
AlertDialogDescription, AlertDialogFooter, AlertDialogHeader,
AlertDialogTitle, AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import { Button } from "@/components/ui/button"
</script>
<AlertDialog><AlertDialogTrigger>{#snippet render({ props })}<Button {...props} variant="outline">Show Dialog</Button>{/snippet}</AlertDialogTrigger><AlertDialogContent><AlertDialogHeader><AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle><AlertDialogDescription>This action cannot be undone. This will permanently delete your account from our servers.</AlertDialogDescription></AlertDialogHeader><AlertDialogFooter><AlertDialogCancel>Cancel</AlertDialogCancel><AlertDialogAction>Continue</AlertDialogAction></AlertDialogFooter></AlertDialogContent></AlertDialog>Accessibility
The window has role="alertdialog", which tells a screen reader it is waiting for an answer, and AlertDialogTitle gives it its name. Focus stays inside until it closes, the page behind is hidden from screen readers, and Escape closes it as Cancel would, returning focus to the trigger. Focus starts on whichever of AlertDialogCancel and AlertDialogAction comes first, so keep Cancel ahead of the action and a stray Enter cancels instead of confirming. Opened by touch, focus goes to the dialog itself, so no on-screen keyboard appears. AlertDialogDescription is announced with the title.
- Enter
- When focus is on the trigger, opens the dialog. On a button inside, presses it.
- Space
- When focus is on the trigger, opens the dialog. On a button inside, presses it.
- Tab
- Moves focus to the next focusable element in the dialog, and from the last one back to the first.
- Shift+Tab
- Moves focus to the previous focusable element in the dialog, and from the first one to the last.
- EscEscape
- Closes the dialog and moves focus back to the trigger.
Demos
Basic
The account deletion question: a title asking if they are sure, one sentence on what is lost, and Cancel ahead of Continue in the footer. Both buttons close the dialog, and Continue has no handler of its own here. Start from this for any yes-or-no question that needs no icon.
Small
Setting size to sm on AlertDialogContent narrows the panel around a permission prompt: one question, one sentence, and Don't allow beside Allow in two equal columns. Use the small size when the whole message fits in a line or two. A consequence that needs a paragraph reads better at the default width.
Media
A prompt titled Share this project?, with an icon in AlertDialogMedia and a sentence saying anyone with the link can view and edit it. An icon helps when an app asks several similar questions and people should tell them apart at a glance. It doesn't replace the description, which still says what happens.
Small media
The small size and an icon together: a Bluetooth mark in AlertDialogMedia over the question about a Bluetooth accessory, with Don't allow and Allow side by side. This suits device and connection prompts, where people recognize the icon before they read. Move to the default width if the explanation grows past a sentence.
Destructive
Deleting a chat, with variant set to destructive on both the trigger and the action, and a trash icon in AlertDialogMedia tinted to match. The description also says where to delete the memories the chat saved. Keep the red button for steps that can't be undone, and use the same verb on the trigger, the title and the button.
Async action
Pressing Delete starts a request, faked here with a timer, and the dialog stays open with Delete disabled until the request finishes, then closes from the open state the demo holds. The action's click handler calls event.preventDefault(), which stops it from closing the dialog at once. Use this when the answer starts work that can fail.
RTL
Both confirmations in Arabic, one at each size, inside a Direction provider set to right to left. Each dialog renders in a portal at the end of the page, and the component carries the provider's direction and language there, so the title, the description and the buttons read from the right.
FAQ
When should I use an Alert Dialog instead of a Dialog?
Use an Alert Dialog when the only answer is yes or no to something with consequences: deleting a file, revoking access, discarding a draft. It ignores clicks outside, so nobody confirms or cancels by accident while reaching for the page.
Once people have to type, pick a date or choose between options, use a Dialog. It holds a form, and a click outside closes it.
How do I keep an Alert Dialog open until my delete request finishes?
AlertDialogAction closes the dialog after its click handler runs, unless you call event.preventDefault() there. Do that, hold the open state yourself, set isLoading on the action while the request runs, and close the dialog once it succeeds, as the Async action demo does. If the request fails, the dialog stays open for the error.
Why doesn't my Alert Dialog close when I click outside it?
That is the point of an alert dialog: a click outside does nothing, so the choice stays deliberate. Escape still closes it, the same as Cancel.
UI SDK's alert dialog never closes on a click outside, so use a Dialog when that matters. To ignore Escape, set dismissible to false on AlertDialog, or call details.cancel() in onOpenChange when details.reason is "escape-key".
How do I make the confirm button red in an Alert Dialog?
Set variant to destructive on AlertDialogAction, as the Destructive demo does for Delete. The trigger can take the same variant, so the red starts where the action starts.
Name the action with the verb from the title, Delete rather than Continue or OK, so people know what they are agreeing to without rereading the question. Leave Cancel in its default outline style.
Which button has focus when the UI SDK Alert Dialog opens?
Whichever of AlertDialogCancel and AlertDialogAction comes first does, so keep Cancel ahead of the action and Enter pressed by reflex cancels. UI SDK passes over links in the description and goes straight to that button, unless an element inside has autofocus. Opened by touch, focus goes to the dialog itself instead.
How do I make an Alert Dialog smaller or add an icon to it?
Set size to sm on AlertDialogContent for a narrower panel, where the two footer buttons sit side by side in equal columns. For an icon, put it in AlertDialogMedia inside AlertDialogHeader, before the title.
The Small, Media and Small media demos show each one and the two together. Mark the icon aria-hidden, since the title already says what it means.
How do I open an Alert Dialog from a menu item instead of a trigger button?
Leave out AlertDialogTrigger and hold the open state yourself, then set it from the menu item's handler. Render the alert dialog outside the menu, because the menu removes its items when it closes, and a dialog inside one would go with it.
Pass open to AlertDialog and update it in onOpenChange, which receives the new boolean.
API Reference
AlertDialog
open?boolean
Whether the dialog is open. Pass it to control the dialog. Two-way with bind:open.
defaultOpen?boolean
Whether the dialog starts open, when uncontrolled. Focus moves into it once it's on the page.
- Default
false
onOpenChange?(open: boolean, details: DialogOpenChangeDetails<AlertDialogOpenChangeReason>) → void
Runs when the alert dialog wants to open or close. details.reason says why, details.cancel() stops it, and details.returnValue is the value of the Close part that closed it.
- Type
(open: boolean, details: DialogOpenChangeDetails<AlertDialogOpenChangeReason>) → void- Parameters
open– booleandetails– DialogOpenChangeDetails<AlertDialogOpenChangeReason>- Returns
void
container?DocumentFragment | Element | null
Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.
- Type
DocumentFragment | Element | null
controller?PopupController<any, AlertDialogOpenChangeReason, {}, any, DialogPayloadChangeReason>
Opens, closes and inspects the alert dialog from anywhere. Create one with createAlertDialogController() or createConfirm().
- Type
PopupController<any, AlertDialogOpenChangeReason, {}, any, DialogPayloadChangeReason>
debug?boolean
Logs every state change with its reason to the console, in development builds.
dismissible?boolean
Whether Escape, an outside press and Android's back gesture can close the dialog. Close parts, triggers and the controller always can. Pass false while a form has unsaved changes, and ask in onDismissAttempt.
- Default
true
onDismissAttempt?(details: DialogDismissAttemptDetails) → void
Runs when a dismissal was refused, because dismissible is false, an outside press doesn't close it, or a Close part's action is running. Ask "Discard changes?" here.
- Type
(details: DialogDismissAttemptDetails) → void- Parameters
details– DialogDismissAttemptDetails- Returns
void
onOpenChangeComplete?(open: boolean) → void
Runs once the dialog has finished opening or closing, after any animation.
- Parameters
open– boolean- Returns
void
onPayloadChange?(payload: unknown, details: ChangeDetails<AlertDialogPayloadChangeReason>) → void
Runs when a trigger or a controller with another payload opens the alert dialog, before onOpenChange, or a controller's open({ payload }) gives the open one another payload. details.cancel() keeps the previous payload.
- Type
(payload: unknown, details: ChangeDetails<AlertDialogPayloadChangeReason>) → void- Parameters
payload– unknowndetails– ChangeDetails<AlertDialogPayloadChangeReason>- Returns
void
pendingAnnouncement?string
What screen readers hear once a Close part's action has been running for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
- Default
"Loading"
persistentElements?(string | Element)[] | (() → Element[])
Elements that stay usable while the dialog makes the page inert, like a chat launcher or a password manager's button. Elements, CSS selectors, or a function that returns elements, read when the dialog opens. Presses on them never close it.
- Type
(string | Element)[] | (() → Element[])- Parameters
string | Element)[] | ((- Returns
Element[])
AlertDialogTrigger
<button> data-slot="alert-dialog-trigger"
controller?PopupController<any, AlertDialogOpenChangeReason, {}, any, DialogPayloadChangeReason>
Connects a trigger outside the alert dialog to the alert dialog with this controller.
- Type
PopupController<any, AlertDialogOpenChangeReason, {}, any, DialogPayloadChangeReason>
disabled?boolean
Stops the trigger from opening the dialog. A native button can't be focused or pressed, unless focusableWhenDisabled is set.
- Default
false
focusableWhenDisabled?boolean
Keeps a disabled trigger in the tab order with aria-disabled instead of the native attribute, so it can host a tooltip that says why. Presses still do nothing.
- Default
false
payload?unknown
Data for the content to render when this trigger opens the dialog, like the row it belongs to.
shortcut?string
A keyboard shortcut that presses the trigger, like "mod+k". mod is Command on Apple platforms and Control elsewhere. Sets aria-keyshortcuts, and a Shortcut part inside shows it. A modal dialog pauses the page's shortcuts while it's open.
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
data-closed?string
Present while the dialog is closed, including while it animates out.
data-disabled?string
Present when the trigger is disabled.
data-open?string
Present while the dialog is open. On a trigger, while it's open from that trigger.
AlertDialogPortal
AlertDialogOverlay
<div> data-slot="alert-dialog-backdrop"
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
data-closed?string
Present while the dialog is closed, including while it animates out.
data-ending-style?string
Present while the dialog animates out. Style the state you animate to.
data-open?string
Present while the dialog is open. On a trigger, while it's open from that trigger.
data-starting-style?string
Present for the first frame. Style the state you animate from.
AlertDialogContent
<div> data-slot="alert-dialog-content"
container?DocumentFragment | Element | null
Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.
- Type
DocumentFragment | Element | null
finalFocus?PopupFocusTarget
Where focus goes when the dialog closes while focus is inside it.
- Default
"trigger"
initialFocus?DialogInitialFocus
Where focus goes when the dialog opens. Opening with touch focuses the dialog itself, so no on-screen keyboard appears, unless this is set to something other than "auto".
- Default
"auto"
inline?boolean
Renders the dialog and its backdrop where they are in the page instead of above it, so it can stay inside a frame, like an app preview. Pair it with modal="trap-focus" to keep the rest of the page usable. A container on the dialog still moves its backdrop, so leave it off.
- Default
false
keepMounted?boolean
Whether the dialog stays in the page, hidden, while closed. Its content keeps its state between opens.
- Default
false
size?"default" | "sm"
The size.
- Type
"default" | "sm"- Default
"default"
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
data-closed?string
Present while the dialog is closed, including while it animates out.
data-ending-style?string
Present while the dialog animates out. Style the state you animate to.
data-keyboard?string
Present while the on-screen keyboard covers part of the page.
data-open?string
Present while the dialog is open. On a trigger, while it's open from that trigger.
data-size?string
Mirrors size.
data-starting-style?string
Present for the first frame. Style the state you animate from.
--dialog-stackstring
How many dialogs are open above this one, nested inside it. 0 for the topmost.
--keyboard-insetstring
How many pixels of the page the on-screen keyboard covers, 0 without one.
--visual-viewport-heightstring
The height of the part of the page you can see, above the keyboard.
--visual-viewport-topstring
How far the part of the page you can see is scrolled from the top of the layout, as when the keyboard pushes it up.
--visual-viewport-widthstring
The width of the part of the page you can see.
AlertDialogHeader
<div> data-slot="alert-dialog-header"
description?string
The description of the alert dialog.
title?string
The title of the alert dialog.
class?string
Merged with the part's classes.
AlertDialogFooter
<div> data-slot="alert-dialog-footer"
class?string
Merged with the part's classes.
AlertDialogMedia
<div> data-slot="alert-dialog-media"
class?string
Merged with the part's classes.
AlertDialogTitle
<h2> data-slot="dialog-title"
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AlertDialogDescription
<p> data-slot="dialog-description"
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AlertDialogAction
<button> data-slot="alert-dialog-close"
action?(event: Event, details: ActionDetails) → unknown
Runs when the part is pressed, before the dialog closes. A returned Promise keeps the dialog open while it runs: the part gets data-pending, aria-busy and aria-disabled, screen readers hear the dialog's pendingAnnouncement after a second, and nothing but the controller closes the dialog. It closes with reason "action" once the Promise resolves. A rejection keeps it open and puts the reason in the part's error state until the next press, or until the dialog opens again. details.source says how the part was pressed, for changes made after an await.
- Type
(event: Event, details: ActionDetails) → unknown- Parameters
event– Eventdetails– ActionDetails- Returns
unknown
clickEffect?boolean
Apply a click effect to the button.
- Default
true
debug?boolean
Logs every state change with its reason to the console, in development builds.
disabled?boolean
Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.
- Default
false
focusableWhenDisabled?boolean
Keeps a disabled button in the tab order with aria-disabled instead of the native attribute, so it can hold focus while it becomes disabled and host a tooltip that says why. Presses still do nothing.
- Default
false
isLoading?boolean
Show a loading indicator.
- Default
false
onclick?( event: MouseEvent, details?: ButtonActionDetails ) → unknown
Runs when the button is pressed. Return a Promise to keep it pending
(data-pending) until it settles. details.source says how it was
pressed; a wrapper passing the click on may leave it out.
- Type
( event: MouseEvent, details?: ButtonActionDetails ) → unknown- Parameters
event– React.MouseEvent<HTMLButtonElement>details?– ButtonActionDetails- Returns
unknown
pendingAnnouncement?string
What screen readers hear once the button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
- Default
"Loading"
type?"button" | "reset" | "submit"
What a native button does in a form: "submit" submits it and "reset" resets it. Ignored when the button renders as something else.
- Type
"button" | "reset" | "submit"- Default
"button"
value?string
Reported as returnValue in onOpenChange and by the controller's open(). createConfirm() resolves true for "confirm".
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
data-disabled?stringdata-pending?string
Present on a Close part while its action runs.
AlertDialogCancel
<button> data-slot="alert-dialog-close"
action?(event: Event, details: ActionDetails) → unknown
Runs when the part is pressed, before the dialog closes. A returned Promise keeps the dialog open while it runs: the part gets data-pending, aria-busy and aria-disabled, screen readers hear the dialog's pendingAnnouncement after a second, and nothing but the controller closes the dialog. It closes with reason "action" once the Promise resolves. A rejection keeps it open and puts the reason in the part's error state until the next press, or until the dialog opens again. details.source says how the part was pressed, for changes made after an await.
- Type
(event: Event, details: ActionDetails) → unknown- Parameters
event– Eventdetails– ActionDetails- Returns
unknown
value?string
Reported as returnValue in onOpenChange and by the controller's open(). createConfirm() resolves true for "confirm".
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
data-disabled?stringdata-pending?string
Present on a Close part while its action runs.











