modal
onChange called 0 time(s)
Ship to
12 Harbour Lane
import { state } from '@markless/core';import { lucide, modal } from '@markless/ui';export default function ModalBasic() @{ let street = state('12 Harbour Lane'); <modal.root class="modal"> <div class="card"> <p class="card-label">Ship to</p> <p class="card-address">{street}</p> <modal.trigger class="button open"> <lucide.pencil class="icon" aria-hidden="true" /> Edit address </modal.trigger> </div> <modal.backdrop class="backdrop"> <modal.content class="dialog"> <modal.title class="title">Edit delivery address</modal.title> <modal.description class="description">We will send your order here.</modal.description> <label class="field-label"> Street <input class="field" value={street} onInput={(event) => { street = (event.target as HTMLInputElement).value; }} /> </label> <modal.close class="button done">Done</modal.close> </modal.content> </modal.backdrop> </modal.root>;}
.modal { width: min(22rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.card { display: grid; gap: 0.35em; justify-items: start; padding: 1em 1.1em; border: 1px solid var(--ink); border-radius: 6px;}.card-label,.card-address { margin: 0; font-family: 'Shantell Sans', system-ui, sans-serif;}.card-label { color: var(--muted-ink);}.card-address { margin-block-end: 0.5em; font-size: var(--step-0);}.button { display: inline-flex; gap: 0.5em; align-items: center; padding: 0.45em 0.9em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif; cursor: pointer; transition: background 140ms ease;}.button:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.button:active,.open[ui-open] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.button:focus-visible,.field:focus-visible,.dialog:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px;}.icon { width: 1em; height: 1em; flex: none;}.backdrop:not([hidden]) { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 1rem; background: color-mix(in oklch, var(--slab) 55%, transparent);}.dialog { display: grid; gap: 0.75em; box-sizing: border-box; width: min(24rem, 100%); padding: 1.35em; border: 1.5px solid var(--ink); border-radius: 10px; background: var(--raised); box-shadow: 4px 4px 0 var(--muted-ink); color: var(--ink);}.title { margin: 0; font-family: 'Joy Elia', system-ui, sans-serif; font-size: var(--step-1); line-height: 1.2; text-shadow: none;}.description { margin: 0; font-family: 'Shantell Sans', system-ui, sans-serif;}.field-label { display: grid; gap: 0.3em; font-family: 'Shantell Sans', system-ui, sans-serif;}.field { padding: 0.45em 0.7em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif;}.done { justify-self: end;}@media (prefers-reduced-motion: reduce) { .button { transition: none; }}
modal is a dialog that blocks the page behind it. Use it when a person must finish or cancel one
task before they go back to the page.
modal.backdrop wraps modal.content. It does not sit beside it. The dialog sits inside the dim
layer, so you can center it with one grid rule on the backdrop.
Anatomy
The dialog itself; the trigger, backdrop, surface and close control go inside it.
modal.rootholds the state: whether the dialog is open, and whether it is an alert.modal.triggeris a<button>that opens the dialog. It writesaria-haspopup="dialog",aria-expandedandaria-controls.modal.backdropis the layer behind the dialog. It carrieshiddenwhile the dialog is closed.modal.contentis the dialog. It writesrole="dialog",aria-modal="true"andtabindex="-1".modal.titleis an<h2>. It gives the dialog its name. See Name the dialog.modal.descriptionis a<p>. A screen reader reads it after the name.modal.closeis a<button>that closes the dialog.
The root, the trigger, the backdrop and the content write ui-open while the dialog shows. They
write ui-closed while it does not.
What the tests check
- While the dialog is open, the page behind it is inert. Focus cannot reach it, and the page does not scroll.
- Tab never leaves the dialog.
- Escape closes the dialog and puts focus back on the trigger.
- If a dialog opens a second dialog, Escape closes only the one on top.
- A press on the backdrop closes the dialog. The press must start and end on the backdrop.
- A right-button press on the backdrop does nothing.
- A press inside the dialog never closes it.
When the dialog opens, focus goes to modal.content itself, not to its first control. If your own
code opens the dialog through open, focus goes back where it was when the dialog closes.
Name the dialog
Render modal.title and it names the dialog: the content points aria-labelledby at it, with no prop. VoiceOver and NVDA both read that name, on focus and on a walk, wherever the surface sits on the page. Use aria-label on modal.content only when there is no title. With a title rendered, aria-labelledby outranks aria-label in the accessible name, so an aria-label beside a title changes nothing.
<modal.content>
<modal.title>Edit delivery address</modal.title>
</modal.content>
<modal.content aria-label="Confirm the move">
<p>Move these files to the archive?</p>
</modal.content>Alerts
Set alert on the root for a dialog that asks a person to confirm something. The content then
writes role="alertdialog". A press on the backdrop does not close it, but Escape does. Focus
goes to modal.close when it opens.
<modal.root alert>Style the backdrop
modal ships no CSS. The backdrop does not cover the page until you write that rule. Give it a
class, such as backdrop:
.backdrop:not([hidden]) {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: rgb(0 0 0 / 0.4);
}The dialog takes focus when it opens, so the browser can draw a focus ring around all of it. Style
:focus-visible on your dialog class to change that ring.
Props on modal.root
| Prop | Type | What it does |
|---|---|---|
open |
boolean |
Whether the dialog shows. Omit it and the dialog starts closed. |
alert |
boolean |
Makes an alert dialog. A backdrop press cannot close it. Focus goes to the close control when it opens. |
onChange |
(open) => void |
Gets the new value each time the dialog opens or closes. Escape and backdrop presses call it too. |
The root also passes anything a <div> accepts to its element. Your own onClick on the trigger
runs after the dialog opens. Your own onClick on modal.close runs after it closes.