Alert Dialog
A modal dialog that requires users to confirm or cancel an action.
An alert dialog interrupts users before an action with consequences and blocks the page until they decide. Users close it with the cancel button or the Escape key, and a press outside the dialog doesn't close it. The action button doesn't close it on its own, so its click handler closes the alert dialog once the action has run.
Usage
Examples
Destructive
Deleting a supplier or a promotion removes its data for good. The action button signals this consequence through variant set to "destructive", so users recognize the risk while the cancel button still offers a way back.
Small
A short question needs little room, such as whether to print new shelf labels now. With size set to "sm", the alert dialog stays compact on larger screens as well, so users take in the question and both answers at a single glance.
Default Size
Most confirmations explain their consequence in a sentence or two. Unless another size is set, the alert dialog takes the size "default" and gives that explanation more room on larger screens, so users read it before they decide.
With Media
Users who confirm the same kinds of decisions every day recognize them faster by a symbol than by their wording. Through AlertDialogMedia, an icon or an image accompanies the question, and the title still names the decision where the symbol is unfamiliar.
Controlled
Many decisions take a moment to carry out, such as booking a stock correction, and the alert dialog should stay open until that work is done. With open and onOpenChange, the application holds the open state, closes the alert dialog once the action has run and can open it from other parts of the interface as well.
Disabled Trigger
An alert dialog belongs to an action that is not always available, such as closing a stock count while figures are still arriving. Marked as disabled, the trigger no longer opens the alert dialog, so users never face a decision they cannot make yet.
Disabled Cancel
While an action is already under way, such as a stock booking that has been sent, stepping back is no longer possible. Here, disabled keeps the cancel button from closing the alert dialog and shows users that the decision stands. The Escape key still closes it.
Disabled Action
Some decisions wait for a condition, for example a price change that a second person still has to approve. As long as the action button carries disabled, it accepts no interaction and exposes its state to assistive technology, while users can still leave through the cancel button.
API Reference
AlertDialog
| Prop | open |
|---|---|
| Type | boolean |
| Default | – |
| Description | Sets whether the alert dialog is open and makes it controlled. Update it in onOpenChange. |
| Prop | defaultOpen |
|---|---|
| Type | boolean |
| Default | false |
| Description | Opens the alert dialog at first render. The alert dialog then tracks its open state itself. |
| Prop | onOpenChange |
|---|---|
| Type | (open: boolean, eventDetails: AlertDialogRootChangeEventDetails) => void |
| Default | – |
| Description | Runs when users open or close the alert dialog. It receives the open state that follows. |
| Prop | onOpenChangeComplete |
|---|---|
| Type | (open: boolean) => void |
| Default | – |
| Description | Runs after the alert dialog has opened or closed and its animations have finished. |
| Prop | triggerId |
|---|---|
| Type | string | null |
| Default | – |
| Description | Sets the ID of the trigger that a controlled alert dialog is associated with. An alert dialog without open doesn't need it. |
| Prop | defaultTriggerId |
|---|---|
| Type | string | null |
| Default | – |
| Description | Sets the ID of the trigger that the alert dialog is associated with when defaultOpen opens it at first render. |
| Prop | handle |
|---|---|
| Type | AlertDialogHandle<Payload> |
| Default | – |
| Description | Connects the alert dialog with triggers that are rendered outside of it. Create the handle with AlertDialog.createHandle() from @base-ui/react/alert-dialog. |
| Prop | actionsRef |
|---|---|
| Type | RefObject<AlertDialogRootActions | null> |
| Default | – |
| Description | Receives the functions close and unmount. close closes the alert dialog from code, and unmount removes it from the DOM after a closing animation that the code controls itself. |
| Prop | children |
|---|---|
| Type | ReactNode | ((arg: { payload: Payload | undefined }) => ReactNode) |
| Default | – |
| Description | Holds the parts of the alert dialog. A function receives the payload of the trigger that opened the alert dialog. |
AlertDialogTrigger
| Prop | handle |
|---|---|
| Type | AlertDialogHandle<Payload> |
| Default | – |
| Description | Connects the trigger with an alert dialog that it is rendered outside of. Pass the same handle to AlertDialog. |
| Prop | payload |
|---|---|
| Type | Payload |
| Default | – |
| Description | Passes a value to the alert dialog when this trigger opens it. A function as children of AlertDialog receives it. |
| Prop | id |
|---|---|
| Type | string |
| Default | – |
| Description | Sets the ID of the trigger element. triggerId and defaultTriggerId of AlertDialog refer to it. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the trigger with another component. |
| Prop | nativeButton |
|---|---|
| Type | boolean |
| Default | true |
| Description | Declares whether the rendered element is a native button. Set it to false when render returns a different element. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from opening the alert dialog with this trigger. |
AlertDialogPortal
| Prop | keepMounted |
|---|---|
| Type | boolean |
| Default | false |
| Description | Keeps the portal in the DOM while the alert dialog is closed. |
| Prop | container |
|---|---|
| Type | HTMLElement | ShadowRoot | RefObject<HTMLElement | ShadowRoot | null> | null |
| Default | – |
| Description | Sets the element that the portal is rendered into. Without it, the portal is appended to the body. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the portal with another component. |
AlertDialogOverlay
| Prop | forceRender |
|---|---|
| Type | boolean |
| Default | false |
| Description | Renders the overlay even when the alert dialog is nested in another dialog. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the overlay with another component. |
AlertDialogContent
| Prop | size |
|---|---|
| Type | "default" | "sm" |
| Default | "default" |
| Description | Controls the width of the alert dialog and the layout of its header and footer. With "sm", the alert dialog stays narrow, the header is centered and the two footer buttons share one row. |
| Prop | initialFocus |
|---|---|
| Type | boolean | RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | HTMLElement | null | void) |
| Default | – |
| Description | Determines the element that receives focus when the alert dialog opens. Without it, focus moves to the first focusable element in the alert dialog. |
| Prop | finalFocus |
|---|---|
| Type | boolean | RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | HTMLElement | null | void) |
| Default | – |
| Description | Determines the element that receives focus when the alert dialog closes. Without it, focus returns to the trigger or to the element that had focus before. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the content with another component. |
AlertDialogHeader
AlertDialogHeader groups the media, the title and the description. It renders a div and has no props of its own.
AlertDialogMedia
AlertDialogMedia frames an icon or an image in the header. It renders a div and has no props of its own.
AlertDialogTitle
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the title with another component. |
AlertDialogDescription
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the description with another component. |
AlertDialogFooter
AlertDialogFooter holds the cancel button and the action button. It renders a div and has no props of its own.
AlertDialogCancel
| Prop | variant |
|---|---|
| Type | "default" | "outline" | "secondary" | "ghost" | "destructive" | "link" |
| Default | "outline" |
| Description | Controls the visual emphasis and semantic role of the cancel button. |
| Prop | size |
|---|---|
| Type | "default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg" |
| Default | "default" |
| Description | Controls the dimensions and spacing of the cancel button. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the button that the cancel part renders. variant and size then have no effect. |
| Prop | nativeButton |
|---|---|
| Type | boolean |
| Default | true |
| Description | Declares whether the rendered element is a native button. Set it to false when render returns a different element. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents users from closing the alert dialog with this button. |
AlertDialogAction
| Prop | variant |
|---|---|
| Type | "default" | "outline" | "secondary" | "ghost" | "destructive" | "link" |
| Default | "default" |
| Description | Controls the visual emphasis and semantic role of the action button. |
| Prop | size |
|---|---|
| Type | "default" | "xs" | "sm" | "lg" | "icon" | "icon-xs" | "icon-sm" | "icon-lg" |
| Default | "default" |
| Description | Controls the dimensions and spacing of the action button. |
| Prop | render |
|---|---|
| Type | ReactElement | render function |
| Default | – |
| Description | Replaces the native element or composes the action button with another component. |
| Prop | nativeButton |
|---|---|
| Type | boolean |
| Default | true |
| Description | Declares whether the rendered element is a native button. Set it to false when render returns a different element. |
| Prop | disabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Prevents interaction and exposes the disabled state to assistive technology. |
| Prop | focusableWhenDisabled |
|---|---|
| Type | boolean |
| Default | false |
| Description | Keeps the action button focusable while it is disabled. |