Drawer

A sheet from the bottom edge you can drag, flick and snap.

Three effects, and only three

Installation

Terminal
npx shadcn@latest add https://fabrickit.org/r/drawer.json

This 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.

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 the
lib/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 content

Usage

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

PropTypeDefault
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.

number0
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) }—