Jone

Dialog

A high-quality, unstyled React dialog component that opens on top of the entire page.

A popup that opens on top of the entire page.

Usage guidelines

  • Dialog doesn't support gestures: Use Drawer when you need gesture support or snap points. A panel that slides in from the edge of the screen and doesn't need gesture support is a positioned Dialog.

Anatomy

Import the component and assemble its parts:

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

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

Examples

State

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

Uncontrolled dialog
<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.

Controlled dialog
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.

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);
  }}
>

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
import * as React from 'react';
import { Dialog } from '@base-ui/react/dialog';
import { Menu } from '@base-ui/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>
            {/* prettier-ignore */}
            {/* 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.

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
const demoDialog = Dialog.createHandle();

<Dialog.Trigger handle={demoDialog}>Open</Dialog.Trigger>

<Dialog.Root handle={demoDialog}>
  ...
</Dialog.Root>

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
<Dialog.Root>
  <Dialog.Trigger>Trigger 1</Dialog.Trigger>
  <Dialog.Trigger>Trigger 2</Dialog.Trigger>
  ...
</Dialog.Root>
Multiple detached triggers
const demoDialog = Dialog.createHandle();

<Dialog.Trigger handle={demoDialog}>Trigger 1</Dialog.Trigger>
<Dialog.Trigger handle={demoDialog}>Trigger 2</Dialog.Trigger>
<Dialog.Root handle={demoDialog}>
  ...
</Dialog.Root>

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
const demoDialog = Dialog.createHandle<{ text: string }>();

<Dialog.Trigger handle={demoDialog} payload={{ text: 'Trigger 1' }}>
  Trigger 1
</Dialog.Trigger>

<Dialog.Trigger handle={demoDialog} payload={{ text: 'Trigger 2' }}>
  Trigger 2
</Dialog.Trigger>

<Dialog.Root handle={demoDialog}>
  {({ payload }) => (
    <Dialog.Portal>
      <Dialog.Popup>
        <Dialog.Title>Dialog</Dialog.Title>
        {payload !== undefined && (
          <Dialog.Description>
            This has been opened by {payload.text}
          </Dialog.Description>
        )}
      </Dialog.Popup>
    </Dialog.Portal>
  )}
</Dialog.Root>

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

NameTypeDefaultDescription
openOptionalboolean | undefinedWhether the dialog is currently open.
defaultOpenOptionalboolean | undefinedfalseWhether the dialog is initially open. To render a controlled dialog, use the `open` prop instead.
modalOptionalboolean | 'trap-focus' | undefinedtrueDetermines 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.
onOpenChangeOptional((open: boolean, eventDetails: DialogRoot.ChangeEventDetails) => void) | undefinedEvent handler called when the dialog is opened or closed.
onOpenChangeCompleteOptional((open: boolean) => void) | undefinedEvent handler called after any animations complete when the dialog is opened or closed.
disablePointerDismissalOptionalboolean | undefinedfalseDetermines whether the dialog should close on outside clicks.
actionsRefOptionalReact.RefObject<DialogRoot.Actions | null> | undefinedA 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.
handleOptionalDialogHandle<Payload> | undefinedA 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.
childrenOptionalReact.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.
triggerIdOptionalstring | null | undefinedID 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 (i.e. when the `open` prop is not set).
defaultTriggerIdOptionalstring | null | undefinedID of the trigger that the dialog is associated with. This is useful in conjunction with the `defaultOpen` prop to create an initially open dialog.

Trigger

NameTypeDefaultDescription
handleOptionalDialogHandle<Payload> | undefinedA handle to associate the trigger with a dialog. Can be created with the Dialog.createHandle() method.
payloadOptionalPayload | undefinedA payload to pass to the dialog when it is opened.
idOptionalstring | undefinedID 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 DialogRoot `triggerId` prop).

Portal

NameTypeDefaultDescription
keepMountedOptionalboolean | undefinedfalseWhether to keep the portal mounted in the DOM while the popup is hidden.
containerOptionalFloatingPortal.Props<DialogPortalState>['container'] | undefinedA parent element to render the portal element into.

Backdrop

NameTypeDefaultDescription
forceRenderOptionalboolean | undefinedfalseWhether the backdrop is forced to render even when nested.

On this page