Explore the index
Browse components
NavigationstableReactPortalPointer EventsCSS

Elastic Sheet

A side panel on a desk, a bottom sheet in your hand.

Backdrop clickDesktop width

Open Filters, change the year range to trigger validation, then apply.

A real browser viewport. Scroll within the preview when the sample needs more space. Configuration stays in sync between sizes.

01What it does

A responsive dialog sheet with measured spring settling, a focus trap, scroll containment and optional touch dismissal. Desktop gets a side panel; narrow viewports get a bottom sheet with a grab handle.

In this previewA project library with a working filter form. Applying filters changes the visible cards and active chips; reset restores the full shelf. Validation blocks an inverted year range. The preview controls change backdrop behaviour, swipe dismissal and desktop width.

Design decisions

The sheet is portaled to the body so it cannot be clipped by a preview stage or a transformed card, while its CSS remains scoped to the component.

Opening locks page scroll with a scrollbar-width compensation value; the panel itself owns its overflow. The backdrop only changes opacity, so context behind it stays legible.

Touch dismissal lives on a dedicated handle with pointer capture and a threshold. It cannot fire from a scroll gesture in the body and is off by default.

02Install

Registry install

registry:component

Run this in a React project with components.json. The component and its stylesheet land in components/lab/elastic-sheet/.

npx shadcn@latest add https://lab.tanishk.me/r/elastic-sheet.json

Files included

2
  • components/lab/elastic-sheet/elastic-sheet.tsxcomponent
  • components/lab/elastic-sheet/elastic-sheet.cssstyles

Manual install: copy each file into the same folder and preserve its relative imports. Open the source

03Usage

elastic-sheet.usage.tsx
"use client";
import { useState } from "react";
import { ElasticSheet } from "@/components/lab/elastic-sheet/elastic-sheet";

export function LibraryFilters() {
const [open, setOpen] = useState(false);
return (
<>
<button type="button" onClick={() => setOpen(true)}>Filters</button>
<ElasticSheet
open={open}
onOpenChange={setOpen}
title="Filter projects"
description="Changes appear when you apply them."
closeOnBackdrop={false}
>
<form onSubmit={(event) => { event.preventDefault(); setOpen(false); }}>
{/* real controls and an apply action */}
</form>
</ElasticSheet>
</>
);
}

04Props

Props accepted by Elastic Sheet
PropTypeDefaultNotes
openboolean—Controlled visibility.
onOpenChange(open: boolean) => void—Called for Escape, the close button, backdrop dismissal and a completed swipe.
titlestring—Dialog heading and accessible name.
descriptionstring—Optional supporting line, linked with aria-describedby.
childrenReactNode—Scrollable sheet body; usually a form.
closeOnBackdropbooleantrueAllow a backdrop click to dismiss the sheet.
swipeToDismissbooleanfalseEnable the mobile grab-handle drag. Ignored for mouse pointers.
size"regular" | "wide""regular"Desktop panel width; mobile is always a full-width bottom sheet.
classNamestring—Merged onto the panel for local styling.

05Accessibility

  • Uses role="dialog" with aria-modal, labelled by its title and optionally described by its description.
  • Focus moves into the sheet on open, Tab and Shift+Tab are contained, and focus returns to the opener on close.
  • Escape always closes. Backdrop dismissal is configurable, and the close button is a label-able 36px target.
  • The page scroll is locked with scrollbar compensation, so opening does not shift the layout; content scrolls inside the sheet.
  • Swipe dismissal is opt-in, requires a deliberate 92px drag, and never competes with the sheet's vertical scroll.

06Source

2 files, 477 lines. The combined copy is a reference; save each file separately for use.

components/lab/elastic-sheet/elastic-sheet.tsx— press enter to show or hide the source
tsx
"use client";

import {
useCallback,
useEffect,
useId,
useRef,
useSyncExternalStore,
type PointerEvent as ReactPointerEvent,
type ReactNode,
} from "react";
import { createPortal } from "react-dom";
import "./elastic-sheet.css";

export type ElasticSheetProps = {
open: boolean;
onOpenChange: (open: boolean) => void;
title: string;
description?: string;
children: ReactNode;
/** Clicking the backdrop closes the sheet. Default true. */
closeOnBackdrop?: boolean;
/** Drag the mobile grab handle down to dismiss. Default false. */
swipeToDismiss?: boolean;
/** A wider desktop sheet; mobile remains a bottom sheet. */
size?: "regular" | "wide";
className?: string;
};

const FOCUSABLE =
'a[href],button:not([disabled]),textarea:not([disabled]),input:not([disabled]),select:not([disabled]),[tabindex]:not([tabindex="-1"])';

let lockCount = 0;
let restoreBody: (() => void) | null = null;

