Dialog
A popup that opens on top of the entire page.
View as MarkdownAnatomy
Import the component and assemble its parts:
import { Dialog } from '@base-ui-components/react/dialog';
<Dialog.Root>
<Dialog.Trigger />
<Dialog.Portal>
<Dialog.Backdrop />
<Dialog.Popup>
<Dialog.Title />
<Dialog.Description />
<Dialog.Close />
</Dialog.Popup>
</Dialog.Portal>
</Dialog.Root>API reference
Root
Groups all parts of the dialog. Doesn’t render its own HTML element.
defaultOpenboolean
false
defaultOpenboolean
false
- Name
- Description
Whether the dialog is initially open.
To render a controlled dialog, use the
openprop instead.- Type
boolean | undefined- Default
false
openboolean
—
openboolean
—- Name
- Description
Whether the dialog is currently open.
- Type
boolean | undefined
onOpenChangefunction
—
onOpenChangefunction
—- Name
- Description
Event handler called when the dialog is opened or closed.
- Type
| (( open: boolean, eventDetails: Dialog.Root.ChangeEventDetails, ) => void) | undefined
actionsRefRefObject<Dialog.Root.Actions>
—
actionsRefRefObject<Dialog.Root.Actions>
—- Name
- Description
A ref to imperative actions.
unmount: When specified, the dialog will not be unmounted when closed. Instead, theunmountfunction must be called to unmount the dialog manually. Useful when the dialog's animation is controlled by an external library.
- Type
React.RefObject<Dialog.Root.Actions> | undefined
dismissibleboolean
true
dismissibleboolean
true
- Name
- Description
Determines whether the dialog should close on outside clicks.
- Type
boolean | undefined- Default
true
modalboolean | 'trap-focus'
true
modalboolean | '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
onOpenChangeCompletefunction
—
onOpenChangeCompletefunction
—- Description
Event handler called after any animations complete when the dialog is opened or closed.
- Type
((open: boolean) => void) | undefined
childrenReactNode
—
childrenReactNode
—- Name
- Type
React.ReactNode
Trigger
A button that opens the dialog.
Renders a <button> element.
nativeButtonboolean
true
nativeButtonboolean
true
- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set tofalseif the rendered element is not a button (e.g.<div>).- Type
boolean | undefined- Default
true
classNamestring | function
—
classNamestring | function
—- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
string | ((state: Dialog.Trigger.State) => string)
renderReactElement | function
—
renderReactElement | function
—- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Dialog.Trigger.State, ) => ReactElement)
data-popup-open
Present when the corresponding 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.
By default, the portal element is appended to <body>.
Renders a <div> element.
containerUnion
—
containerUnion
—- Name
- Description
A parent element to render the portal element into.
- Type
| HTMLElement | ShadowRoot | React.RefObject<HTMLElement | ShadowRoot | null> | null | undefined
classNamestring | function
—
classNamestring | function
—- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
string | ((state: Dialog.Portal.State) => string)
keepMountedboolean
false
keepMountedboolean
false
- Name
- Description
Whether to keep the portal mounted in the DOM while the popup is hidden.
- Type
boolean | undefined- Default
false
renderReactElement | function
—
renderReactElement | function
—- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Dialog.Portal.State, ) => ReactElement)
Backdrop
An overlay displayed beneath the popup.
Renders a <div> element.
forceRenderboolean
false
forceRenderboolean
false
- Name
- Description
Whether the backdrop is forced to render even when nested.
- Type
boolean | undefined- Default
false
classNamestring | function
—
classNamestring | function
—- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
string | ((state: Dialog.Backdrop.State) => string)
renderReactElement | function
—
renderReactElement | function
—- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Dialog.Backdrop.State, ) => ReactElement)
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.
Popup
A container for the dialog contents.
Renders a <div> element.
initialFocusUnion
—
initialFocusUnion
—- Name
- 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, orkeyboard). Return an element to focus,trueto use the default behavior, orfalse/undefinedto do nothing.
- Type
| boolean | React.RefObject<HTMLElement | null> | (( openType: InteractionType, ) => boolean | void | HTMLElement | null) | undefined
finalFocusUnion
—
finalFocusUnion
—- Name
- 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, orkeyboard). Return an element to focus,trueto use the default behavior, orfalse/undefinedto do nothing.
- Type
| boolean | React.RefObject<HTMLElement | null> | (( closeType: InteractionType, ) => boolean | void | HTMLElement | null) | undefined
classNamestring | function
—
classNamestring | function
—- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
string | ((state: Dialog.Popup.State) => string)
renderReactElement | function
—
renderReactElement | function
—- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Dialog.Popup.State, ) => ReactElement)
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.
Title
A heading that labels the dialog.
Renders an <h2> element.
classNamestring | function
—
classNamestring | function
—- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
string | ((state: Dialog.Title.State) => string)
renderReactElement | function
—
renderReactElement | function
—- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Dialog.Title.State, ) => ReactElement)
Description
A paragraph with additional information about the dialog.
Renders a <p> element.
classNamestring | function
—
classNamestring | function
—- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Dialog.Description.State) => string)
renderReactElement | function
—
renderReactElement | function
—- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Dialog.Description.State, ) => ReactElement)
Close
A button that closes the dialog.
Renders a <button> element.
nativeButtonboolean
true
nativeButtonboolean
true
- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set tofalseif the rendered element is not a button (e.g.<div>).- Type
boolean | undefined- Default
true
classNamestring | function
—
classNamestring | function
—- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
string | ((state: Dialog.Close.State) => string)
renderReactElement | function
—
renderReactElement | function
—- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Dialog.Close.State, ) => ReactElement)
data-disabled
Present when the button is disabled.
Examples
State
By default, Dialog is an uncontrolled component that manages its own state.
<Dialog.Root>
<Dialog.Trigger>Open</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Popup>
<Dialog.Title>Example dialog</Dialog.Title>
<Dialog.Close>Close</Dialog.Close>
</Dialog.Popup>
</Dialog.Portal>
</Dialog.Root>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.
const [open, setOpen] = React.useState(false);
return (
<Dialog.Root open={open} onOpenChange={setOpen}>
<Dialog.Trigger>Open</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Popup>
<form
// Close the dialog once the form data is submitted
onSubmit={async () => {
await submitData();
setOpen(false);
}}
>
...
</form>
</Dialog.Popup>
</Dialog.Portal>
</Dialog.Root>
);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.
<Dialog.Root
open={open}
onOpenChange={(open) => {
// Do stuff when the dialog is closed
if (!open) {
doStuff();
}
// Set the new state
setOpen(open);
}}
>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.
import * as React from 'react';
import { Dialog } from '@base-ui-components/react/dialog';
import { Menu } from '@base-ui-components/react/menu';
function ExampleMenu() {
const [dialogOpen, setDialogOpen] = React.useState(false);
return (
<React.Fragment>
<Menu.Root>
<Menu.Trigger>Open menu</Menu.Trigger>
<Menu.Portal>
<Menu.Positioner>
<Menu.Popup>
{/* Open the dialog when the menu item is clicked */}
<Menu.Item onClick={() => setDialogOpen(true)}>Open dialog</Menu.Item>
</Menu.Popup>
</Menu.Positioner>
</Menu.Portal>
</Menu.Root>
{/* Control the dialog state */}
<Dialog.Root open={dialogOpen} onOpenChange={setDialogOpen}>
<Dialog.Portal>
<Dialog.Backdrop />
<Dialog.Popup>
{/* Rest of the dialog */}
</Dialog.Popup>
</Dialog.Portal>
</Dialog.Root>
</React.Fragment>
);
}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.