Dialog

A modal overlay that interrupts the user with important content.

Import

import { Dialog } from '@matix/components/dialog';

Usage

Controlled

Use open and onOpenChange to control the dialog programmatically.

function ControlledDialog() {
  const [open, setOpen] = React.useState(false);

  return (
    <>
      <Button onClick={() => setOpen(true)}>Open</Button>
      <Dialog.Root open={open} onOpenChange={setOpen}>
        <Dialog.Portal>
          <Dialog.Backdrop />
          <Dialog.Popup>
            <Dialog.Title>Controlled Dialog</Dialog.Title>
            <Dialog.Description>This dialog is controlled by external state.</Dialog.Description>
            <Dialog.Close render={<Button />}>Done</Dialog.Close>
          </Dialog.Popup>
        </Dialog.Portal>
      </Dialog.Root>
    </>
  );
}

With Form Content

Dialogs work well for collecting user input via forms. A Select inside the dialog opens above it.

Close Buttons

Dialog.Close has two forms:

  • Corner X. Without render, Dialog.Close is a small icon button in the top-right corner of the popup. With no children, it shows an X icon and has the label "Close dialog". To use a different icon, pass it as children and add an aria-label.
  • Footer button. With render, Dialog.Close applies no styles of its own. The rendered element sets the look and the position.
<Dialog.Close />
<Dialog.Close render={<Button variant="outline" />}>Cancel</Dialog.Close>

Both forms close the dialog when you click them.

Animations

Dialog uses data-open attribute for enter/exit animations. Animations are enabled by default and respect prefers-reduced-motion. You can disable animations globally via MatixProvider.

<MatixProvider animate={false}>
  <Dialog.Root>...</Dialog.Root>
</MatixProvider>

Non-Modal

Set modal={false} to allow interaction with the rest of the page while the dialog is open.

<Dialog.Root modal={false}>
  <Dialog.Trigger>Open Non-Modal</Dialog.Trigger>
  <Dialog.Portal>
    <Dialog.Popup>
      <Dialog.Title>Non-Modal Dialog</Dialog.Title>
      <Dialog.Description>
        You can still interact with the page behind this dialog.
      </Dialog.Description>
      <Dialog.Close />
    </Dialog.Popup>
  </Dialog.Portal>
</Dialog.Root>

Custom Styles

Use the sx prop on any sub-component to override styles.

import * as stylex from '@stylexjs/stylex';

const custom = stylex.create({
  popup: { maxWidth: '480px', borderRadius: '16px' },
  title: { fontSize: '1.5rem' },
});

<Dialog.Root>
  <Dialog.Trigger>Styled Dialog</Dialog.Trigger>
  <Dialog.Portal>
    <Dialog.Backdrop />
    <Dialog.Popup sx={custom.popup}>
      <Dialog.Title sx={custom.title}>Custom Styled</Dialog.Title>
      <Dialog.Description>This dialog uses custom StyleX overrides.</Dialog.Description>
      <Dialog.Close />
    </Dialog.Popup>
  </Dialog.Portal>
</Dialog.Root>;

Backdrop Blur

colors.overlay sets the backdrop color. To blur the page behind the backdrop, set the --mtx-overlay-backdrop-filter custom property. The Dialog, AlertDialog, and Drawer backdrops read it. The default is none.

:root {
  --mtx-overlay-backdrop-filter: blur(4px);
}

Stacking

The overlays use the zIndex scale from @matix/tokens/z-index.stylex. Backdrops use overlay (1300) and modal popups use modal (1400). Select, Menu, Popover, Combobox, NavigationMenu, and DatePicker popups use popover (1500), and tooltips use tooltip (1600). Thus a popup that opens from inside a modal shows above it.

Server Components

Dialog is a client component. A Server Component cannot read a property of a client component, so Dialog.Root is undefined there. In a Server Component, import the named parts. Each Dialog.<Part> is also exported as Dialog<Part>.

import {
  DialogRoot,
  DialogTrigger,
  DialogPortal,
  DialogBackdrop,
  DialogPopup,
  DialogTitle,
  DialogDescription,
  DialogClose,
} from '@matix/components/dialog';

In a client component, both forms work.

API Reference

ComponentPropTypeDefaultDescription
Dialog.Rootopenboolean—Controlled open state
Dialog.RootonOpenChange(open: boolean) => void—Called when the open state changes
Dialog.RootmodalbooleantrueWhether the dialog is modal
Dialog.TriggersxStyleXStyles—StyleX style override
Dialog.PortalcontainerHTMLElementdocument.bodyPortal target
Dialog.BackdropsxStyleXStyles—StyleX style override
Dialog.PopupsxStyleXStyles—StyleX style override
Dialog.TitlesxStyleXStyles—StyleX style override
Dialog.DescriptionsxStyleXStyles—StyleX style override
Dialog.CloserenderReactElement—Render this element without the corner X styles
Dialog.ClosesxStyleXStyles—StyleX style override

All sub-components forward additional props to their underlying Base UI elements.

Accessibility

  • Built on Base UI Dialog -- full WAI-ARIA dialog pattern.
  • Modal dialogs trap focus within the popup and return focus to the trigger on close.
  • Backdrop click and Escape close the dialog by default.
  • Dialog.Title maps to aria-labelledby and Dialog.Description to aria-describedby on the popup.

Keyboard

KeyAction
EscapeCloses the dialog
TabMoves focus to the next focusable element inside the dialog
Shift + TabMoves focus to the previous focusable element inside the dialog