- Supports modal and non-modal modes
- Focus is trapped and scrolling is blocked in modal mode
- Screen reader announcements via rendered title and description
- Pressing
Escapecloses the dialog - Supports controlled and uncontrolled open state
- Supports nested dialogs with automatic visual hierarchy
Installation
Usage
Import the dialog package and connect the machine to your framework:machine— Behavior logic.connect— Maps behavior to JSX props and event handlers.
- React
- Vue
Managing focus
When the dialog opens, it focuses the first focusable element and keeps keyboard focus inside. When it closes, focus returns to the trigger. To control which element receives focus on open, passinitialFocusEl:
finalFocusEl:
Non-modal dialog
Setmodal to false to allow interaction with content behind the dialog. Focus is not trapped and scrolling is not blocked.
Closing on outside interaction
By default, the dialog closes when you click its backdrop. SetcloseOnInteractOutside to false to prevent this.
event.preventDefault() in onInteractOutside:
Controlled dialog
Useopen and onOpenChange to control the dialog’s open state externally.
Preventing scroll
By default, scrolling on thebody is blocked when the dialog is open. Set preventScroll to false to disable this.
Alert dialog
Setrole to "alertdialog" for dialogs that require an immediate user response.
Alert dialogs should contain two or more action buttons. Use
initialFocusEl to set focus to the least destructive action.Disabling Escape key
SetcloseOnEscape to false if the dialog should not close when Escape is pressed.
Labeling without a visible title
If you do not render a title element, provide anaria-label instead.
API Reference
The controlled open state of the dialog.
The initial open state when rendered. Use when you don’t need to control the open state.
Whether to prevent pointer interaction outside the dialog and hide all content below it.
The ARIA role for the dialog. Use
"alertdialog" for urgent, confirmation-required dialogs.Whether to trap keyboard focus inside the dialog when open.
Whether to prevent scrolling behind the dialog when open.
Whether to close the dialog when clicking outside its content.
Whether to close the dialog when the
Escape key is pressed.Element to receive focus when the dialog opens.
Element to receive focus when the dialog closes.
Whether to restore focus to the element that had focus before the dialog opened.
Accessible label for the dialog when no visible title is rendered.
Callback invoked when the dialog’s open state changes.
Styling
Each part includes adata-part attribute you can target in CSS.
Parts
| Part | Description |
|---|---|
trigger | The button that opens the dialog |
backdrop | The backdrop/overlay behind the dialog |
positioner | Centers or positions the dialog content |
content | The dialog panel |
title | The dialog title |
description | The dialog description |
close-trigger | The close button inside the dialog |
Open and closed state
Nested dialogs
When dialogs are nested, the layer stack automatically applies data attributes for visual hierarchy:| Attribute | Description |
|---|---|
data-nested | Applied to dialogs that are nested inside another dialog |
data-has-nested | Applied to dialogs that have a nested dialog open |
--nested-layer-count | CSS variable with the count of nested dialogs |
Accessibility
The dialog follows the WAI-ARIA Dialog pattern and Alert Dialog pattern.- The content element has
role="dialog"(orrole="alertdialog") witharia-modal="true". aria-labelledbypoints to the title element, andaria-describedbypoints to the description element.- Focus is trapped within the dialog in modal mode.
Keyboard interactions
| Key | Description |
|---|---|
Escape | Closes the dialog (unless closeOnEscape is false). |
Tab | Moves focus to the next focusable element within the dialog. |
Shift + Tab | Moves focus to the previous focusable element within the dialog. |