Alert Dialog

A modal dialog for confirmations and destructive actions that requires an explicit response.

Import

import { AlertDialog } from '@matix/components/alert-dialog';

Usage

Destructive Action

Use Button variant="destructive" to signal a dangerous operation. Pass the Button to AlertDialog.Close through render, so that the page has one button and not a button inside a button.

<AlertDialog.Root>
  <AlertDialog.Trigger render={<Button variant="destructive" />}>
    Delete Account
  </AlertDialog.Trigger>
  <AlertDialog.Portal>
    <AlertDialog.Backdrop />
    <AlertDialog.Popup>
      <AlertDialog.Title>Delete your account?</AlertDialog.Title>
      <AlertDialog.Description>
        All your data will be permanently removed. This cannot be reversed.
      </AlertDialog.Description>
      <AlertDialog.Actions>
        <AlertDialog.Close render={<Button variant="outline" />}>Cancel</AlertDialog.Close>
        <AlertDialog.Close render={<Button variant="destructive" />}>
          Delete Account
        </AlertDialog.Close>
      </AlertDialog.Actions>
    </AlertDialog.Popup>
  </AlertDialog.Portal>
</AlertDialog.Root>

Controlled

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

  return (
    <>
      <Button onClick={() => setOpen(true)}>Show Alert</Button>
      <AlertDialog.Root open={open} onOpenChange={setOpen}>
        <AlertDialog.Portal>
          <AlertDialog.Backdrop />
          <AlertDialog.Popup>
            <AlertDialog.Title>Unsaved Changes</AlertDialog.Title>
            <AlertDialog.Description>
              You have unsaved changes. Discard them?
            </AlertDialog.Description>
            <AlertDialog.Actions>
              <Button variant="outline" onClick={() => setOpen(false)}>
                Keep Editing
              </Button>
              <Button variant="solid" onClick={() => setOpen(false)}>
                Discard
              </Button>
            </AlertDialog.Actions>
          </AlertDialog.Popup>
        </AlertDialog.Portal>
      </AlertDialog.Root>
    </>
  );
}

Close Buttons

AlertDialog.Close has two forms:

  • Action button. With render or with children, AlertDialog.Close applies no styles of its own. Use render={<Button />} for the buttons in AlertDialog.Actions.
  • Corner X. With no render and no children, AlertDialog.Close is a small X icon button in the top-right corner of the popup, with the label "Close dialog".
<AlertDialog.Close render={<Button variant="outline" />}>Cancel</AlertDialog.Close>
<AlertDialog.Close />

AlertDialog.Close with children and no render is an unstyled native button. This is different from Dialog.Close, which keeps the corner X styles until you pass render.

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: '400px' },
  actions: { gap: '16px' },
});

<AlertDialog.Root>
  <AlertDialog.Trigger render={<Button variant="outline" />}>Styled Alert</AlertDialog.Trigger>
  <AlertDialog.Portal>
    <AlertDialog.Backdrop />
    <AlertDialog.Popup sx={custom.popup}>
      <AlertDialog.Title>Confirm</AlertDialog.Title>
      <AlertDialog.Description>Proceed with this action?</AlertDialog.Description>
      <AlertDialog.Actions sx={custom.actions}>
        <AlertDialog.Close render={<Button variant="outline" />}>Cancel</AlertDialog.Close>
        <AlertDialog.Close render={<Button />}>OK</AlertDialog.Close>
      </AlertDialog.Actions>
    </AlertDialog.Popup>
  </AlertDialog.Portal>
</AlertDialog.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

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

import {
  AlertDialogRoot,
  AlertDialogTrigger,
  AlertDialogPortal,
  AlertDialogBackdrop,
  AlertDialogPopup,
  AlertDialogTitle,
  AlertDialogDescription,
  AlertDialogClose,
  AlertDialogActions,
} from '@matix/components/alert-dialog';

In a client component, both forms work.

API Reference

ComponentPropTypeDefaultDescription
AlertDialog.Rootopenboolean—Controlled open state
AlertDialog.RootonOpenChange(open: boolean) => void—Called when the open state changes
AlertDialog.Trigger———Renders a button that opens the alert dialog
AlertDialog.PortalcontainerHTMLElementdocument.bodyPortal target
AlertDialog.BackdropsxStyleXStyles—StyleX style override
AlertDialog.PopupsxStyleXStyles—StyleX style override
AlertDialog.TitlesxStyleXStyles—StyleX style override
AlertDialog.DescriptionsxStyleXStyles—StyleX style override
AlertDialog.CloserenderReactElement—Render this element without the corner X styles
AlertDialog.ClosesxStyleXStyles—StyleX style override
AlertDialog.ActionssxStyleXStyles—StyleX style override for the actions container

All sub-components forward additional props to their underlying Base UI elements. AlertDialog.Actions renders a <div> with flex layout for button alignment.

Accessibility

  • Built on Base UI AlertDialog -- full WAI-ARIA alertdialog pattern.
  • Unlike Dialog, the alert dialog cannot be dismissed by clicking the backdrop or pressing Escape. The user must interact with one of the action buttons.
  • Focus is trapped inside the popup and returned to the trigger on close.
  • AlertDialog.Title maps to aria-labelledby and AlertDialog.Description to aria-describedby on the popup.

Keyboard

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