Drawer
A sheet from the bottom edge you can drag, flick and snap.
ShareSend to
Installation
Terminal
npx shadcn@latest add https://fabrickit.org/r/drawer.jsonTerminal
pnpm dlx shadcn@latest add https://fabrickit.org/r/drawer.jsonTerminal
yarn shadcn@latest add https://fabrickit.org/r/drawer.jsonTerminal
bunx --bun shadcn@latest add https://fabrickit.org/r/drawer.jsonThis copies the component into components/fabricui and its engine into lib/fabricui. It's your code from then on. First time? Add the theme once: see Installation.
Step 1: Add the theme (once)
Every colour, radius and curve the components use, as --fabric-* variables. Save it as fabric.css next to your global CSS and import it there. If you've added another Fabric Kit component before, skip this.globals.css@import "./fabric.css";fabric.css/* * fabric.css — Fabric Kit's theme: every colour, radius, ring and curve the * components use, as --fabric-* variables (prefixed, so they never clash * with your own shadcn theme). Installed by `shadcn add @fabrickit/theme` * (it lands in your global CSS) — or import this file once, globally. * Dark by default. For light, put data-theme="light" on <html> (or on any * element: the tokens follow it down the tree; data-theme="dark" turns back). */ :root, [data-theme="dark"] { --fabric-page: #111111; --fabric-surface: #191919; /* triggers, recessed tiles */ --fabric-panel: #202020; /* open panels */ --fabric-raised: #2a2a2a; /* hover, range band */ --fabric-raised-hi: #343434; /* hover inside a band */ --fabric-preview: #252525; /* soft preview fills */ --fabric-text: #e0e0e0;Step 2: Copy the shared engines
Plain JavaScript that does the motion, shared between components. Create each file at the same path in your project; if a file is already there from another component, keep it.lib/fabricui/mount.js/* * mount.js — runs a Fabric Kit engine for a React component and cleans up * after it. The engines are plain JavaScript that wire themselves to the page * (listeners on the document, resize observers, a few elements of their own); * mount() records everything an engine attaches and changes while it's being * set up, and dispose() takes it all back, so a component can come and go (or * be set up twice in a row, as React's Strict Mode does) without leaving * anything behind. * * const m = mount(() => new Engine(root, options)); * m.instance; // the engine * m.dispose(); // on unmount: listeners and observers off, the DOM as it was, then instance.destroy() * // (and destroy() on anything it owns that registered itself, like each ClothDrop) */ export function mount(create) { const ac = new AbortController();lib/fabricui/cloth-drop.jsimport { Dither } from "./dither"; /** * cloth-drop.js — the library's open/close motion, shared by every component * that drops open (calendar, accordion, …). * * ClothDrop: the open/close motion. * * Open is a dropped cloth: the panel falls under gravity from behind the * trigger (the calendar rides its bottom edge), the top edge catches, the * fabric stretches a touch and springs back, a wave runs up from the hem and * the drape folds and sway it was thrown into settle. * * The falling cloth is one finely subdivided WebGL mesh, textured with the * browser's own rendering of the calendar (the real HTML + CSS + Geist, drawn * through an SVG foreignObject), so its text is pixel-identical to the page.lib/fabricui/dither.js/* * dither.js — Fabric Kit's dither, in one place. Every dithered thing (cloth * fold shade, shimmer, weaves, chart fills) uses this 4×4 ordered (Bayer) * pattern, so dots line up and read as one texture across components. * * Dither.B4 the 4×4 matrix, 0–15 * Dither.T4 its thresholds, centred: (v + 0.5) / 16 * Dither.at(x, y) the threshold for cell (x, y) * Dither.GLSL the same pattern for shaders: bayer4(cell) → 0–1 */ const B4 = [0, 8, 2, 10, 12, 4, 14, 6, 3, 11, 1, 9, 15, 7, 13, 5]; const T4 = B4.map(v => v / 16 + 1 / 32); export const Dither = { B4, T4,Step 3: Copy the Drawer files
The component itself. Create each one at the same path in your project. It's your code from here on.components/fabricui/drawer.tsx"use client"; import * as React from "react"; import { mount } from "@/lib/fabricui/mount"; import { Drawer as Engine } from "@/lib/fabricui/drawer-engine"; import "./drawer.css"; const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect; export type DrawerHandle = { open: () => void; close: () => void; snapTo: (stop: number) => void }; export type DrawerProps = { /** What's in it. */ children: React.ReactNode; /** Its name, for screen readers. */ label: string;components/fabricui/drawer.css/* * Drawer — a sheet from the bottom edge that you drag. Shared by every page * with one; drawer.js drives it (class Drawer). * * <div class="drawer" data-modal> fills its container (a screen) * <div class="drawer__scrim"></div> * <div class="cloth drawer__cloth" aria-hidden="true"><canvas></canvas></div> * <div class="drawer__sheet" role="dialog" aria-label="…"> * <button class="drawer__handle" aria-label="…"></button> * <div class="drawer__content">…</div> * </div> * </div> * * It follows the finger 1:1, rubber-bands past its ends, and settles on a * spring at the nearest stop (the throw counts). Effects: * - fabric: a modal drawer opens as a length of cloth pulled up from thelib/fabricui/drawer-engine.js/* * drawer-engine.js — a bottom sheet you drag (see drawer.css). From drawer.js. * Uses cloth-drop.js for the modal drawer's fabric entrance. * Changes for React: the "when was the pointer last pressed" listener is added * by each drawer (not when the file loads, so it can be imported on a server), * onOpen is told when it opens, and destroy() stops its motion. * * // modal: as tall as its content, opens over a scrim, drag or flick down to close * const share = new Drawer(el, { onClose }); * share.open(); share.close(); * * // stops: always there, rests at one of several heights (px shown; "max" = all) * const places = new Drawer(el, { stops: [92, 330, "max"], start: 0, onChange: i => … }); * places.snapTo(1); * * Dragging follows the finger 1:1 (the handle, the header, or the contentStep 4: Check the import paths
The files import each other through the @/ alias (the shadcn default, there in a new Next.js app). If your project doesn't have it, add it to tsconfig.json, or change the imports to relative paths.tsconfig.json{ "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["./*"] } } }Step 5: Use it
Import it from @/components/fabricui/drawer. The Usage example below shows it in a page.
Code
components/fabricui/drawer.tsx
"use client";
import * as React from "react";
import { mount } from "@/lib/fabricui/mount";
import { Drawer as Engine } from "@/lib/fabricui/drawer-engine";
import "./drawer.css";
const useIsoLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
export type DrawerHandle = { open: () => void; close: () => void; snapTo: (stop: number) => void };
export type DrawerProps = {
/** What's in it. */
children: React.ReactNode;
/** Its name, for screen readers. */
label: string;components/fabricui/drawer.css
/*
* Drawer — a sheet from the bottom edge that you drag. Shared by every page
* with one; drawer.js drives it (class Drawer).
*
* <div class="drawer" data-modal> fills its container (a screen)
* <div class="drawer__scrim"></div>
* <div class="cloth drawer__cloth" aria-hidden="true"><canvas></canvas></div>
* <div class="drawer__sheet" role="dialog" aria-label="…">
* <button class="drawer__handle" aria-label="…"></button>
* <div class="drawer__content">…</div>
* </div>
* </div>
*
* It follows the finger 1:1, rubber-bands past its ends, and settles on a
* spring at the nearest stop (the throw counts). Effects:
* - fabric: a modal drawer opens as a length of cloth pulled up from thelib/fabricui/drawer-engine.js
/*
* drawer-engine.js — a bottom sheet you drag (see drawer.css). From drawer.js.
* Uses cloth-drop.js for the modal drawer's fabric entrance.
* Changes for React: the "when was the pointer last pressed" listener is added
* by each drawer (not when the file loads, so it can be imported on a server),
* onOpen is told when it opens, and destroy() stops its motion.
*
* // modal: as tall as its content, opens over a scrim, drag or flick down to close
* const share = new Drawer(el, { onClose });
* share.open(); share.close();
*
* // stops: always there, rests at one of several heights (px shown; "max" = all)
* const places = new Drawer(el, { stops: [92, 330, "max"], start: 0, onChange: i => … });
* places.snapTo(1);
*
* Dragging follows the finger 1:1 (the handle, the header, or the contentUsage
Usage
"use client";
import * as React from "react";
import { Drawer } from "@/components/fabricui/drawer";
import { Button } from "@/components/fabricui/button";
// it rises from the bottom of its nearest positioned ancestor
export function Share() {
const [open, setOpen] = React.useState(false);
return (
<div style={{ position: "relative", height: 480 }}>
<Button onClick={() => setOpen(true)}>Share</Button>
<Drawer label="Share" open={open} onOpenChange={setOpen} width={440}>
<p>Send to…</p>
</Drawer>
</div>
);
}
// or always there, resting at one of several heights
export function Places() {
return <Drawer label="Places" stops={[92, 330, "max"]}>…</Drawer>;
}Props
| Prop | Type | Default |
|---|---|---|
| label Its name, for screen readers. | string | — |
| children What's in it. Taller than the space, it scrolls (at full height). | ReactNode | — |
| open Modal: open or closed. It opens as cloth over a scrim; drag or flick it down, press the handle, Escape or the scrim to close. | boolean | — |
| onOpenChange Called when it opens or closes. | (open: boolean) => void | — |
| stops Instead of modal: always there, resting at one of these heights (px showing, lowest first; "max" is all of it). | (number | "max")[] | — |
| start Stops: which one it starts at. | number | 0 |
| onStopChange Stops: called when it settles at another. | (stop: number) => void | — |
| width How wide, centred (all the width by default). | number | string | — |
| ref Drive it from code. | { open(), close(), snapTo(i) } | — |