Alert Dialog
A dialog that requires a user response to proceed.
In Nyte
import { AlertDialog } from "@nyte-ai/ui/alert-dialog";@nyte-ai/ui/alert-dialog re-exports the Base UI namespace unchanged. There is no Nyte styling on
this subpath; the consumer owns every class, StyleX style, and layout decision. Base UI owns the
interaction model, keyboard handling, focus management, and ARIA below.
Consumed by:
desktop/src/renderer/src/components/confirm-dialog.tsx
The rest of this page is Base UI's documentation for @base-ui/react/alert-dialog, reproduced
verbatim under the MIT license, © Material-UI SAS. Component paths in examples refer to the Base UI
package; in Nyte, import from the subpath above.
Anatomy
Import the component and assemble its parts:
import { AlertDialog } from '@base-ui/react/alert-dialog';
<AlertDialog.Root>
<AlertDialog.Trigger />
<AlertDialog.Portal>
<AlertDialog.Backdrop />
<AlertDialog.Viewport>
<AlertDialog.Popup>
<AlertDialog.Title />
<AlertDialog.Description />
<AlertDialog.Close />
</AlertDialog.Popup>
</AlertDialog.Viewport>
</AlertDialog.Portal>
</AlertDialog.Root>;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.
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.
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.
const demoAlertDialog = AlertDialog.createHandle();
// @highlight
// @highlight-text "handle={demoAlertDialog}"
<AlertDialog.Trigger handle={demoAlertDialog}>Open</AlertDialog.Trigger>
// @highlight
// @highlight-text "handle={demoAlertDialog}"
<AlertDialog.Root handle={demoAlertDialog}>
...
</AlertDialog.Root>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>.
<AlertDialog.Root>
<AlertDialog.Trigger>Trigger 1</AlertDialog.Trigger>
<AlertDialog.Trigger>Trigger 2</AlertDialog.Trigger>
...
</AlertDialog.Root>const demoAlertDialog = AlertDialog.createHandle();
<AlertDialog.Trigger handle={demoAlertDialog}>Trigger 1</AlertDialog.Trigger>
<AlertDialog.Trigger handle={demoAlertDialog}>Trigger 2</AlertDialog.Trigger>
<AlertDialog.Root handle={demoAlertDialog}>
...
</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 using the function-as-a-child pattern in <AlertDialog.Root>.
The payload can be strongly typed by providing a type argument to the createHandle() function:
// @highlight
const demoAlertDialog = AlertDialog.createHandle<{ message: string }>();
// @highlight
// @highlight-text "payload"
<AlertDialog.Trigger handle={demoAlertDialog} payload={{ message: 'Trigger 1' }}>
Trigger 1
</AlertDialog.Trigger>
// @highlight
// @highlight-text "payload"
<AlertDialog.Trigger handle={demoAlertDialog} payload={{ message: 'Trigger 2' }}>
Trigger 2
</AlertDialog.Trigger>
<AlertDialog.Root handle={demoAlertDialog}>
{({ payload }) => ( // @highlight-text "payload"
<AlertDialog.Portal>
<AlertDialog.Popup>
<AlertDialog.Title>Alert dialog</AlertDialog.Title>
{payload !== undefined && ( // @highlight-text "payload"
<AlertDialog.Description>
Confirming {payload.message} {/* @highlight-text "payload" */}
</AlertDialog.Description>
)}
</AlertDialog.Popup>
</AlertDialog.Portal>
)}
</AlertDialog.Root>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.
Root Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| defaultOpen | boolean | false | Whether the dialog is initially open. To render a controlled dialog, use the open prop instead. |
| open | boolean | - | Whether the dialog is currently open. |
| onOpenChange | ((open: boolean, eventDetails: AlertDialog.Root.ChangeEventDetails) => void) | - | Event handler called when the alert dialog is opened or closed. |
| actionsRef | React.RefObject<AlertDialog.Root.Actions | null> | - | A ref to imperative actions. unmount: Manually unmounts the alert dialog.
Call this after any externally controlled closing animation finishes.close: Closes the alert dialog imperatively when called. |
| defaultTriggerId | string | null | - | ID of the trigger that the dialog is associated with.
This is useful in conjunction with the defaultOpen prop to create an initially open dialog. |
| handle | AlertDialog.Handle<Payload> | - | 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. |
| onOpenChangeComplete | ((open: boolean) => void) | - | Event handler called after any animations complete when the dialog is opened or closed. |
| triggerId | 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). |
| children | React.ReactNode | PayloadChildRenderFunction<Payload> | - | The content of the dialog.
This can be a regular React node or a render function that receives the payload of the active trigger. |
Root.Props
Re-export of Root props.
Root.State
type AlertDialogRootState = {};Root.Actions
type AlertDialogRootActions = { unmount: () => void; close: () => void };Root.ChangeEventReason
type AlertDialogRootChangeEventReason =
| 'trigger-press'
| 'outside-press'
| 'escape-key'
| 'close-press'
| 'focus-out'
| 'imperative-action'
| 'none';Root.ChangeEventDetails
type AlertDialogRootChangeEventDetails = (
| { reason: 'trigger-press'; event: MouseEvent | PointerEvent | TouchEvent | KeyboardEvent }
| { reason: 'outside-press'; event: MouseEvent | PointerEvent | TouchEvent }
| { reason: 'escape-key'; event: KeyboardEvent }
| { reason: 'close-press'; event: MouseEvent | PointerEvent | KeyboardEvent }
| { reason: 'focus-out'; event: KeyboardEvent | FocusEvent }
| { reason: 'imperative-action'; event: Event }
| { reason: 'none'; event: Event }
) & {
/** Cancels Base UI from handling the event. */
cancel: () => void;
/** Allows the event to propagate in cases where Base UI will stop the propagation. */
allowPropagation: () => void;
/** Indicates whether the event has been canceled. */
isCanceled: boolean;
/** Indicates whether the event is allowed to propagate. */
isPropagationAllowed: boolean;
/** The element that triggered the event, if applicable. */
trigger: Element | undefined;
preventUnmountOnClose: preventUnmountOnClose;
};Trigger
A button that opens the alert dialog.
Renders a <button> element.
Trigger Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| handle | AlertDialog.Handle<Payload> | - | A handle to associate the trigger with an alert dialog. Can be created with the AlertDialog.createHandle() method. |
| nativeButton | boolean | true | Whether the component renders a native <button> element when replacing it
via the render prop.
Set to false if the rendered element is not a button (for example, <div>). |
| payload | Payload | - | A payload to pass to the dialog when it is opened. |
| id | 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 triggerId prop). |
| className | string | ((state: AlertDialog.Trigger.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: AlertDialog.Trigger.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: AlertDialog.Trigger.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Trigger Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-popup-open | - | Present when the corresponding alert dialog is open. |
| data-disabled | - | Present when the trigger is disabled. |
Trigger.Props
Re-export of Trigger props.
Trigger.State
type AlertDialogTriggerState = {
/** Whether the trigger is currently disabled. */
disabled: boolean;
/** Whether the dialog is currently open and was opened by this trigger. */
open: boolean;
};Portal
A portal element that moves the popup to a different part of the DOM.
By default, the portal element is appended to <body>.
Renders a <div> element.
Portal Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| container | HTMLElement | ShadowRoot | React.RefObject<HTMLElement | ShadowRoot | null> | null | - | A parent element to render the portal element into. |
| className | string | ((state: AlertDialog.Portal.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: AlertDialog.Portal.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| keepMounted | boolean | false | Whether to keep the portal mounted in the DOM while the popup is hidden. |
| render | ReactElement | ((props: HTMLProps, state: AlertDialog.Portal.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Portal.Props
Re-export of Portal props.
Portal.State
type AlertDialogPortalState = {};Backdrop
An overlay displayed beneath the popup.
Renders a <div> element.
Backdrop Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| forceRender | boolean | false | Whether the backdrop is forced to render even when nested. |
| className | string | ((state: AlertDialog.Backdrop.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: AlertDialog.Backdrop.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: AlertDialog.Backdrop.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Backdrop Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| 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. |
Backdrop.Props
Re-export of Backdrop props.
Backdrop.State
type AlertDialogBackdropState = {
/** Whether the dialog is currently open. */
open: boolean;
/** The transition status of the component. */
transitionStatus: TransitionStatus;
};Popup
A container for the dialog contents.
Renders a <div> element.
Popup Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| initialFocus | boolean | React.RefObject<HTMLElement | null> | ((openType: InteractionType) => boolean | void | HTMLElement | null) | - | 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).RefObject: Move focus to the ref element.function: Called with the interaction type (mouse, touch, pen, or keyboard).
Return an element to focus, true to use the default behavior, null to fall back to the default behavior, or false/undefined to do nothing. |
| finalFocus | boolean | React.RefObject<HTMLElement | null> | ((closeType: InteractionType) => boolean | void | HTMLElement | null) | - | 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).RefObject: Move focus to the ref element.function: Called with the interaction type (mouse, touch, pen, or keyboard).
Return an element to focus, true to use the default behavior, null to fall back to the default behavior, or false/undefined to do nothing. |
| className | string | ((state: AlertDialog.Popup.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: AlertDialog.Popup.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: AlertDialog.Popup.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Popup Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| 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. |
Popup CSS Variables:
| Variable | Type | Description |
|---|---|---|
--nested-dialogs | number | Indicates how many dialogs are nested within. |
Popup.Props
Re-export of Popup props.
Popup.State
type AlertDialogPopupState = {
/** Whether the dialog is currently open. */
open: boolean;
/** The transition status of the component. */
transitionStatus: TransitionStatus;
/** Whether the dialog is nested within a parent dialog. */
nested: boolean;
/** Whether the dialog has nested dialogs open. */
nestedDialogOpen: boolean;
};Title
A heading that labels the dialog.
Renders an <h2> element.
Title Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | ((state: AlertDialog.Title.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: AlertDialog.Title.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: AlertDialog.Title.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Title.Props
Re-export of Title props.
Title.State
type AlertDialogTitleState = {};Description
A paragraph with additional information about the dialog.
Renders a <p> element.
Description Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | ((state: AlertDialog.Description.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: AlertDialog.Description.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: AlertDialog.Description.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Description.Props
Re-export of Description props.
Description.State
type AlertDialogDescriptionState = {};Close
A button that closes the dialog.
Renders a <button> element.
Close Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| nativeButton | boolean | true | Whether the component renders a native <button> element when replacing it
via the render prop.
Set to false if the rendered element is not a button (for example, <div>). |
| className | string | ((state: AlertDialog.Close.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: AlertDialog.Close.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: AlertDialog.Close.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Close Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| data-disabled | - | Present when the button is disabled. |
Close.Props
Re-export of Close props.
Close.State
type AlertDialogCloseState = {
/** Whether the button is currently disabled. */
disabled: boolean;
};Viewport
A positioning container for the dialog popup that can be made scrollable.
Renders a <div> element.
Viewport Props:
| Prop | Type | Default | Description |
|---|---|---|---|
| className | string | ((state: AlertDialog.Viewport.State) => string | undefined) | - | CSS class applied to the element, or a function that returns a class based on the component's state. |
| style | React.CSSProperties | ((state: AlertDialog.Viewport.State) => React.CSSProperties | undefined) | - | Style applied to the element, or a function that returns a style object based on the component's state. |
| render | ReactElement | ((props: HTMLProps, state: AlertDialog.Viewport.State) => ReactElement) | - | Allows you to replace the component's HTML element
with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render. |
Viewport Data Attributes:
| Attribute | Type | Description |
|---|---|---|
| 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. |
Viewport.Props
Re-export of Viewport props.
Viewport.State
type AlertDialogViewportState = {
/** Whether the dialog is currently open. */
open: boolean;
/** The transition status of the component. */
transitionStatus: TransitionStatus;
/** Whether the dialog is nested within another dialog. */
nested: boolean;
/** Whether the dialog has nested dialogs open. */
nestedDialogOpen: boolean;
};createHandle
Creates a new handle to connect an AlertDialog.Root with detached AlertDialog.Trigger components.
Return Value:
type ReturnValue = AlertDialog.Handle<Payload>;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.
Properties:
| Property | Type | Modifiers | Description |
|---|---|---|---|
| isOpen | boolean | readonly | Whether the dialog is currently open. Returns false while no root is attached to the handle. |
Methods:
function open(triggerId: string | null): void;Opens the dialog, optionally associating it with a trigger.
This method should only be called in an event handler or an effect (not during rendering).
function openWithPayload(payload: Payload): void;Opens the dialog with the given payload, without associating it with any trigger.
This method should only be called in an event handler or an effect (not during rendering).
function close(): void;Closes the dialog.
This method should only be called in an event handler or an effect (not during rendering).
External Types
PayloadChildRenderFunction
type PayloadChildRenderFunction = (arg: { payload: unknown | undefined }) => ReactNode;preventUnmountOnClose
type preventUnmountOnClose = () => void;InteractionType
type InteractionType = 'mouse' | 'touch' | 'pen' | 'keyboard' | '';Export Groups
AlertDialog.Root:AlertDialog.Root,AlertDialog.Root.State,AlertDialog.Root.Props,AlertDialog.Root.Actions,AlertDialog.Root.ChangeEventReason,AlertDialog.Root.ChangeEventDetailsAlertDialog.Backdrop:AlertDialog.Backdrop,AlertDialog.Backdrop.Props,AlertDialog.Backdrop.StateAlertDialog.Close:AlertDialog.Close,AlertDialog.Close.Props,AlertDialog.Close.StateAlertDialog.Description:AlertDialog.Description,AlertDialog.Description.Props,AlertDialog.Description.StateAlertDialog.Popup:AlertDialog.Popup,AlertDialog.Popup.Props,AlertDialog.Popup.StateAlertDialog.Portal:AlertDialog.Portal,AlertDialog.Portal.State,AlertDialog.Portal.PropsAlertDialog.Title:AlertDialog.Title,AlertDialog.Title.Props,AlertDialog.Title.StateAlertDialog.Trigger:AlertDialog.Trigger,AlertDialog.Trigger.Props,AlertDialog.Trigger.StateAlertDialog.Viewport:AlertDialog.Viewport,AlertDialog.Viewport.State,AlertDialog.Viewport.PropsAlertDialog.HandleAlertDialog.createHandleDefault:AlertDialogBackdropProps,AlertDialogBackdropState,AlertDialogCloseProps,AlertDialogCloseState,AlertDialogDescriptionProps,AlertDialogDescriptionState,AlertDialogPopupProps,AlertDialogPopupState,AlertDialogPortalProps,AlertDialogPortalState,AlertDialogTitleProps,AlertDialogTitleState,AlertDialogViewportProps,AlertDialogViewportState,AlertDialogRootState,AlertDialogRootProps,AlertDialogRootActions,AlertDialogRootChangeEventReason,AlertDialogRootChangeEventDetails,AlertDialogTriggerProps,AlertDialogTriggerState
Canonical Types
Maps Canonical: Alias — Use Canonical when its namespace is already imported; otherwise use Alias.
AlertDialog.Root.State:AlertDialogRootStateAlertDialog.Root.Props:AlertDialogRootPropsAlertDialog.Root.Actions:AlertDialogRootActionsAlertDialog.Root.ChangeEventReason:AlertDialogRootChangeEventReasonAlertDialog.Root.ChangeEventDetails:AlertDialogRootChangeEventDetailsAlertDialog.Backdrop.Props:AlertDialogBackdropPropsAlertDialog.Backdrop.State:AlertDialogBackdropStateAlertDialog.Close.Props:AlertDialogClosePropsAlertDialog.Close.State:AlertDialogCloseStateAlertDialog.Description.Props:AlertDialogDescriptionPropsAlertDialog.Description.State:AlertDialogDescriptionStateAlertDialog.Popup.Props:AlertDialogPopupPropsAlertDialog.Popup.State:AlertDialogPopupStateAlertDialog.Portal.State:AlertDialogPortalStateAlertDialog.Portal.Props:AlertDialogPortalPropsAlertDialog.Title.Props:AlertDialogTitlePropsAlertDialog.Title.State:AlertDialogTitleStateAlertDialog.Trigger.Props:AlertDialogTriggerPropsAlertDialog.Trigger.State:AlertDialogTriggerStateAlertDialog.Viewport.State:AlertDialogViewportStateAlertDialog.Viewport.Props:AlertDialogViewportProps