function lockPage() {
if (typeof document === "undefined") return () => {};
lockCount += 1;
if (lockCount === 1) {
const body = document.body;
const html = document.documentElement;
const scrollbar = window.innerWidth - html.clientWidth;
const previous = {
bodyOverflow: body.style.overflow,
bodyPadding: body.style.paddingRight,
htmlOverflow: html.style.overflow,
};
body.style.overflow = "hidden";
html.style.overflow = "hidden";
if (scrollbar > 0) body.style.paddingRight = `${scrollbar}px`;
restoreBody = () => {
body.style.overflow = previous.bodyOverflow;
body.style.paddingRight = previous.bodyPadding;
html.style.overflow = previous.htmlOverflow;
restoreBody = null;
};
}
let released = false;
return () => {
if (released) return;
released = true;
lockCount = Math.max(0, lockCount - 1);
if (lockCount === 0) restoreBody?.();
};
}

const noopSubscribe = () => () => {};
function useClientReady() {
return useSyncExternalStore(
noopSubscribe,
() => true,
() => false,
);
}

/**
* ELASTIC SHEET
*
* A side sheet on a wide viewport and a bottom sheet on a narrow one.
* The panel lands with a short damped spring; the backdrop only changes
* opacity, so the page behind keeps its context. Focus is trapped while
* open, Escape closes, and focus returns to the trigger. The mobile
* grab handle is inert unless `swipeToDismiss` is explicitly enabled.
*/
export function ElasticSheet({
open,
onOpenChange,
title,
description,
children,
closeOnBackdrop = true,
swipeToDismiss = false,
size = "regular",
className,
}: ElasticSheetProps) {
const uid = useId();
const ready = useClientReady();
const panelRef = useRef<HTMLDivElement>(null);
const closeRef = useRef<HTMLButtonElement>(null);
const returnFocus = useRef<HTMLElement | null>(null);
const wasOpen = useRef(open);
const drag = useRef({ startY: 0, delta: 0, active: false });

const close = useCallback(() => onOpenChange(false), [onOpenChange]);

useEffect(() => {
if (open) {
returnFocus.current =
document.activeElement instanceof HTMLElement
? document.activeElement
: null;
const panel = panelRef.current;
if (panel) {
panel.style.transform = "";
panel.style.transition = "";
}
const timer = window.setTimeout(() => closeRef.current?.focus(), 30);
return () => window.clearTimeout(timer);
}
if (!wasOpen.current) return;
const target = returnFocus.current;
const timer = window.setTimeout(() => {
if (target?.isConnected) target.focus();
}, 20);
return () => window.clearTimeout(timer);
}, [open]);

useEffect(() => {
wasOpen.current = open;
}, [open]);

useEffect(() => {
if (!open) return;
return lockPage();
}, [open]);

useEffect(() => {
if (!open) return;
const onKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") {
event.preventDefault();
close();
return;
}
if (event.key !== "Tab") return;
const panel = panelRef.current;
if (!panel) return;
const nodes = [...panel.querySelectorAll<HTMLElement>(FOCUSABLE)].filter(
(node) => node.getClientRects().length > 0,
);
if (!nodes.length) {
event.preventDefault();
panel.focus();
return;
}
const first = nodes[0];
const last = nodes[nodes.length - 1];
if (!panel.contains(document.activeElement)) {
event.preventDefault();
first.focus();
} else if (event.shiftKey && document.activeElement === first) {
event.preventDefault();
last.focus();
} else if (!event.shiftKey && document.activeElement === last) {
event.preventDefault();
first.focus();
}
};
document.addEventListener("keydown", onKeyDown);
return () => document.removeEventListener("keydown", onKeyDown);
}, [open, close]);

const onBackdrop = (event: React.MouseEvent<HTMLDivElement>) => {
if (event.target === event.currentTarget && closeOnBackdrop) close();
};

const onPointerDown = (event: ReactPointerEvent<HTMLDivElement>) => {
if (!swipeToDismiss || event.pointerType === "mouse") return;
drag.current = { startY: event.clientY, delta: 0, active: true };
event.currentTarget.setPointerCapture?.(event.pointerId);
};

const onPointerMove = (event: ReactPointerEvent<HTMLDivElement>) => {
if (!drag.current.active) return;
const panel = panelRef.current;
if (!panel) return;
const delta = Math.max(0, event.clientY - drag.current.startY);
drag.current.delta = delta;
panel.style.transition = "none";
panel.style.transform = `translate3d(0, ${delta}px, 0)`;
};

const endDrag = () => {
if (!drag.current.active) return;
const panel = panelRef.current;
const delta = drag.current.delta;
drag.current.active = false;
if (panel) {
panel.style.transition = "";
panel.style.transform = "";
}
if (delta > 92) close();
};

if (!ready) return null;

