Skip to contents

Dialog

A popup that opens on top of the entire page.

View as Markdown

Anatomy

Import the component and assemble its parts:

Anatomy

Examples

State

By default, Dialog is an uncontrolled component that manages its own state.

Uncontrolled dialog

Use open and onOpenChange props if you need to access or control the state of the dialog. For example, you can control the dialog state in order to open it imperatively from another place in your app.

Controlled dialog

It’s also common to use onOpenChange if your app needs to do something when the dialog is closed or opened. This is recommended over React.useEffect when reacting to state changes.

Running code when dialog state changes

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.

Connecting a dialog to a menu

Nested dialogs

You can nest dialogs within one another normally.

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.

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.

Outside scroll dialog

The dialog can be made scrollable by using <Dialog.Viewport> as an outer scrollable container for <Dialog.Popup> while the popup can extend past the bottom edge. The scrollable area uses the Scroll Area component to provide custom scrollbars.

Inside scroll dialog

The dialog can be made scrollable by making an inner container scrollable while the popup stays fully on screen. <Dialog.Viewport> is used as a positioning container for <Dialog.Popup>, while an inner scrollable area is created using the Scroll Area component.

Placing elements outside the popup

When adding elements that should appear “outside” the colored popup area, continue to place them inside <Dialog.Popup>, but create a child element that has the popup styles. This ensures they are kept in the tab order and announced correctly by screen readers.

<Dialog.Popup> has pointer-events: none, while inner content (the colored popup and close button) has pointer-events: auto so clicks on the backdrop continue to be registered.

Detached triggers

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

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

Detached triggers

Multiple triggers

A single 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 <Dialog.Trigger> components inside a single <Dialog.Root>.

Multiple triggers within the Root part
Multiple detached triggers

The dialog can render different content depending on which trigger opened it. This is achieved by passing a payload to the <Dialog.Trigger> and using the function-as-a-child pattern in <Dialog.Root>.

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

Detached triggers with payload

Controlled mode with multiple triggers

You can control the dialog’s open state externally using the open and onOpenChange props on <Dialog.Root>. This allows you to manage the 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 <Dialog.Root> and the id prop on each <Dialog.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 dialog. Doesn’t render its own HTML element.

defaultOpen
boolean
false
Description

Whether the dialog is initially open.

To render a controlled dialog, use the open prop instead.

Type
boolean | undefined
Default
false
open
boolean
Name
Description

Whether the dialog is currently open.

Type
boolean | undefined
onOpenChange
function
Description

Event handler called when the dialog is opened or closed.

Type
actionsRef
| React.RefObject<Dialog.Root.Actions
| null>
Description

A ref to imperative actions.

  • unmount: When specified, the dialog will not be unmounted when closed. Instead, the unmount function must be called to unmount the dialog manually. Useful when the dialog’s animation is controlled by an external library.
  • close: Closes the dialog imperatively when called.
Type
React.RefObject<Dialog.Root.Actions | null> | undefined
defaultTriggerId
string | null
Description

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.

Type
string | null | undefined
disablePointerDismissal
boolean
false
Description

Determines whether the dialog should close on outside clicks.

Type
boolean | undefined
Default
false
handle
Dialog.Handle<Payload>
Name
Description

A handle to associate the dialog with a trigger. If specified, allows external triggers to control the dialog’s open state. Can be created with the Dialog.createHandle() method.

Type
Dialog.Handle<Payload> | undefined
modal
boolean | 'trap-focus'
true
Name
Description

Determines if the dialog enters a modal state when open.

  • true: user interaction is limited to just the dialog: focus is trapped, document page scroll is locked, and pointer interactions on outside elements are disabled.
  • false: user interaction with the rest of the document is allowed.
  • 'trap-focus': focus is trapped inside the dialog, but document page scroll is not locked and pointer interactions outside of it remain enabled.
Type
boolean | 'trap-focus' | undefined
Default
true
onOpenChangeComplete
function
Description

Event handler called after any animations complete when the dialog is opened or closed.

Type
triggerId
string | null
Description

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 popover is uncontrolled (that is, when the open prop is not set).

Type
string | null | undefined
children
| React.ReactNode
| PayloadChildRenderFunction<Payload>
Description

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

Additional Types

Dialog.Root.Props

Re-Export of Root props as DialogRootProps

Dialog.Root.Actions
Dialog.Root.ChangeEventReason
Dialog.Root.ChangeEventDetails

Trigger

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

handle
Dialog.Handle<Payload>
Name
Description

A handle to associate the trigger with a dialog. Can be created with the Dialog.createHandle() method.

Type
Dialog.Handle<Payload> | undefined
nativeButton
boolean
true
Description

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>).

