modal

onChange called 0 time(s)

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 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

modal anatomy
Anatomy perspectiveFlat
Layers
Open detailsProject detailsA little more context.×
modal.root

The dialog itself; the trigger, backdrop, surface and close control go inside it.

  • modal.root holds the state: whether the dialog is open, and whether it is an alert.
  • modal.trigger is a <button> that opens the dialog. It writes aria-haspopup="dialog", aria-expanded and aria-controls.
  • modal.backdrop is the layer behind the dialog. It carries hidden while the dialog is closed.
  • modal.content is the dialog. It writes role="dialog", aria-modal="true" and tabindex="-1".
  • modal.title is an <h2>. It gives the dialog its name. See Name the dialog.
  • modal.description is a <p>. A screen reader reads it after the name.
  • modal.close is 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.