Skip to content

Dialog ​

Modal dialogs and alerts with focus trapping and accessible keyboard interactions.

Features ​

  • ✅ Focus trap when dialog is open
  • ✅ Escape key to close
  • ✅ Focus returns to trigger on close
  • ✅ Backdrop click to close
  • ✅ Accessible ARIA attributes
  • ✅ Prevent body scroll when open
  • ✅ Support for programmatic control via events
  • ✅ Support for nested dialogs

Basic Usage ​

Dialog Title

This is a basic dialog example. Click outside or press Escape to close.

Confirmation Dialog ​

Confirm Deletion

Are you sure you want to delete this item? This action cannot be undone.

Programmatic Control ​

You can control dialogs programmatically using custom events with the name prop.

Programmatic Dialog

This dialog was opened using a custom event.

With Transitions ​

Animated Dialog

This dialog smoothly fades and scales in.

Full Screen Modal ​

Full Screen Dialog

This is a full screen modal dialog. Perfect for complex forms, image galleries, or detailed content.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Drawer/Flyout Modal ​

Drawer Menu

This drawer slides in from the right side of the screen.

API Reference ​

Component Props ​

PropTypeDefaultDescription
namestring-Unique identifier for programmatic control
openbooleanfalseControlled open state
modalbooleantrueEnable modal behavior (focus trap, scroll lock)
closeOnInteractOutsidebooleantrueClose when clicking backdrop
closeOnEscapebooleantrueClose when pressing Escape key
trapFocusbooleantrueTrap focus within dialog
preventScrollbooleantruePrevent body scroll when open
role'dialog' | 'alertdialog''dialog'ARIA role for the dialog

Parts ​

PartDescription
x-dialogRoot container element
x-dialog:triggerButton that opens the dialog
x-dialog:backdropBackdrop/overlay element
x-dialog:positionerPositions the dialog content
x-dialog:contentDialog content container
x-dialog:titleDialog title (for aria-labelledby)
x-dialog:descriptionDialog description (for aria-describedby)
x-dialog:close-triggerClose button

Data Attributes ​

AttributeDescription
data-scopeAlways set to dialog
data-partIdentifies the part (root, trigger, backdrop, etc.)
data-stateCurrent state (open or closed) on root

Events ​

Dispatched by component:

EventDetailDescription
open{ open: true }Fired when dialog opens
close{ open: false }Fired when dialog closes

Listened for (window events):

EventDetailDescription
modal:openstring (dialog name)Opens dialog matching the name
modal:closestring (dialog name)Closes dialog matching the name

Accessibility ​

Keyboard Interactions ​

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

Focus Management ​

  • When opened, focus moves to the first focusable element in the dialog
  • Focus is trapped within the dialog while open (in modal mode)
  • When closed, focus returns to the trigger button
  • Focusable elements include: links, buttons, inputs, selects, textareas, and elements with tabindex >= 0

ARIA Attributes ​

The dialog automatically includes:

  • role="dialog" (or role="alertdialog") on the content
  • aria-modal="true" on the content (when modal)
  • aria-labelledby pointing to the title
  • aria-describedby pointing to the description
  • aria-expanded on the trigger
  • aria-haspopup="dialog" on the trigger

Tips ​

  • Modal vs Non-modal: Use modal: true for dialogs that require user attention before continuing. Use modal: false for informational dialogs that don't block interaction.
  • Alert Dialog: Use role: 'alertdialog' for dialogs that require immediate user response (like confirmations or warnings).
  • Programmatic Control: Use the name prop with window events to open/close dialogs from anywhere in your application.
  • Transitions: Combine with Alpine's x-transition directives for smooth animations.
  • Nested Dialogs: The component supports multiple dialogs with proper focus and scroll management.

Built with Alpine.js and inspired by Zag.js