Skip to content

Dialog

A dialog creates a window overlaid on top of a page window, rendering the content underneath inert.

Demo

dialog.html
<div x-init x-dialog>
<button x-dialog:trigger>Open Dialog</button>
<template x-teleport="body">
<div x-dialog:content>
<div x-dialog:overlay></div>
<div>
<h2 x-dialog:title>Dialog</h2>
<p x-dialog:description>
The dialog is commonly used for alerts, menus, and interactive
prompts.
</p>
<button x-dialog:close>Close</button>
</div>
</div>
</template>
</div>

Features

  • Focus is automatically trapped in a modal
  • Manages screen reader announcements with dialog title and description
  • Clicking on overlay closes the modal
  • Esc key closes the modal

Anatomy

<div x-init x-dialog>
<button x-dialog:trigger></button>
<template x-teleport="body">
<div x-dialog:content>
<div x-dialog:overlay></div>
<h2 x-dialog:title></h2>
<p x-dialog:description></p>
<button x-dialog:close></button>
</div>
</template>
</div>

Directives

x-dialog

Required

The root element for the dialog.

EventDescriptiondetail
dialog:changeWhether the dialog is open or closed.{ open: boolean }
Data AttributeValues
[data-state]open | closed

x-dialog:trigger

Required

Trigger button that opens or closes the dialog content.

EventDescriptiondetail
dialog:changeWhether the dialog is open or closed.{ open: boolean }
Data AttributeValues
[data-state]open | closed

x-dialog:content

Required

The content that will be shown or hidden.

EventDescriptiondetail
dialog:changeWhether the dialog is open or closed.{ open: boolean }
Data AttributeValues
[data-state]open | closed

x-dialog:overlay


Overlay for the page content. Closes the dialog when clicked on.

x-dialog:title


Dialog title announced to screen readers when opened.

x-dialog:description


Dialog description announced to screen readers when opened.

x-dialog:close

Required

Button to close the dialog when opened.