Skip to contents

Alert Dialog

A dialog that requires a user response to proceed.

Anatomy

Import the component and assemble its parts:

Examples

Open from a menu

In order to open a dialog using a menu, control the dialog state and open it imperatively using the onclick handler on the menu item. Bind open on <AlertDialog.Root> and set it from that handler.

Close confirmation

This example shows a nested confirmation dialog that opens if the text entered in the parent dialog is going to be discarded.

To implement this, both dialogs should be controlled. The confirmation dialog may be opened when onOpenChange callback of the parent dialog receives a request to close. This way, the confirmation is automatically shown when the user clicks the backdrop, presses the Esc key, or clicks a close button. Call eventDetails.cancel() in that callback to keep the parent dialog open while the confirmation shows.

Use the [data-nested-dialog-open] selector and the var(--nested-dialogs) CSS variable to customize the styling of the parent dialog. Backdrops of the child dialogs won't be rendered so that you can present the parent dialog in a clean way behind the one on top of it.

Detached triggers

An alert dialog can be controlled by a trigger located either inside or outside the <AlertDialog.Root> component. For simple, one-off interactions, place the <AlertDialog.Trigger> inside <AlertDialog.Root>, as shown in the example at the top of this page.

However, if defining the alert dialog's content next to its trigger is not practical, you can use a detached trigger. This involves placing the <AlertDialog.Trigger> outside of <AlertDialog.Root> and linking them with a handle created by the AlertDialog.createHandle() function.

The imperative methods on the handle, such as open() and openWithPayload(), require an <AlertDialog.Root> using the same handle to be mounted. Calls made while no root is attached to the handle — before one mounts, or after it unmounts — are ignored. Each time a root mounts, it starts from fresh state: a call made while no root was attached is not replayed, and no open state carries over from a previous mount.

Multiple triggers

A single alert dialog can be opened by multiple trigger elements. You can achieve this by using the same handle for several detached triggers, or by placing multiple <AlertDialog.Trigger> components inside a single <AlertDialog.Root>.

The alert dialog can render different content depending on which trigger opened it. This is achieved by passing a payload to the <AlertDialog.Trigger> and reading it from the children snippet of <AlertDialog.Root>, which receives { payload }.

The payload can be strongly typed by providing a type argument to the createHandle() function:

Controlled mode with multiple triggers

You can control the alert dialog's open state externally using the open and onOpenChange props on <AlertDialog.Root>. This allows you to manage the alert dialog's visibility based on your application's state. When using multiple triggers, you have to manage which trigger is active with the triggerId prop on <AlertDialog.Root> and the id prop on each <AlertDialog.Trigger>.

Note that there is no separate onTriggerIdChange prop. Instead, the onOpenChange callback receives an additional argument, eventDetails, which contains the trigger element that initiated the state change.

API reference

Root

Groups all parts of the alert dialog. Doesn't render its own HTML element. Always modal. Outside presses do not close it.

onOpenChange function —
Description

Called before the open state changes. Call eventDetails.cancel() to veto it.

Type
(open: boolean, eventDetails: AlertDialogChangeEventDetails) => void
handle AlertDialogHandle<Payload> —
Name
Description

Connects detached triggers. Create one with AlertDialog.createHandle().

Type
AlertDialogHandle<Payload>
children Snippet<[{ payload: Payload | undefined }]> —
Type
Snippet<[{ payload: Payload | undefined }]>
open boolean false
Name
Description

Whether the dialog is open. Use bind:open to share it with the parent. A value passed without bind: holds until the parent changes it.

Type
boolean
Default
false
Binding
bind:open
onOpenChangeComplete function —
Description

Called after the open or close animation finishes.

Type
(open: boolean) => void
triggerId string | null null
Description

Id of the trigger that owns the dialog. Use bind:triggerId to share it with the parent. Pass it with an initial open to name the trigger the dialog starts from. An id written by a trigger press resets to null once the popup unmounts.

Type
string | null
Default
null
Binding
bind:triggerId

Trigger

A button that opens the alert dialog. Renders a <button> element.

handle AlertDialogHandle<Payload> —
Name
Description

Associates this trigger with an alert dialog that is not its parent.

