Drawer
A drawer slides a panel in from the edge of the viewport. The platform already
does the work: buttons open and close a native <dialog> with
invoker commands,
showModal() puts it in the top layer, makes the page inert, focuses the
dialog content and returns focus on close.
<cinq-drawer> wraps the <dialog> and adds only what the platform lacks:
drawer:before-open/drawer:before-close, to cancel or defer an open or a close withwaitUntil(promise)data-replaceon a button, to open the next drawer 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”pnpm add @agencecinq/drawerImport once:
import "@agencecinq/drawer";Then write the markup: a <dialog> inside <cinq-drawer>, and buttons that
target its id with commandfor. Without JavaScript, the buttons still open
and close the drawer: only the hooks are missing.
HTML is the source of truth. The component will not auto-set labels, auto-migrate attributes, or warn about missing names. Run an a11y linter (axe, Lighthouse) to catch invalid markup.
<button type="button" commandfor="demo-drawer" command="show-modal">Open drawer</button>
<cinq-drawer> <dialog id="demo-drawer" closedby="any" aria-labelledby="demo-drawer-title" class="drawer"> <div class="p-8 flex flex-col gap-4"> <h2 id="demo-drawer-title" class="text-2xl font-bold">Bestiary drawer</h2> <p class="text-gray-600"> Use the drawer as a sliding panel listing creatures from your AD&D bestiary: trolls, dracoliches, or galeb duhrs guarding the dungeon entrance. </p> <button type="button" commandfor="demo-drawer" command="request-close">Close</button> </div> </dialog></cinq-drawer>Styling
Section titled “Styling”The component never writes inline styles. Position and animate the <dialog>
and its ::backdrop from CSS:
@starting-stylesets where the entry animation starts.transition-behavior: allow-discreteondisplayandoverlaykeeps the dialog rendered, in the top layer, until the exit animation ends.
.drawer { inset: 0 0 0 auto; margin: 0; width: min(100%, 28rem); max-width: none; height: 100%; max-height: none; border: 0; padding: 0; translate: 100% 0; transition: translate 300ms, display 300ms allow-discrete, overlay 300ms allow-discrete;}
.drawer:is([open], :popover-open) { translate: 0 0;}
@starting-style { .drawer:is([open], :popover-open) { translate: 100% 0; }}
.drawer::backdrop { background: rgb(0 0 0 / 0.5); opacity: 0; transition: opacity 300ms, display 300ms allow-discrete, overlay 300ms allow-discrete;}
.drawer[open]::backdrop { opacity: 1;}
@starting-style { .drawer[open]::backdrop { opacity: 0; }}With Tailwind, where open: matches both [open] and :popover-open:
fixed inset-y-0 right-0 left-auto m-0 h-full max-h-none w-full max-w-md border-0 p-0translate-x-full open:translate-x-0 starting:open:translate-x-fulltransition-[translate,overlay,display] transition-discrete duration-300backdrop:bg-black/50 backdrop:opacity-0 open:backdrop:opacity-100 starting:open:backdrop:opacity-0backdrop:transition-[opacity,overlay,display] backdrop:transition-discrete backdrop:duration-300Browsers without @starting-style or allow-discrete open and close the
drawer without animation.
The UA stylesheet sets color: CanvasText and background: Canvas on the
<dialog>, so it does not inherit the page text color. Set both on the dialog
or its content, or a dark color scheme renders white text on a white panel.
Background scroll
Section titled “Background scroll”The package does not lock document scroll. A modal dialog makes the page inert but the page can still scroll underneath. Lock it in CSS:
html:has(cinq-drawer > dialog:is(:modal, :popover-open)) { overflow: hidden; scrollbar-gutter: stable;}Non-modal drawer
Section titled “Non-modal drawer”Add popover to the <dialog> when a control outside the drawer must stay
usable while it is open, such as a burger menu in a header above the drawer.
The burger toggles it with command="toggle-popover":
<header class="sticky top-0 z-20"> <button type="button" commandfor="menu-drawer" command="toggle-popover">Menu</button></header>
<cinq-drawer> <dialog id="menu-drawer" popover aria-label="Menu" class="drawer peer">...</dialog> <div class="scrim peer-open:opacity-100" aria-hidden="true"></div></cinq-drawer>The browser handles the rest: aria-expanded on the burger, Escape, a press
outside the drawer, focus moved into the dialog and back to the burger on
close, and one drawer open at a time. Focus is not contained: Tab can leave the
drawer, as in a disclosure menu.
The popover sits in the top layer, above the header: offset it from the top
(margin-top) to leave the header visible. Its ::backdrop would cover the
header too, so dim the page with a scrim placed after the dialog, below the
header, and shown with dialog:popover-open + .scrim (Tailwind: peer on the
dialog, peer-open: on the scrim). Give the scrim pointer events while it
shows, so a press on it closes the drawer instead of reaching the page below.
Escape and a press outside close the drawer without drawer:before-close: the
browser does not let a popover cancel them. Buttons and close() still go
through the hook. A <form method="dialog"> does not close a popover: close it
on submit.
When the drawer covers the page, make the covered regions inert while it is open, so Tab and screen readers stay out of what they cannot see. The drawer leaves this to the page, which alone knows the regions it covers. Leave out the region holding the triggers, here the header:
document.querySelector("#menu-drawer").addEventListener("beforetoggle", (event) => { const inert = event.newState === "open";
document.querySelectorAll("main, footer").forEach(($element) => { $element.inert = inert; });});beforetoggle fires before focus returns, so an opener inside a covered region
still gets its focus back.
Markup
Section titled “Markup”| Element / attribute | Required | Role |
|---|---|---|
<cinq-drawer> |
Yes | Host. Wraps one <dialog>. |
<dialog id> |
Yes | The drawer. Buttons target its id with commandfor. |
closedby="any" on <dialog> |
No | Closes a modal drawer on a backdrop click. Without it, only Escape and buttons close it. |
popover on <dialog> |
No | Makes a non-modal drawer. See Non-modal drawer. |
autofocus |
No | Element focused on open. Defaults to the first focusable element. |
data-replace on a button that opens |
No | Opens the target in place of the drawer holding the button. The target’s drawer:before-open runs first: if it cancels, the current drawer stays open. |
Commands
Section titled “Commands”| Button | Drawer | Effect | Hook |
|---|---|---|---|
command="show-modal" |
Modal | Opens the dialog. | drawer:before-open |
command="request-close" |
Modal | Closes the dialog. The button value becomes dialog.returnValue. |
drawer:before-close |
command="close" |
Modal | Closes the dialog at once. | None |
command="toggle-popover" |
Non-modal | Opens or closes the dialog. | drawer:before-open or drawer:before-close |
command="show-popover" |
Non-modal | Opens the dialog. | drawer:before-open |
command="hide-popover" |
Non-modal | Closes the dialog. | drawer:before-close |
On a modal drawer, Escape and the backdrop click (closedby="any") go through
drawer: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.
Methods
Section titled “Methods”| Method | Description |
|---|---|
show() |
Opens the drawer. Resolves true if this call opened it, false if already open or canceled. |
close(returnValue?) |
Closes the drawer. 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. |
Events
Section titled “Events”Prefer the constants from @agencecinq/utils (EVENTS.DRAWER_*). Events are
dispatched on document.documentElement.
| Event | Cancelable | Detail payload | Description |
|---|---|---|---|
drawer:before-open |
Yes | { drawer, instance, trigger, waitUntil } |
Fired before the dialog opens. preventDefault() cancels, waitUntil(promise) defers. |
drawer:before-close |
Yes | { drawer, instance, waitUntil } |
Fired before the dialog closes. preventDefault() cancels, waitUntil(promise) defers. |
detail.drawer is the dialog id. Once the dialog opens or closes, it fires
the native toggle event (event.newState is "open" or "closed"), in both
modes.
Deferring open or close
Section titled “Deferring open or close”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.DRAWER_BEFORE_OPEN, (event) => { if (event.detail.drawer !== "my-drawer") return;
event.detail.waitUntil(doAsyncWork());});
document.documentElement.addEventListener(EVENTS.DRAWER_BEFORE_CLOSE, (event) => { if (event.detail.drawer !== "my-drawer") return;
event.detail.waitUntil(doAsyncWork());});Several listeners can defer the same action: the drawer 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 panel would feel broken if empty: show loading on the trigger
while the promise is pending. For slower or unpredictable loads, skip
the defer: let the drawer open immediately and fetch on toggle with a
skeleton or spinner inside the panel. 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/drawer to type listeners.
Shopify (Liquid snippet)
Section titled “Shopify (Liquid snippet)”If you are integrating in a Shopify theme, you can use the provided snippet and Vite plugin. The plugin requires Node.js 18+.
1) Copy the snippet automatically (Vite)
Section titled “1) Copy the snippet automatically (Vite)”import { defineConfig } from 'vite';import { cinqDrawerPlugin } from '@agencecinq/drawer/plugin';
export default defineConfig({ plugins: [cinqDrawerPlugin()],});This copies the package snippet to your theme:
snippets/cinq-drawer.html.liquid
Snippet params: id (the dialog id), content, label (ID of the element
labelling the dialog), modal (false for a non-modal drawer with a scrim),
direction (from-right, from-left or from-bottom), classes (added to
the <dialog>), margin_top and background. A modal drawer closes on a
backdrop click.
2) Render the snippet
Section titled “2) Render the snippet”{% render 'cinq-drawer.html', id: 'bestiary-drawer', label: 'bestiary-drawer-title', content: '<div class="p-8"><h2 id="bestiary-drawer-title">Bestiary</h2><p class="mb-2">The galeb duhr looks like a boulder until it moves.</p><p class="mb-0">Trolls and jermlaines skulk deeper in the dungeon corridors.</p></div>'%}
<button type="button" commandfor="bestiary-drawer" command="show-modal">Open bestiary</button>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 8.x
Section titled “Migration from 8.x”Version 9.0 drops <cinq-drawer-button>: buttons target the dialog with
commandfor, the id moves to the <dialog>, data-modal="false" becomes
popover, and drawer:toggle, drawer:open and drawer:close give way to the
native toggle event. Scroll lock moves to CSS. Step-by-step guide in the
README.
Playground (live)
Section titled “Playground (live)”Bestiary drawer
A slide-in bestiary panel: backdrop, close on a backdrop click, and a tally of creatures catalogued from your AD&D campaign.
Replace: open next, close current
Section titled “Replace: open next, close current”A button with data-replace opens the other drawer in place of the one holding
it. The next drawer accepts the open first (drawer:before-open), then the
current one closes (drawer:before-close). Open notes, switch to bestiary,
then close it: focus lands back on Open left.
Replace a drawer
A data-replace button opens the other drawer in place of the one holding it, instead of over it. Closing the second drawer returns focus to the page button that opened the first.
Non-modal drawer (popover)
Section titled “Non-modal drawer (popover)”The page stays usable while the drawer is open. The same button opens and
closes it, and the browser sets its aria-expanded.
Spellbook menu
A non-modal drawer: the page stays usable, the same button opens and closes it, and a press outside or Escape closes it.
The spellbook is closed.
Async bestiary (before-open / before-close)
Section titled “Async bestiary (before-open / before-close)”Try opening and closing the drawer below. Each transition is deferred until async work finishes: the creature loads before the dialog opens, then archives before it closes. The panel shows the wait state while the hook is pending.
Bestiary drawer
Open the drawer to load a creature. Close it to archive the entry.