Skip to main content

Basic

A modal is a dialog that sits on top of the page and blocks it until the person closes it. button-text or button-icon renders the button that opens it; button-label, button-variant, button-appearance and button-class set its name, look and classes. The id is on the root, the button is <id>-button and the dialog <id>-panel. A button of your own goes in the button slot and has aria-controls (<id>-panel), aria-expanded="false" and aria-haspopup="dialog". While the dialog is open the head script adds role="dialog" (or the role in data-dialog-role, see Confirm) and aria-modal="true", moves focus to the first focusable element, keeps Tab inside it, closes on Esc or a click on the backdrop, and returns focus to the trigger. A button with aria-controls but no aria-expanded inside the dialog is a close button. Name the dialog with aria-labelledby (the title) and aria-describedby. The close X is last in the markup, so the first focus lands on the content or the first action; it is placed at the top right with absolute. To lock the page scroll, put has-[[aria-modal=true]]:overflow-hidden on <body> (this page does).

Custom header

The header slot replaces the title and the description; the close button stays, and title becomes the aria-label of the dialog. A button without text needs button-label.

Sizes

Set the maximum width on the panel: max-w-sm, max-w-lg (the default), max-w-2xl and max-w-4xl. On a narrow screen every size fills the width, minus the page padding.

Position

Change where the panel sits with the alignment of the wrapper (items-start, items-center, items-end) and its padding. Resize the window to see the bottom sheet and the full-screen version on a narrow screen.

Confirm

A short question before something that cannot be undone. Say what will happen and name the thing (Delete “Website redesign”, not Are you sure?), and label the buttons with the action. Cancel comes first in the markup, so the first focus lands on the safe choice and Enter does not delete by accident. This is an alert dialog: alert adds data-dialog-role="alertdialog" to the panel and the head script gives it role="alertdialog" instead of dialog while it is open. A screen reader then reads the title and the description (aria-describedby, required here) at once, so the person hears the warning before choosing. Use it only for a question that needs an answer; a form or a detail view stays a plain dialog.

Form

A form inside a modal. The first focus lands on the first field because the close button is last in the markup. The submit button is a plain type="submit" without aria-controls, so the browser validates first (leave the name empty to see it); only the Cancel button closes the dialog by itself. After a valid submit, close it from the submit handler: @submit.prevent="$el.querySelector('[aria-controls="modal-form-panel"]').click()", or after your request succeeds.

Scrollable

When the content is longer than the screen, limit the panel height (max-h-[calc(100dvh-2rem)]), make it a column (flex flex-col) and let only the body scroll (min-h-0 flex-1 overflow-y-auto), so the title and the buttons stay in view. The scrolling body is focusable (tabindex="0", role="region" with a name) so the keyboard can scroll it. If the body is short, the dialog is as tall as its content.

Static backdrop

static adds data-static to the panel, so a click outside does not close the dialog. Esc still closes it, because a dialog must always have a keyboard way out.