Type
AlertDialogHandle<Payload>
id string | null —
Name
Type
string | null
payload Payload —
Name
Description

Passed to the dialog when this trigger opens it.

Type
Payload
nativeButton boolean true
Description

false renders through render instead of a native button.

Type
boolean
Default
true
render Snippet<[props: DialogTriggerHostProps, state: DialogTriggerState, children: Snippet | undefined]> —
Name
Type
Snippet<[props: DialogTriggerHostProps, state: DialogTriggerState, children: Snippet | undefined]>
disabled boolean
Name
disabled
Description

Whether the trigger is currently disabled.

Type
boolean
open boolean
Name
open
Description

Whether the dialog is open and was opened by this trigger.

Type
boolean
data-popup-open
Present when the corresponding alert dialog is open.
data-disabled
Present when the trigger is disabled.

Portal

A portal element that moves the popup to a different part of the DOM. Renders through the shared FloatingPortal, which appends to <body> by default.

keepMounted boolean false
Description

Keep the portal mounted while the popup is closed.

Type
boolean
Default
false
container HTMLElement | ShadowRoot | null —
Description

Element the portal is appended to. undefined uses the parent portal or document.body. null waits and does not mount until an element or shadow root is set. There is no ref object.

Type
HTMLElement | ShadowRoot | null
render Snippet<[props: HTMLAttributes<HTMLDivElement> & Record<symbol, Attachment<HTMLDivElement>>, state: DialogPortalState, children: Snippet | undefined]> —
Name
Description

Replace the portal element. The snippet receives host props, an empty state, and the children snippet.

Type
Snippet<[props: HTMLAttributes<HTMLDivElement> & Record<symbol, Attachment<HTMLDivElement>>, state: DialogPortalState, children: Snippet | undefined]>

Backdrop

An overlay displayed beneath the popup. Renders a <div> element.

forceRender boolean false
Description

Render the backdrop even when this dialog is nested.

Type
boolean
Default
false
render Snippet<[props: HTMLAttributes<HTMLDivElement> & Record<symbol, Attachment<HTMLDivElement>>, state: DialogBackdropState, children: Snippet | undefined]> —
Name
Type
Snippet<[props: HTMLAttributes<HTMLDivElement> & Record<symbol, Attachment<HTMLDivElement>>, state: DialogBackdropState, children: Snippet | undefined]>
open boolean
Name
open
Type
boolean
transitionStatus 'starting' | 'ending' | 'idle' | undefined
Name
transitionStatus
Type
'starting' | 'ending' | 'idle' | undefined
data-open
Present when the dialog is open.
data-closed
Present when the dialog is closed.
data-starting-style
Present when the dialog begins animating in.
data-ending-style
Present when the dialog is animating out.

Viewport

A positioning container for the dialog popup. Renders a <div> element.

render Snippet<[props: HTMLAttributes<HTMLDivElement> & Record<symbol, Attachment<HTMLDivElement>>, state: DialogViewportState, children: Snippet | undefined]> —
Name
Type
Snippet<[props: HTMLAttributes<HTMLDivElement> & Record<symbol, Attachment<HTMLDivElement>>, state: DialogViewportState, children: Snippet | undefined]>
open boolean
Name
open
Description

Whether the dialog is currently open.

Type
boolean
transitionStatus 'starting' | 'ending' | 'idle' | undefined
Name
transitionStatus
Description

Open/close motion phase.

Type
'starting' | 'ending' | 'idle' | undefined
nested boolean
Name
nested
Description

Whether this dialog is nested inside a parent dialog.

Type
boolean
nestedDialogOpen boolean
Name
nestedDialogOpen
Description

Whether a dialog nested inside this one is open.

Type
boolean
data-open
Present when the dialog is open.
data-closed
Present when the dialog is closed.
data-nested
Present when the dialog is nested within another dialog.
data-nested-dialog-open
Present when the dialog has other open dialogs nested within it.
data-starting-style
Present when the dialog begins animating in.
data-ending-style
Present when the dialog is animating out.

A container for the dialog contents. Renders a <div> element.

initialFocus DialogFocusTarget —
Description