return createPortal(
<div
className="es-root"
data-visible={open ? "true" : "false"}
data-size={size}
inert={!open}
aria-hidden={!open}
>
<div className="es-backdrop" aria-hidden="true" onClick={onBackdrop} />
<div
ref={panelRef}
className={`es-panel${className ? ` ${className}` : ""}`}
role="dialog"
aria-modal="true"
aria-labelledby={`${uid}-title`}
aria-describedby={description ? `${uid}-description` : undefined}
tabIndex={-1}
>
<div
className="es-grab"
aria-hidden="true"
onPointerDown={onPointerDown}
onPointerMove={onPointerMove}
onPointerUp={endDrag}
onPointerCancel={endDrag}
>
<span />
<i />
<i />
</div>
<header className="es-head">
<div>
<h2 id={`${uid}-title`}>{title}</h2>
{description ? <p id={`${uid}-description`}>{description}</p> : null}
</div>
<button
ref={closeRef}
type="button"
className="es-close"
aria-label="Close sheet"
onClick={close}
>
<svg viewBox="0 0 20 20" aria-hidden="true" focusable="false">
<path
d="M5 5l10 10M15 5L5 15"
fill="none"
stroke="currentColor"
strokeWidth="1.6"
strokeLinecap="round"
/>
</svg>
</button>
</header>
<div className="es-body">{children}</div>
</div>
</div>,
document.body,
);
}
components/lab/elastic-sheet/elastic-sheet.css— press enter to show or hide the source
css
.es-root {
--es-ink: #23252a;
--es-muted: #666a72;
--es-line: rgba(35, 37, 42, 0.12);
--es-surface: #fffdf8;
--es-soft: #f4f1e9;
--es-accent: #a3602a;
--es-focus: #23252a;
--es-ease: cubic-bezier(0.16, 1, 0.3, 1);
position: fixed;
inset: 0;
z-index: 80;
visibility: hidden;
pointer-events: none;
transition: visibility 0s linear 480ms;
color: var(--es-ink);
font: inherit;
}

.es-root[data-visible="true"] {
visibility: visible;
pointer-events: auto;
transition: visibility 0s linear 0s;
}

.es-backdrop {
position: absolute;
inset: 0;
background: rgba(25, 23, 18, 0.34);
opacity: 0;
transition: opacity 220ms ease;
}

.es-root[data-visible="true"] .es-backdrop {
opacity: 1;
}

.es-panel {
position: absolute;
top: 18px;
right: 18px;
bottom: 18px;
display: flex;
flex-direction: column;
width: min(430px, calc(100vw - 36px));
max-height: calc(100dvh - 36px);
overflow: hidden;
border: 1px solid var(--es-line);
border-radius: 16px;
background: var(--es-surface);
box-shadow:
0 1px 2px rgba(25, 23, 18, 0.08),
0 34px 80px -40px rgba(25, 23, 18, 0.55);
transform: translate3d(calc(100% + 28px), 0, 0);
opacity: 0;
transition:
transform 420ms var(--es-ease),
opacity 180ms ease;
}

.es-root[data-size="wide"] .es-panel {
width: min(540px, calc(100vw - 36px));
}

.es-root[data-visible="true"] .es-panel {
transform: translate3d(0, 0, 0);
opacity: 1;
}

.es-panel:focus {
outline: none;
}

.es-grab {
display: none;
}

.es-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 16px;
padding: 24px 24px 18px;
}

.es-head h2 {
margin: 0;
font-size: 1.25rem;
font-weight: 600;
letter-spacing: -0.02em;
line-height: 1.2;
}

.es-head p {
margin: 6px 0 0;
color: var(--es-muted);
font-size: 0.8125rem;
line-height: 1.5;
}

.es-close {
flex: none;
display: grid;
place-items: center;
width: 36px;
height: 36px;
margin: -4px -4px 0 0;
border: 1px solid transparent;
border-radius: 9px;
background: transparent;
color: var(--es-muted);
cursor: pointer;
transition:
color 150ms ease,
background-color 150ms ease,
border-color 150ms ease;
}

.es-close:hover {
color: var(--es-ink);
background: var(--es-soft);
border-color: var(--es-line);
}

.es-close:focus-visible {
outline: 2px solid var(--es-focus);
outline-offset: 2px;
}

.es-close svg {
width: 18px;
height: 18px;
}

.es-body {
flex: 1;
min-height: 0;
overflow-y: auto;
overscroll-behavior: contain;
padding: 0 24px 24px;
border-top: 1px solid var(--es-line);
scrollbar-width: thin;
}

@media (max-width: 640px) {
.es-panel {
top: auto;
right: 0;
bottom: 0;
left: 0;
width: 100%;
max-height: min(88dvh, 760px);
border-radius: 20px 20px 0 0;
border-bottom: 0;
transform: translate3d(0, calc(100% + 12px), 0);
transition:
transform 460ms var(--es-ease),
opacity 180ms ease;
}

.es-root[data-size="wide"] .es-panel {
width: 100%;
}

.es-root[data-visible="true"] .es-panel {
transform: translate3d(0, 0, 0);
}

.es-grab {
display: grid;
justify-items: center;
gap: 3px;
padding: 10px 0 4px;
touch-action: none;
cursor: grab;
}

.es-grab span {
width: 38px;
height: 4px;
border-radius: 999px;
background: rgba(35, 37, 42, 0.2);
}

.es-grab i {
display: block;
width: 16px;
height: 1px;
background: rgba(35, 37, 42, 0.1);
}

.es-head {
padding: 10px 20px 16px;
}

.es-body {
padding: 0 20px 20px;
}
}

@media (prefers-reduced-motion: reduce) {
.es-panel,
.es-backdrop {
transition-duration: 0.01ms;
}

.es-root[data-visible="true"] .es-panel {
transform: none;
}
}