Side drawer
effect · overlay · navigation · T1 · v1.0.0 · readyAbout
A modal drawer built on the native `<dialog>`: focus stays inside, the page behind is inert, Escape and a click on the dimmed page close it. Opening slides the panel in from the right or left while the page dims, then fades the content in; closing reverses it. Without motion it opens and closes at once.
Open the drawer; close it with Escape, the close button or a click on the dimmed page.
Layers
- Native
- No gated feature: supported everywhere.
- Static
- Opens and closes at once, with the same dimmed page.
Skin
| Variable | Default | Meaning |
|---|---|---|
--side-drawer-width | 24rem | Panel width; capped at the viewport width minus the inset on both sides. |
--side-drawer-inset | 0.75rem | Gap between the panel and the viewport edges; 0 for an edge-to-edge drawer. |
--side-drawer-radius | 1rem | Corner radius of the panel. |
--side-drawer-surface | #fff | Panel background. |
--side-drawer-backdrop | rgb(0 0 0 / 0.4) | Colour laid over the page behind the panel, usually translucent. |
--side-drawer-duration | 400ms | Length of the slide, of the dimming and of the content fade. |
--side-drawer-easing | cubic-bezier(0.32, 0.72, 0, 1) | Easing of the slide and of the content fade. |
--side-drawer-content-delay | 150ms | Delay before the content fades in, after the panel starts sliding. |
Accessibility
Static layer: opens and closes at once.
Needs an accessible name: `label` or `labelledBy`.
Sources
- reel · thedesignshop.tds · "Cursor with Context" reel (drawer opened by a click); technique only (P6).
Code
Self-contained: copy every file, keeping the folder structure.
mechanism.module.css/* web-effects side-drawer 1.0.0 · MIT-0 */
/* Mechanism of the side drawer (D2.8, D3.11), shared by both styling variants.
.drawer is a modal <dialog> (showModal: top layer, focus trap, Escape, inert page, ::backdrop)
positioned as a floating panel along one side; .body fills it and scrolls. Opening: the panel
slides in from its side while the backdrop fades in, then the content fades in. Closing: the
hook sets data-closing, waits for the drawer's animations, then closes the dialog.
L1: CSS animations, run by every supported browser: no feature gate, no JS layer. JS only
opens, closes and waits (D3.6c). L3 (base rules): opens and closes at once. */
.drawer {
--_side-drawer-width: var(--side-drawer-width, 24rem);
--_side-drawer-inset: var(--side-drawer-inset, 0.75rem);
--_side-drawer-radius: var(--side-drawer-radius, 1rem);
--_side-drawer-surface: var(--side-drawer-surface, #fff);
--_side-drawer-backdrop: var(--side-drawer-backdrop, rgb(0 0 0 / 0.4));
--_side-drawer-duration: var(--side-drawer-duration, 400ms);
--_side-drawer-easing: var(
--side-drawer-easing,
cubic-bezier(0.32, 0.72, 0, 1)
);
--_side-drawer-content-delay: var(--side-drawer-content-delay, 150ms);
/* Off-screen offset the panel slides from: its own width plus the inset. */
--_side-drawer-from: calc(100% + var(--_side-drawer-inset));
/* Replaces the UA's centred modal box. */
position: fixed;
inset: var(--_side-drawer-inset) var(--_side-drawer-inset)
var(--_side-drawer-inset) auto;
box-sizing: border-box;
width: min(var(--_side-drawer-width), 100% - 2 * var(--_side-drawer-inset));
height: auto;
max-width: none;
max-height: none;
margin: 0;
padding: 0;
border: 0;
border-radius: var(--_side-drawer-radius);
background: var(--_side-drawer-surface);
color: inherit;
overflow: hidden;
}
.drawer[data-side="left"] {
--_side-drawer-from: calc(-100% - var(--_side-drawer-inset));
inset: var(--_side-drawer-inset) auto var(--_side-drawer-inset)
var(--_side-drawer-inset);
}
/* ::backdrop inherits the alias from the dialog (Chrome 122, Firefox 120, Safari 17.4). */
.drawer::backdrop {
background: var(--_side-drawer-backdrop);
}
/* Fills the panel, so a click whose target is the dialog itself landed on the backdrop. */
.body {
box-sizing: border-box;
height: 100%;
overflow: auto;
overscroll-behavior: contain;
}
@keyframes slide-in {
from {
translate: var(--_side-drawer-from) 0;
opacity: 0;
}
}
@keyframes slide-out {
to {
translate: var(--_side-drawer-from) 0;
opacity: 0;
}
}
@keyframes fade-in {
from {
opacity: 0;
}
}
@keyframes fade-out {
to {
opacity: 0;
}
}
@keyframes content-in {
from {
translate: 0 0.5rem;
opacity: 0;
}
}
/* L1 native (gated on the override attribute, D3.6a). */
:root:not([data-fx-layer]) .drawer[open],
:root[data-fx-layer="native"] .drawer[open] {
animation: slide-in var(--_side-drawer-duration) var(--_side-drawer-easing);
}
:root:not([data-fx-layer]) .drawer[open]::backdrop,
:root[data-fx-layer="native"] .drawer[open]::backdrop {
animation: fade-in var(--_side-drawer-duration) ease-out;
}
:root:not([data-fx-layer]) .drawer[open] .body,
:root[data-fx-layer="native"] .drawer[open] .body {
animation: content-in var(--_side-drawer-duration) var(--_side-drawer-easing)
var(--_side-drawer-content-delay) both;
}
/* Closing: the hook waits for the drawer's own animation (the slide) before closing. */
:root:not([data-fx-layer]) .drawer[data-closing],
:root[data-fx-layer="native"] .drawer[data-closing] {
animation: slide-out var(--_side-drawer-duration) var(--_side-drawer-easing)
forwards;
}
:root:not([data-fx-layer]) .drawer[data-closing]::backdrop,
:root[data-fx-layer="native"] .drawer[data-closing]::backdrop {
animation: fade-out var(--_side-drawer-duration) ease-in forwards;
}
:root:not([data-fx-layer]) .drawer[data-closing] .body,
:root[data-fx-layer="native"] .drawer[data-closing] .body {
animation: none;
}
/* L3 under reduced motion (unless forced): open and close at once. Same specificity as the
native rules above ([open] also matches while closing), so these win by order. */
@media (prefers-reduced-motion: reduce) {
:root:not([data-fx-layer]) .drawer[open],
:root:not([data-fx-layer]) .drawer[open]::backdrop,
:root:not([data-fx-layer]) .drawer[open] .body {
animation: none;
}
}
tailwind/SideDrawer.tsx// web-effects side-drawer 1.0.0 · MIT-0
"use client";
import type { ReactNode } from "react";
import mechanism from "../mechanism.module.css";
import { useSideDrawer } from "../use-side-drawer";
/**
* A modal panel that slides in from one side over a dimmed page. Controlled: `open` and
* `onOpenChange` (called with false on Escape, on a backdrop click, or when the dialog closes).
* Name it with `label` or `labelledBy`. `lockScroll` stops the page scrolling while open.
*/
export function SideDrawer({
open,
onOpenChange,
children,
side = "right",
label,
labelledBy,
lockScroll = true,
className = "",
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
children: ReactNode;
side?: "left" | "right";
label?: string;
labelledBy?: string;
lockScroll?: boolean;
className?: string;
}) {
const { dialogProps } = useSideDrawer({ open, onOpenChange, lockScroll });
return (
<dialog
{...dialogProps}
data-side={side}
aria-label={label}
aria-labelledby={labelledBy}
className={`${mechanism.drawer} shadow-2xl ${className}`}
>
<div className={`${mechanism.body} p-6`}>{children}</div>
</dialog>
);
}
use-side-drawer.ts// web-effects side-drawer 1.0.0 · MIT-0
import {
useEffect,
useLayoutEffect,
useRef,
useState,
type MouseEvent,
type PointerEvent,
type SyntheticEvent,
} from "react";
// Behaviour shared by both styling variants (P8): syncing `open` with the modal dialog, the
// animated close, light dismiss, and the scroll lock. CSS owns every animation; closing waits
// for whatever animations the drawer runs (none on the static layer, so it closes at once).
export function useSideDrawer({
open,
onOpenChange,
lockScroll,
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
lockScroll: boolean;
}) {
const ref = useRef<HTMLDialogElement>(null);
const latest = useRef({ open, onOpenChange });
useLayoutEffect(() => {
latest.current = { open, onOpenChange };
});
// Whether the dialog is showing, closing animation included: drives the scroll lock.
const [shown, setShown] = useState(false);
useEffect(() => {
const dialog = ref.current;
if (!dialog) return;
if (open) {
delete dialog.dataset.closing;
if (!dialog.open) {
dialog.showModal();
setShown(true);
}
return;
}
if (!dialog.open) return;
let cancelled = false;
dialog.dataset.closing = "";
// getAnimations() applies the pending style change first, so the slide-out is included.
Promise.allSettled(dialog.getAnimations().map((a) => a.finished)).then(
() => {
if (cancelled) return;
dialog.close();
delete dialog.dataset.closing;
},
);
return () => {
cancelled = true;
};
}, [open]);
useEffect(() => {
if (!shown || !lockScroll) return;
const root = document.documentElement;
const previous = root.style.overflow;
root.style.overflow = "hidden";
return () => {
root.style.overflow = previous;
};
}, [shown, lockScroll]);
// Light dismiss: press and release both on the backdrop (the dialog itself, since .body fills
// the panel), so a text selection dragged out of the panel does not close it.
const pressedOnBackdrop = useRef(false);
const requestClose = () => {
if (latest.current.open) latest.current.onOpenChange(false);
};
return {
dialogProps: {
ref,
onCancel: (event: SyntheticEvent<HTMLDialogElement>) => {
// Escape: animate instead of closing at once. Browsers may refuse the cancellation
// (repeated Escape without user activation); onClose then keeps `open` in sync.
event.preventDefault();
requestClose();
},
onClose: () => {
setShown(false);
requestClose();
},
onPointerDown: (event: PointerEvent<HTMLDialogElement>) => {
pressedOnBackdrop.current = event.target === event.currentTarget;
},
onClick: (event: MouseEvent<HTMLDialogElement>) => {
if (pressedOnBackdrop.current && event.target === event.currentTarget)
requestClose();
pressedOnBackdrop.current = false;
},
},
};
}