AlertDialog
A dialog that interrupts with a question that must be answered, such as confirming a deletion, without a way to click it away.
An alert dialog asks a question the reader must answer before going on: delete this, discard those changes. Unlike a dialog, it does not close on a click outside, and it is announced as an alert. For the common case of confirming one action with a busy state, use the composed ConfirmActionDialog, which is built on it.
The scheme exists.
// apps/docs/examples/ui/alert-dialog.tsx'use client';
import {useState} from 'react';import {Trash2Icon} from 'lucide-react';
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogMedia, AlertDialogTitle, AlertDialogTrigger,} from '@workspace/active-ui/components/primitives/alert-dialog';import {Button} from '@workspace/active-ui/components/primitives/button';
/** * A question that must be answered before the reader goes on: delete a * scheme, or discard unsaved changes. The cancel button closes the dialog by * itself; the action is an ordinary button, so it does its work and closes * the dialog through the open state. */export default function AlertDialogExample() { const [open, setOpen] = useState(false); const [deleted, setDeleted] = useState(false); return ( <div className="flex flex-col items-center gap-4"> <div className="flex flex-wrap justify-center gap-2"> <AlertDialog open={open} onOpenChange={setOpen}> <AlertDialogTrigger render={<Button variant="destructive" disabled={deleted} />} > Delete scheme </AlertDialogTrigger> <AlertDialogContent> <AlertDialogHeader> <AlertDialogMedia> <Trash2Icon aria-hidden /> </AlertDialogMedia> <AlertDialogTitle>Delete Pension scheme 2026?</AlertDialogTitle> <AlertDialogDescription> The scheme and its 214 participants are removed. This cannot be undone. </AlertDialogDescription> </AlertDialogHeader> <AlertDialogFooter> <AlertDialogCancel>Keep the scheme</AlertDialogCancel> <AlertDialogAction variant="destructive" onClick={() => { setDeleted(true); setOpen(false); }} > Delete </AlertDialogAction> </AlertDialogFooter> </AlertDialogContent> </AlertDialog>
<AlertDialog> <AlertDialogTrigger render={<Button variant="outline" />}> Leave the form </AlertDialogTrigger> <AlertDialogContent size="sm"> <AlertDialogHeader> <AlertDialogTitle>Discard your changes?</AlertDialogTitle> <AlertDialogDescription> The changes you made to this form are not saved. </AlertDialogDescription> </AlertDialogHeader> <AlertDialogFooter> <AlertDialogCancel>Stay</AlertDialogCancel> <AlertDialogCancel variant="default">Discard</AlertDialogCancel> </AlertDialogFooter> </AlertDialogContent> </AlertDialog> </div> <p className="text-muted-foreground text-sm" aria-live="polite"> {deleted ? 'The scheme was deleted.' : 'The scheme exists.'} </p> {deleted ? ( <Button variant="ghost" size="sm" onClick={() => setDeleted(false)}> Reset the example </Button> ) : null} </div> );}Usage#
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger,} from '@workspace/active-ui/components/primitives/alert-dialog';
<AlertDialog open={open} onOpenChange={setOpen}> <AlertDialogTrigger render={<Button variant="destructive" />}> Delete </AlertDialogTrigger> <AlertDialogContent> <AlertDialogHeader> <AlertDialogTitle>Delete this scheme?</AlertDialogTitle> <AlertDialogDescription>This cannot be undone.</AlertDialogDescription> </AlertDialogHeader> <AlertDialogFooter> <AlertDialogCancel>Cancel</AlertDialogCancel> <AlertDialogAction variant="destructive" onClick={remove}> Delete </AlertDialogAction> </AlertDialogFooter> </AlertDialogContent></AlertDialog>;Anatomy#
Alert: the open state;Dialog openandon, or uncontrolled.Open Change Alert: the element that opens it;Dialog Trigger renderuses your own button.Alert: the window with its backdrop.Dialog Content size:default, orsmfor a short question with the buttons side by side.AlertwithDialog Header AlertandDialog Title Alert;Dialog Description Alertfor an icon beside the title.Dialog Media Alertwith the answers:Dialog Footer Alertcloses the dialog (Base UI's close part, styled as anDialog Cancel outlinebutton, withvariantandsize);Alertis a plain Button: it does not close the dialog. Do the work in itsDialog Action onand close throughClick on, or after the work finished, as in the example. That keeps a failed action from closing the question.Open Change
What your application provides#
The title, the description and the labels of both answers, worded as the outcome ("Delete", "Keep the scheme"), not "OK".
The open state when the action is more than a click, and the work the action starts.
Accessibility and keyboard#
The window is a
role="alertdialog", named by its title and described by its description.Opening moves the focus inside;
Tabstays inside.Escapecloses it like the cancel button.A click on the backdrop does nothing: the question has to be answered.
On close, the focus returns to the trigger.
API#
AlertDialogContent#
Import from @workspace/
Props marked Base UI are behaviour of the Base UI part this component is built on.
Props
- Type
booleanRefObject<HTMLElement | null> (closeType) => โฆ
Determines the element to focus when the dialog is closed.
false: Do not move focus.true: Move focus based on the default behavior (trigger or previously focused element).Ref: Move focus to the ref element.Object function: Called with the interaction type (mouse,touch,pen, orkeyboard). Return an element to focus,trueto use the default behavior,nullto fall back to the default behavior, orfalse/undefinedto do nothing.
- Type
booleanRefObject<HTMLElement | null> (openType) => โฆ
Determines the element to focus when the dialog is opened. By default, focus moves to the first tabbable element inside the popup, except when the dialog is opened by touch โ then the popup itself is focused to avoid opening the virtual keyboard.
false: Do not move focus.true: Move focus based on the default behavior (first tabbable element or popup).Ref: Move focus to the ref element.Object function: Called with the interaction type (mouse,touch,pen, orkeyboard). Return an element to focus,trueto use the default behavior,nullto fall back to the default behavior, orfalse/undefinedto do nothing.
- Type
"default""sm"- Default
'default'
The width and layout: default, or sm for a short question with the answers side by side.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
AlertDialogCancel#
Import from @workspace/
Props marked Base UI are behaviour of the Base UI part this component is built on.
Props
- Type
"default""icon""icon-lg" "icon-sm" "icon-xs" "lg""sm""xs"null- Default
'default'
The height and padding. xs, sm, default and lg for a button with text; icon-, icon-, icon and icon- for a square button with only an icon, which then needs an accessible name (aria-).
- Type
"default""destructive""ghost""link""outline""secondary"null- Default
'outline'
The visual weight of the button. default for the main action of a screen or form; outline and secondary for other actions; ghost for a quiet action in a toolbar or a list; destructive for an action that deletes or cannot be undone; link for an action that reads as a link. Every variant but link sinks 1px while pressed; a link-styled action keeps its text and icon in place (button-).
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
Other parts#
AlertDialog
All props come from the Base UI part this component is built on.
Props
- Type
boolean
Whether the dialog is currently open.
- Type
boolean- Default
false
Whether the dialog is initially open.
To render a controlled dialog, use the open prop instead.
- Type
PayloadChild Render Function<โฆ> ReactNode
The content of the dialog. This can be a regular React node or a render function that receives the payload of the active trigger.
- Type
string | null
ID of the trigger that the dialog is associated with. This is useful in conjunction with the default prop to create an initially open dialog.
- Type
AlertDialog Handle<unknown>
A handle to associate the alert dialog with a trigger. If specified, allows external triggers to control the alert dialog's open state. Can be created with the AlertDialog.createHandle() method.
- Type
string | null
ID of the trigger that the dialog is associated with. This is useful in conjunction with the open prop to create a controlled dialog. There's no need to specify this prop when the dialog is uncontrolled (that is, when the open prop is not set).
- Type
RefObject<โฆ>
A ref to imperative actions.
unmount: Ends the closing phase of the alert dialog after an externally controlled closing animation finishes. CallpreventinUnmount On Close() onfirst, otherwise the alert dialog completes closing on its own. Whether it leaves the DOM is decided byOpen Change keepon the portal.Mounted close: Closes the alert dialog imperatively when called.
Events
Called by the component when something happens.
- Type
(open, eventDetails) => void
Event handler called when the alert dialog is opened or closed.
- Type
(open: boolean) => void
Event handler called after any animations complete when the dialog is opened or closed.
Takes only the props listed.
AlertDialogAction
Props marked Base UI are behaviour of the Base UI part this component is built on.
Props
- Type
boolean- Default
false
Whether the button should be focusable when disabled.
- Type
"default""icon""icon-lg" "icon-sm" "icon-xs" "lg""sm""xs"null
The height and padding. xs, sm, default and lg for a button with text; icon-, icon-, icon and icon- for a square button with only an icon, which then needs an accessible name (aria-).
- Type
"default""destructive""ghost""link""outline""secondary"null
The visual weight of the button. default for the main action of a screen or form; outline and secondary for other actions; ghost for a quiet action in a toolbar or a list; destructive for an action that deletes or cannot be undone; link for an action that reads as a link. Every variant but link sinks 1px while pressed; a link-styled action keeps its text and icon in place (button-).
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
AlertDialogOverlay
All props come from the Base UI part this component is built on.
Props
- Type
boolean- Default
false
Whether the backdrop is forced to render even when nested.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
AlertDialogPortal
All props come from the Base UI part this component is built on.
Props
- Type
HTMLElementRefObject<โฆ> ShadowRoot null
A parent element to render the portal element into.
- Type
boolean- Default
false
Whether to keep the portal mounted in the DOM while the popup is hidden.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
AlertDialogTrigger
All props come from the Base UI part this component is built on.
Props
- Type
AlertDialog Handle<unknown>
A handle to associate the trigger with an alert dialog. Can be created with the AlertDialog.createHandle() method.
- Type
string
ID of the trigger. In addition to being forwarded to the rendered element, it is also used to specify the active trigger for the dialog in controlled mode (with the DialogRoot trigger prop).
- Type
unknown
A payload to pass to the dialog when it is opened.
- Type
ComponentRender Fn<โฆ> ReactElement<โฆ>
Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a React or a function that returns the element to render.
Other props, such as the standard attributes of the underlying element, className and ref, are passed through.
Parts that take only their elementโs attributes
Alert: the attributes of its element, with className and ref, and Base UI'sDialog Description renderto use your own element.Alert: the attributes of aDialog Header <div>, with className and ref.Alert: the attributes of aDialog Media <div>, with className and ref.Alert: the attributes of its element, with className and ref, and Base UI'sDialog Title renderto use your own element.
This page adapts the Alert Dialog documentation of shadcn/ui (MIT licence) to this package; see Third-party notices.