Modal
A modal presents content in a layer above the page. The platform already does
the work: buttons open and close a native <dialog> with
invoker commands,
showModal() makes the page inert, focuses the dialog content and returns focus
on close.
<cinq-modal> wraps the <dialog> and adds only what the platform lacks:
modal:before-open/modal:before-close, to cancel or defer an open or a close withwaitUntil(promise);data-replaceon ashow-modalbutton, to open the next modal in place of the current one instead of over it;- the backdrop click of
closedby="any"in Safari, which does not support it yet.
Install
Section titled “Install”Requires Node.js 18+ (build tooling).
pnpm add @agencecinq/modalImport once:
import "@agencecinq/modal";Then write the markup: a <dialog> with an id inside <cinq-modal>, and
buttons that target it with commandfor. Without JavaScript, or before the
element is defined, the buttons still open and close the dialog: only the hooks
are missing.
HTML is the source of truth. The component will not auto-set
role, auto-migrate attributes, or warn about missing labels. Run an a11y linter (axe, Lighthouse) to catch invalid markup.
<button type="button" commandfor="example-modal" command="show-modal"> Open modal</button>
<cinq-modal> <dialog id="example-modal" closedby="any" aria-labelledby="example-dracolich-lore-title" class="fixed left-1/2 top-1/2 w-[min(92vw,32rem)] -translate-x-1/2 -translate-y-1/2 rounded-xl bg-white p-6 shadow-2xl backdrop:bg-black/50 text-gray-900" > <h2 id="example-dracolich-lore-title" class="text-xl font-semibold mb-2">Dracolich lore</h2> <p class="text-gray-800 mb-4"> The dracolich is an undead dragon created by forbidden rites. Opening this modal feels like leafing through the AD&D Monster Manual. </p>
<button type="button" commandfor="example-modal" command="request-close" class="px-4 py-2 rounded-lg bg-black text-white hover:bg-gray-800" > Close </button> </dialog></cinq-modal>Markup
Section titled “Markup”| Element / attribute | Required | Role |
|---|---|---|
<cinq-modal> |
Yes | Host. Wraps one <dialog>. |
<dialog id> |
Yes | The modal. Buttons target its id with commandfor. |
closedby="any" on <dialog> |
No | Closes on a backdrop click. Without it, only Escape and buttons close the modal. |
autofocus |
No | Element focused on open. Defaults to the first focusable element. |
data-replace on a show-modal button |
No | Opens the target in place of the modal holding the button, instead of over it. The target’s modal:before-open runs first: if it cancels, the current modal stays open. Without JavaScript, the target opens over it. |
Commands
Section titled “Commands”| Button | Effect | Hook |
|---|---|---|
command="show-modal" |
Opens the dialog. | modal:before-open |
command="request-close" |
Closes the dialog. The button value becomes dialog.returnValue. |
modal:before-close |
command="close" |
Closes the dialog at once. | None |
Escape and the backdrop click (closedby="any") go through
modal:before-close too. Direct calls skip the hooks, like on the platform:
dialog.showModal(), dialog.close(), <form method="dialog">, and Escape
when the browser forces the close (pressed repeatedly without user activation).
Methods
Section titled “Methods”| Method | Description |
|---|---|
show() |
Opens the modal. Resolves true if this call opened it, false if already open or canceled. |
close(returnValue?) |
Closes the modal. Resolves true if this call closed it, false if already closed or canceled. |
init() |
Binds listeners. Called on connect. |
destroy() |
Removes listeners and closes the dialog. Called on disconnect. |
$dialog holds the <dialog>, trigger the button that last opened the modal.
Events
Section titled “Events”Prefer the constants from @agencecinq/utils (EVENTS.MODAL_*). Events are
dispatched on document.documentElement. detail.modal is the dialog id.
| Event | Cancelable | Detail payload | Description |
|---|---|---|---|
modal:before-open |
Yes | { modal, instance, trigger, waitUntil } |
Fired before the dialog opens. preventDefault() cancels, waitUntil(promise) defers. |
modal:before-close |
Yes | { modal, instance, waitUntil } |
Fired before the dialog closes. preventDefault() cancels, waitUntil(promise) defers. |
Once the dialog opens or closes, it fires the native toggle event
(event.newState is "open" or "closed"):
document.querySelector("#my-modal").addEventListener("toggle", (event) => { console.log(event.newState);});Deferring open or close
Section titled “Deferring open or close”Without a listener, the modal opens or closes immediately. Call
preventDefault() to cancel, or pass a promise to detail.waitUntil() to defer
until async work finishes:
import { EVENTS } from "@agencecinq/utils";
document.documentElement.addEventListener(EVENTS.MODAL_BEFORE_OPEN, (event) => { if (event.detail.modal !== "my-modal") return;
event.detail.waitUntil(doAsyncWork());});
document.documentElement.addEventListener(EVENTS.MODAL_BEFORE_CLOSE, (event) => { if (event.detail.modal !== "my-modal") return;
event.detail.waitUntil(doAsyncWork());});Several listeners can defer the same action: the modal waits for every
promise, and a rejected one cancels the action. Call waitUntil()
synchronously in the listener.
UX: defer open with before-open when the fetch is short (roughly under
500 ms) and the dialog would feel broken if empty: show loading on
detail.trigger while the promise is pending. For slower or unpredictable
loads, skip the defer: let the modal open immediately and fetch on toggle
with a skeleton or spinner inside the dialog. Defer close with
before-close when you need to save, archive, or run an exit animation before
dismissing.
TypeScript: import BeforeOpenDetail and BeforeCloseDetail from
@agencecinq/modal to type listeners.
Background scroll
Section titled “Background scroll”Not handled by the package. Native showModal() makes the page inert (no
focus / pointer outside the dialog) but does not lock document scroll. A
wheel over the backdrop can still move the page underneath.
That lock is a document / theme concern (layout, scrollbar gutter, iOS quirks), so the consumer owns it, in CSS:
html:has(dialog:modal) { overflow: hidden; scrollbar-gutter: stable;}Works with stacked modals: as long as any modal dialog is open, scroll stays locked. Keep the dialog content scrollable when it overflows. Only lock the page behind.
Older browsers
Section titled “Older browsers”Invoker commands need Chrome 135, Firefox 144 or Safari 26.2. For older browsers, load the invokers polyfill before the component.
Migration from 5.x
Section titled “Migration from 5.x”Version 6.0 drops <cinq-modal-button> for invoker commands, moves the id
to the <dialog>, needs closedby="any" to close on a backdrop click, and
removes modal:toggle, modal:open and modal:close in favor of show() and
the native toggle event. Details in the
README.
Interactive example
Section titled “Interactive example”Playground (live)
Section titled “Playground (live)”Confirm dialog
A confirm/cancel flow: each button closes with request-close and its value, read back from returnValue.
Waiting for your choice...
Inline form
A small form inside the modal. The dialog closes on submit and shows the result outside.
No character saved yet.
Stacked modals (W3C APG)
Section titled “Stacked modals (W3C APG)”Following the Dialog (Modal) pattern: a dialog may open over another. Content underneath is inert. Closing the top dialog returns focus to its invoker (e.g. Verify address inside the parent). Closing the parent returns focus to Add delivery address on the page.
Stacked modals (APG)
A modal can open over another. The lower dialog stays open but inert (native showModal() top layer). Closing the top dialog returns focus to the control that opened it. Inside the parent modal, not the page.
Flow: open address to verify to close verify (focus on Verify) to close address (focus on Add delivery address).
Replace: open next, close current
Section titled “Replace: open next, close current”Not a stack: Continue opens the next dialog with command="show-modal"
and data-replace. The parent closes, then the child opens. Back does the
same toward the parent, and modal:before-open copies the street into the child
before it shows. Closing the child returns focus to Start checkout on the page (native restore after the parent
already returned focus there). Without JavaScript, the child opens over the
parent.
Replace: open next, close current
From the first modal, Continue opens the confirmation withshow-modal and data-replace. The parent closes, then the child opens. Not a stack.Back replaces it the other way, with the street still filled in. Closing confirm returns focus to Start checkout.
Flow: Start checkout to Continue (and Back) to Cancel/Place order to focus back on Start checkout.
Async bestiary (before-open / before-close)
Section titled “Async bestiary (before-open / before-close)”Try opening and closing the modal below. Each transition is deferred until async
work finishes: the creature loads before showModal(), then archives before the
dialog closes.
Bestiary modal
Open the modal to load a creature. Close it to archive the entry.