Dialogs
<button type="button" data-dialog-open="demo-dialog-edit-profile" class="btn" data-variant="outline">Open Dialog</button>
<dialog id="demo-dialog-edit-profile" class="dialog" aria-labelledby="demo-dialog-edit-profile-title" aria-describedby="demo-dialog-edit-profile-description" data-size="sm">
<div>
<header>
<h2 id="demo-dialog-edit-profile-title">Edit profile</h2>
<p id="demo-dialog-edit-profile-description">Make changes to your profile here. Click save when you're done.</p>
</header>
<section>
<form class="grid gap-4">
<div class="grid gap-3">
<label class="label" for="demo-dialog-edit-profile-name">Name</label>
<input class="input" type="text" value="Pedro Duarte" id="demo-dialog-edit-profile-name" autofocus />
</div>
<div class="grid gap-3">
<label class="label" for="demo-dialog-edit-profile-username">Username</label>
<input class="input" type="text" value="@peduarte" id="demo-dialog-edit-profile-username" />
</div>
</form>
</section>
<footer>
<button class="btn" data-variant="outline" data-dialog-close>Cancel</button>
<button class="btn" data-dialog-close>Save changes</button>
</footer>
<button type="button" class="btn" data-variant="ghost" data-size="icon-sm" aria-label="Close dialog" data-dialog-close><i class="icon icon-x" aria-hidden="true"></i></button>
</div>
</dialog>
The markup is a dialog element with one child
The component is the browser's own dialog element. The panel holds a header, a section, a footer and a close button.
<dialog class="dialog" id="edit-profile" aria-labelledby="edit-profile-title">
<div>
<header>
<h2 id="edit-profile-title">Edit profile</h2>
<p>Make changes to your profile here.</p>
</header>
<section>Content</section>
<footer>
<button class="btn" data-variant="outline">Cancel</button>
<button class="btn">Save changes</button>
</footer>
<button type="button" class="btn" data-variant="ghost" data-size="icon-sm" aria-label="Close dialog"><i class="icon icon-x" aria-hidden="true"></i></button>
</div>
</dialog>
Buttons open and close it with data-dialog-open and data-dialog-close
data-dialog-open names the dialog's id and opens it with showModal(). data-dialog-close closes the dialog the button is in. Escape closes it too.
<button type="button" class="btn" data-dialog-open="edit-profile">Edit profile</button>
<button type="button" class="btn" data-dialog-close>Cancel</button>
A click on the dimmed area closes the dialog. data-dialog-static on the dialog keeps it open.
<button type="button" class="btn" data-variant="outline" data-dialog-open="demo-dialog-outside">Open Dialog</button>
<dialog id="demo-dialog-outside" class="dialog" data-size="sm" aria-labelledby="demo-dialog-outside-title">
<div>
<header>
<h2 id="demo-dialog-outside-title">Order notes</h2>
<p>Click outside this panel to close it.</p>
</header>
<footer>
<button class="btn" data-variant="outline" data-dialog-close>Close</button>
</footer>
</div>
</dialog>
Sizes: small, default, large and extra large
<div class="flex flex-wrap gap-2">
<button type="button" data-dialog-open="demo-dialog-size-sm" class="btn" data-variant="outline">Small</button>
<button type="button" data-dialog-open="demo-dialog-size-default" class="btn" data-variant="outline">Default</button>
<button type="button" data-dialog-open="demo-dialog-size-lg" class="btn" data-variant="outline">Large</button>
<button type="button" data-dialog-open="demo-dialog-size-xl" class="btn" data-variant="outline">Extra large</button>
</div>
<dialog id="demo-dialog-size-sm" class="dialog" data-size="sm" aria-labelledby="demo-dialog-size-sm-title" aria-describedby="demo-dialog-size-sm-description">
<div>
<header>
<h2 id="demo-dialog-size-sm-title">Small</h2>
<p id="demo-dialog-size-sm-description">data-size="sm": 24rem wide. A short confirmation or one field.</p>
</header>
<footer>
<button class="btn" data-variant="outline" data-dialog-close>Close</button>
</footer>
<button type="button" class="btn" data-variant="ghost" data-size="icon-sm" aria-label="Close dialog" data-dialog-close><i class="icon icon-x" aria-hidden="true"></i></button>
</div>
</dialog>
<dialog id="demo-dialog-size-default" class="dialog" data-size="" aria-labelledby="demo-dialog-size-default-title" aria-describedby="demo-dialog-size-default-description">
<div>
<header>
<h2 id="demo-dialog-size-default-title">Default</h2>
<p id="demo-dialog-size-default-description">No data-size: 37.5rem (600px) wide. A form.</p>
</header>
<footer>
<button class="btn" data-variant="outline" data-dialog-close>Close</button>
</footer>
<button type="button" class="btn" data-variant="ghost" data-size="icon-sm" aria-label="Close dialog" data-dialog-close><i class="icon icon-x" aria-hidden="true"></i></button>
</div>
</dialog>
<dialog id="demo-dialog-size-lg" class="dialog" data-size="lg" aria-labelledby="demo-dialog-size-lg-title" aria-describedby="demo-dialog-size-lg-description">
<div>
<header>
<h2 id="demo-dialog-size-lg-title">Large</h2>
<p id="demo-dialog-size-lg-description">data-size="lg": 42rem wide. A form with rows of fields, such as an address.</p>
</header>
<footer>
<button class="btn" data-variant="outline" data-dialog-close>Close</button>
</footer>
<button type="button" class="btn" data-variant="ghost" data-size="icon-sm" aria-label="Close dialog" data-dialog-close><i class="icon icon-x" aria-hidden="true"></i></button>
</div>
</dialog>
<dialog id="demo-dialog-size-xl" class="dialog" data-size="xl" aria-labelledby="demo-dialog-size-xl-title" aria-describedby="demo-dialog-size-xl-description">
<div>
<header>
<h2 id="demo-dialog-size-xl-title">Extra large</h2>
<p id="demo-dialog-size-xl-description">data-size="xl": 56rem wide. A table or a preview.</p>
</header>
<footer>
<button class="btn" data-variant="outline" data-dialog-close>Close</button>
</footer>
<button type="button" class="btn" data-variant="ghost" data-size="icon-sm" aria-label="Close dialog" data-dialog-close><i class="icon icon-x" aria-hidden="true"></i></button>
</div>
</dialog>
A form with method="dialog" closes the dialog, and the page reads which button did from returnValue
A long section scrolls inside the dialog, and the header and footer stay put
The panel is never taller than the window. Give the section the classes min-h-0 overflow-y-auto to make it scroll.
An alert dialog asks a question the user has to answer
The class is alert-dialog. Use it to confirm something that cannot be undone.
Alert dialog
<button type="button" data-dialog-open="demo-alert-dialog" class="btn" data-variant="outline">Open Alert Dialog</button>
<dialog id="demo-alert-dialog" class="alert-dialog" aria-labelledby="demo-alert-dialog-title" aria-describedby="demo-alert-dialog-description">
<div>
<header>
<h2 id="demo-alert-dialog-title">Are you absolutely sure?</h2>
<p id="demo-alert-dialog-description">This action cannot be undone. This will permanently delete your account and remove your data from our servers.</p>
</header>
<footer>
<button class="btn" data-variant="outline" data-dialog-close>Cancel</button>
<button class="btn" data-dialog-close>Continue</button>
</footer>
</div>
</dialog>
A figure in the header puts an icon beside the question
<button type="button" class="btn" data-variant="outline" data-dialog-open="demo-alert-dialog-icon">Delete Product</button>
<dialog id="demo-alert-dialog-icon" class="alert-dialog" aria-labelledby="demo-alert-dialog-icon-title" aria-describedby="demo-alert-dialog-icon-description">
<div>
<header>
<figure><i class="icon icon-trash-2 text-destructive" aria-hidden="true"></i></figure>
<h2 id="demo-alert-dialog-icon-title">Delete Timeless Watch?</h2>
<p id="demo-alert-dialog-icon-description">The product and its 3 variants are removed from the store. Orders that hold it keep their lines.</p>
</header>
<footer>
<button class="btn" data-variant="outline" data-dialog-close>Cancel</button>
<button class="btn" data-variant="destructive" data-dialog-close>Delete Product</button>
</footer>
</div>
</dialog>
data-size="sm" on an alert dialog centres it and gives the two buttons equal width
API Reference
Attributes
| Attribute | On | Values | Default | Description |
|---|---|---|---|---|
| class | the dialog | "dialog", "alert-dialog" | none | A dialog with content and a close button, or a question with two answers. |
| data-size | dialog.dialog | "sm", "lg", "xl" | none (37.5rem) | The panel's width from 640px up: 24rem, 42rem or 56rem. Below 640px every size is the window less 1rem each side. |
| data-size | dialog.alert-dialog | "sm" | none | A narrower alert dialog with its text centred and its two buttons side by side at equal width. |
| aria-labelledby | the dialog | the id of its title | none | Names the dialog for a screen reader. Required. |
| aria-describedby | the dialog | the id of its text | none | Reads the line under the title as the dialog's description. |
| autofocus | a control in the dialog | present or absent | absent | The control that takes focus when the dialog opens. Without it the first control does. |
| method | a form in the dialog | "dialog" | none | Submitting the form closes the dialog without sending anything; the pressed button's value becomes returnValue. |
| open | the dialog | present or absent | set by the browser | Present while the dialog is open. Do not write it: a dialog opened that way is not modal. |
| data-dialog-open | a button | the dialog's id | none | A click opens that dialog with showModal(). An id with no dialog on the page throws an error. |
| data-dialog-close | a button in the dialog | present or absent | absent | A click closes the dialog the button is in. |
| data-dialog-static | dialog.dialog | present or absent | absent | Keeps the dialog open on a click on the dimmed area. An alert dialog stays open without it. |
Methods and properties
| Member | Type | Description |
|---|---|---|
| showModal() | method | The element's own. Opens the dialog over the dimmed page and keeps focus inside it. |
| close(returnValue) | method | The element's own. Closes the dialog and gives focus back to what opened it. The optional text becomes returnValue. |
| returnValue | property | The element's own. The text given to close(), or the value of the button that submitted a form with method="dialog". Empty after Escape. |
Events
| Event | On | Detail | Description |
|---|---|---|---|
| close | the dialog | none | The element's own. Sent after the dialog has closed, however it closed. Read returnValue in it. |
| cancel | the dialog | none | The element's own. Sent when Escape is pressed, before the dialog closes. Calling preventDefault() on it keeps the dialog open. |
Keyboard
| Key | Description |
|---|---|
| Escape | Closes the dialog. This is the browser's own behaviour, and it applies to an alert dialog too. |
| Tab | Moves between the controls in the dialog and does not reach the page behind it. |
| Enter | In a form field: submits the form, which closes the dialog when the form's method is "dialog". |
Everything above marked as the element's own is the browser's dialog element. The three data-dialog attributes are read by dialog.js.
A click that starts and ends on the dimmed area closes a dialog. A drag that ends there does not.
A popover, a select or a date picker opened inside a dialog closes on Escape first; the dialog stays open.
The drawer and the command palette are also dialog elements, with their own classes: see Drawers and Commands.