Element to focus when the dialog opens. Touch opens focus the popup itself unless this says otherwise. Pass an element or a function. There is no ref object.

Type
DialogFocusTarget
finalFocus DialogFocusTarget —
Description

Element to focus when the dialog closes. Pass an element or a function. There is no ref object.

Type
DialogFocusTarget
render Snippet<[props: HTMLAttributes<HTMLDivElement> & Record<symbol, Attachment<HTMLDivElement>>, state: DialogPopupState, children: Snippet | undefined]> —
Name
Type
Snippet<[props: HTMLAttributes<HTMLDivElement> & Record<symbol, Attachment<HTMLDivElement>>, state: DialogPopupState, children: Snippet | undefined]>
open boolean
Name
open
Description

Whether the dialog is currently open.

Type
boolean
transitionStatus 'starting' | 'ending' | 'idle' | undefined
Name
transitionStatus
Description

Open/close motion phase.

Type
'starting' | 'ending' | 'idle' | undefined
nested boolean
Name
nested
Description

Whether this dialog is nested inside a parent dialog.

Type
boolean
nestedDialogOpen boolean
Name
nestedDialogOpen
Description

Whether a dialog nested inside this one is open.

Type
boolean
data-open
Present when the dialog is open.
data-closed
Present when the dialog is closed.
data-nested
Present when the dialog is nested within another dialog.
data-nested-dialog-open
Present when the dialog has other open dialogs nested within it.
data-starting-style
Present when the dialog begins animating in.
data-ending-style
Present when the dialog is animating out.
--nested-dialogs
Indicates how many dialogs are nested within.

Title

A heading that labels the dialog. Renders an <h2> element.

render Snippet<[props: HTMLAttributes<HTMLHeadingElement> & Record<symbol, Attachment<HTMLHeadingElement>>, state: DialogTitleState, children: Snippet | undefined]> —
Name
Type
Snippet<[props: HTMLAttributes<HTMLHeadingElement> & Record<symbol, Attachment<HTMLHeadingElement>>, state: DialogTitleState, children: Snippet | undefined]>

Description

A paragraph with additional information about the dialog. Renders a <p> element.

render Snippet<[props: HTMLAttributes<HTMLParagraphElement> & Record<symbol, Attachment<HTMLParagraphElement>>, state: DialogDescriptionState, children: Snippet | undefined]> —
Name
Type
Snippet<[props: HTMLAttributes<HTMLParagraphElement> & Record<symbol, Attachment<HTMLParagraphElement>>, state: DialogDescriptionState, children: Snippet | undefined]>

Close

A button that closes the dialog. Renders a <button> element.

nativeButton boolean true
Description

false renders through render instead of a native button.

Type
boolean
Default
true
render Snippet<[props: DialogTriggerHostProps, state: DialogCloseState, children: Snippet | undefined]> —
Name
Type
Snippet<[props: DialogTriggerHostProps, state: DialogCloseState, children: Snippet | undefined]>
disabled boolean
Name
disabled
Type
boolean
data-disabled
Present when the button is disabled.

createHandle

Creates a new handle to connect an AlertDialog.Root with detached AlertDialog.Trigger components. Pass a type argument to type the payload, as in AlertDialog.createHandle<{ message: string }>(). It returns an AlertDialog.Handle<Payload>. The library also exports it by name as createAlertDialogHandle.

Handle

Controls an alert dialog imperatively and associates detached AlertDialog.Trigger components with an AlertDialog.Root. Create one with AlertDialog.createHandle() and pass it to the handle prop of the root and of any triggers rendered outside of it.

The imperative methods take effect only while a root using this handle is mounted; calls made before a root attaches (or after it unmounts) are ignored.

MemberDescription
isOpenWhether the alert dialog is currently open. Read-only. false while no root is attached to the handle.
payloadThe payload of the trigger that opened the alert dialog, or undefined. Read-only.
open(triggerId)Opens the alert dialog, optionally associating it with a trigger. Call it in an event handler or an effect, not during rendering.
openWithPayload(payload)Opens the alert dialog with the given payload, without associating it with any trigger.
close()Closes the alert dialog.
unmount()Removes the popup at once, without waiting for the exit animation.