Base UI
v1.0.0-alpha.1GitHub

Dialog

A popup that opens on top of the entire page.

API reference

Import the component and place its parts the following way:

Anatomy
import { Dialog } from '@base-ui-components/react/Dialog';

<Dialog.Root>
  <Dialog.Trigger />
  <Dialog.Backdrop />
  <Dialog.Popup>
    <Dialog.Title />
    <Dialog.Description />
    <Dialog.Close />
  </Dialog.Popup>
</Dialog.Root>;

Root

Groups all parts of the dialog. Doesn’t render its own HTML element.

PropTypeDefault
animatedbooleantrue
defaultOpenbooleanfalse
dismissiblebooleantrue
modalbooleantrue
onOpenChange(open, event) => voidundefined
openbooleanundefined

Trigger

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

PropTypeDefault
classNamestring | (state) => stringundefined
render| React.ReactElement
| (props, state) => React.ReactElement
undefined
AttributeType
[data-modal]Empty attribute
[data-popup-open]Empty attribute

Backdrop

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

PropTypeDefault
classNamestring | (state) => stringundefined
keepMountedbooleanfalse
render| React.ReactElement
| (props, state) => React.ReactElement
undefined
AttributeType
[data-open]Empty attribute
[data-closed]Empty attribute
[data-entering]Empty attribute
[data-exiting]Empty attribute

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

PropTypeDefault
classNamestring | (state) => stringundefined
containerReact.Ref | HTMLElement | nullundefined
finalFocusReact.Refundefined
initialFocus| React.Ref
| (interactionType => HTMLElement | null)
undefined
keepMountedbooleanfalse
render| React.ReactElement
| (props, state) => React.ReactElement
undefined
AttributeType
[data-open]Empty attribute
[data-closed]Empty attribute
[data-entering]Empty attribute
[data-exiting]Empty attribute
[data-modal]Empty attribute
[data-nested-dialogs]number
CSS VariableType
--nested-dialogsnumber

Title

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

PropTypeDefault
classNamestring | (state) => stringundefined
render| React.ReactElement
| (props, state) => React.ReactElement
undefined

Description

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

PropTypeDefault
classNamestring | (state) => stringundefined
render| React.ReactElement
| (props, state) => React.ReactElement
undefined

Examples

State

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

Uncontrolled dialog
<Dialog.Root>
  <Dialog.Trigger>Open</Dialog.Trigger>
  <Dialog.Popup>
    <Dialog.Title>Example dialog</Dialog.Title>
    <Dialog.Close>Close</Dialog.Close>
  </Dialog.Popup>
</Dialog.Root>

Use open and onOpenChange props if you need to access or control the state of the dialog.

Controlled dialog
const [open, setOpen] = React.useState(false);
return (
  <Dialog.Root open={open} onOpenChange={setOpen}>
    <Dialog.Trigger>Open</Dialog.Trigger>
    <Dialog.Popup>
      <form
        // Close the dialog once the form data is submitted
        onSubmit={async () => {
          await submitData();
          setOpen(false);
        }}
      >
        ...
      </form>
    </Dialog.Popup>
  </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.

Running code when dialog state changes
<Dialog.Root
  open={open}
  onOpenChange={(open) => {
    // Do stuff when the dialog is closed
    if (!open) {
      doStuff();
    }
    // Set the new state
    setOpen(open);
  }}
>