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
renderor with children,AlertDialog.Closeapplies no styles of its own. Userender={<Button />}for the buttons inAlertDialog.Actions. - Corner X. With no
renderand no children,AlertDialog.Closeis 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
| Component | Prop | Type | Default | Description |
|---|---|---|---|---|
AlertDialog.Root | open | boolean | — | Controlled open state |
AlertDialog.Root | onOpenChange | (open: boolean) => void | — | Called when the open state changes |
AlertDialog.Trigger | — | — | — | Renders a button that opens the alert dialog |
AlertDialog.Portal | container | HTMLElement | document.body | Portal target |
AlertDialog.Backdrop | sx | StyleXStyles | — | StyleX style override |
AlertDialog.Popup | sx | StyleXStyles | — | StyleX style override |
AlertDialog.Title | sx | StyleXStyles | — | StyleX style override |
AlertDialog.Description | sx | StyleXStyles | — | StyleX style override |
AlertDialog.Close | render | ReactElement | — | Render this element without the corner X styles |
AlertDialog.Close | sx | StyleXStyles | — | StyleX style override |
AlertDialog.Actions | sx | StyleXStyles | — | 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-ARIAalertdialogpattern. - Unlike
Dialog, the alert dialog cannot be dismissed by clicking the backdrop or pressingEscape. The user must interact with one of the action buttons. - Focus is trapped inside the popup and returned to the trigger on close.
AlertDialog.Titlemaps toaria-labelledbyandAlertDialog.Descriptiontoaria-describedbyon the popup.
Keyboard
| Key | Action |
|---|---|
Tab | Moves focus to the next focusable element inside the alert dialog |
Shift + Tab | Moves focus to the previous focusable element inside the alert dialog |