Type
boolean | undefined
Default
true
payload
Payload
Description

A payload to pass to the dialog when it is opened.

Type
Payload | undefined
id
string
Name
Description

ID of the trigger. In addition to being forwarded to the rendered element, it is also used to specify the active trigger for the dialogs in controlled mode (with the Dialog.Root triggerId prop).

Type
string | undefined
className
string | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
style
React.CSSProperties | function
Name
Type
render
ReactElement | function
Name
Description

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.

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

Additional Types

Dialog.Trigger.Props

Re-Export of Trigger props as DialogTriggerProps

Dialog.Trigger.State

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.

container
Union
Description

A parent element to render the portal element into.

Type
className
string | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
style
React.CSSProperties | function
Name
Type
keepMounted
boolean
false
Description

Whether to keep the portal mounted in the DOM while the popup is hidden.

Type
boolean | undefined
Default
false
render
ReactElement | function
Name
Description

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.

Type

Additional Types

Dialog.Portal.Props

Re-Export of Portal props as DialogPortalProps

Dialog.Portal.State

Backdrop

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

forceRender
boolean
false
Description

Whether the backdrop is forced to render even when nested.

Type
boolean | undefined
Default
false
className
string | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
style
React.CSSProperties | function
Name
Type
render
ReactElement | function
Name
Description

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.

Type
data-open
Present when the dialog is open.
data-closed
Present when the dialog is closed.
data-starting-style
Present when the dialog is animating in.
data-ending-style
Present when the dialog is animating out.

Additional Types

Dialog.Backdrop.Props

Re-Export of Backdrop props as DialogBackdropProps

Dialog.Backdrop.State

Viewport

A positioning container for the dialog popup that can be made scrollable. Renders a <div> element.

className
string | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
style
React.CSSProperties | function
Name
Type
render
ReactElement | function
Name
Description

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.

Type
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 is animating in.
data-ending-style
Present when the dialog is animating out.

Additional Types

Dialog.Viewport.Props

Re-Export of Viewport props as DialogViewportProps

Dialog.Viewport.State

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

initialFocus
Union
Description

Determines the element to focus when the dialog is opened.

  • 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, or false/undefined to do nothing.
Type
finalFocus
Union
Description

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, or false/undefined to do nothing.
Type
className
string | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
style
React.CSSProperties | function
Name
Type
render
ReactElement | function
Name
Description

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.

Type
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 is animating in.
data-ending-style
Present when the dialog is animating out.
--nested-dialogs

Indicates how many dialogs are nested within.

Additional Types

Dialog.Popup.Props

Re-Export of Popup props as DialogPopupProps

Dialog.Popup.State

Title

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

className
string | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
style
React.CSSProperties | function
Name
Type
render
ReactElement | function
Name
Description

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.

Type

Additional Types

Dialog.Title.Props

Re-Export of Title props as DialogTitleProps

Dialog.Title.State

Description

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

className
string | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
style
React.CSSProperties | function
Name
Type
render
ReactElement | function
Name
Description

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.

Type

Additional Types

Dialog.Description.Props

Re-Export of Description props as DialogDescriptionProps

Dialog.Description.State

Close

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

nativeButton
boolean
true
Description

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>).

Type
boolean | undefined
Default
true
className
string | function
Description

CSS class applied to the element, or a function that returns a class based on the component’s state.

Type
style
React.CSSProperties | function
Name
Type
render
ReactElement | function
Name
Description

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.

Type
data-disabled
Present when the button is disabled.

Additional Types

Dialog.Close.Props

Re-Export of Close props as DialogCloseProps

Dialog.Close.State

createHandle

Creates a new handle to connect a Dialog.Root with detached Dialog.Trigger components.

Return value

Dialog.Handle<Payload>

Handle

A handle to control a Dialog imperatively and to associate detached triggers with it.

Constructor parameters

store
DialogStore<Payload>
Name
Type
DialogStore<Payload> | undefined

Properties

isOpen
boolean
readonly
Name
Description

Indicates whether the dialog is currently open.

Type
boolean
Modifiers
readonly

Methods

open(triggerId)void
Name
Description

Opens the dialog and associates it with the trigger with the given id. The trigger, if provided, must be a Dialog.Trigger component with this handle passed as a prop.

This method should only be called in an event handler or an effect (not during rendering).

Parameters
  • triggerIdstring | null

    ID of the trigger to associate with the dialog. If null, the dialog will open without a trigger association.

Returns
void
openWithPayload(payload)void
Description

Opens the dialog and sets the payload. Does not associate the dialog with any trigger.

Parameters
  • payloadPayload

    Payload to set when opening the dialog.

Returns
void
close()void
Name
Description

Closes the dialog.

Returns
void