# Drawer

A sheet of paper dragged in from an edge of the screen and flicked away again.

Docs: https://ballpoint.st9wd.com/docs/drawer

```bash
npx shadcn@latest add @ballpoint/drawer
```

```tsx
import { Drawer, DrawerContent, DrawerDescription, DrawerHeader, DrawerTitle, DrawerTrigger } from "@/components/ui/drawer"

<Drawer>
  <DrawerTrigger>Open</DrawerTrigger>
  <DrawerContent>
    <DrawerHeader>
      <DrawerTitle>Move goal</DrawerTitle>
      <DrawerDescription>Set your daily goal.</DrawerDescription>
    </DrawerHeader>
  </DrawerContent>
</Drawer>
```

Props:

- `direction`: `"top" | "right" | "bottom" | "left"` (default `"bottom"`). The edge it comes from, and the way you drag it to put it away.
- `open / defaultOpen`: `boolean` (default `false`). Open or closed, controlled or not.
- `snapPoints`: `(number | string)[]`. Heights a bottom drawer rests at. Passed through to Base UI.
- `DrawerContent seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `passes`, `radius`, `corners`, `shadow`, `weight`, `speed` (see Pen settings).

Every other prop goes to Base UI's Drawer: https://base-ui.com/react/components/drawer

## Example

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger } from "@/components/ui/drawer";
import { InkIcon } from "@/components/ui/ink-icons";

export default function DrawerDemo() {
  const [goal, setGoal] = useState(350);
  const adjust = (by: number) => setGoal((g) => Math.min(400, Math.max(200, g + by)));
  return (
    <Drawer>
      <DrawerTrigger render={<Button variant="outline" seed="drawer-open" />}>Open drawer</DrawerTrigger>
      <DrawerContent seed="drawer" className="mx-auto max-w-sm">
        <DrawerHeader>
          <DrawerTitle>Move goal</DrawerTitle>
          <DrawerDescription>Set your daily calorie goal.</DrawerDescription>
        </DrawerHeader>
        <div className="flex items-center justify-center gap-6 px-5 py-4">
          <Button variant="outline" size="icon" aria-label="Decrease" onClick={() => adjust(-10)} disabled={goal <= 200} seed="drawer-minus">
            <InkIcon name="minus" />
          </Button>
          <div className="text-center">
            <div className="text-5xl font-bold tabular-nums">{goal}</div>
            <div className="text-sm text-ink-3 uppercase">Calories / day</div>
          </div>
          <Button variant="outline" size="icon" aria-label="Increase" onClick={() => adjust(10)} disabled={goal >= 400} seed="drawer-plus">
            <InkIcon name="plus" />
          </Button>
        </div>
        <DrawerFooter>
          <Button seed="drawer-submit">Submit</Button>
          <DrawerClose render={<Button variant="outline" seed="drawer-cancel" />}>Cancel</DrawerClose>
        </DrawerFooter>
      </DrawerContent>
    </Drawer>
  );
}
```

## Directions

A drawer from each edge of the screen.

```tsx
import { Button } from "@/components/ui/button";
import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger } from "@/components/ui/drawer";

const sides = ["top", "right", "bottom", "left"] as const;

export default function DrawerSides() {
  return (
    <div className="flex flex-wrap gap-4">
      {sides.map((side) => (
        <Drawer key={side} direction={side}>
          <DrawerTrigger render={<Button variant="outline" seed={`ds-${side}`} />} className="capitalize">
            {side}
          </DrawerTrigger>
          <DrawerContent seed={`ds-content-${side}`}>
            <DrawerHeader>
              <DrawerTitle>From the {side}</DrawerTitle>
              <DrawerDescription>Drag it back toward the edge it came from, or flick it away.</DrawerDescription>
            </DrawerHeader>
            <DrawerFooter>
              <DrawerClose render={<Button variant="outline" seed={`ds-close-${side}`} />}>Close</DrawerClose>
            </DrawerFooter>
          </DrawerContent>
        </Drawer>
      ))}
    </div>
  );
}
```

## With a form

A side drawer for something longer than a confirmation.

```tsx
"use client";

import { Button } from "@/components/ui/button";
import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger } from "@/components/ui/drawer";
import { Field, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";

export default function DrawerForm() {
  return (
    <Drawer direction="right">
      <DrawerTrigger render={<Button seed="df-open" />}>Write a note</DrawerTrigger>
      <DrawerContent seed="df">
        <DrawerHeader>
          <DrawerTitle>A note for the margin</DrawerTitle>
          <DrawerDescription>It is saved to the page you are on.</DrawerDescription>
        </DrawerHeader>
        <form className="flex flex-col gap-4 px-5" onSubmit={(e) => e.preventDefault()}>
          <Field>
            <FieldLabel htmlFor="drawer-title">Title</FieldLabel>
            <Input id="drawer-title" placeholder="Remember to…" seed="df-title" />
          </Field>
          <Field>
            <FieldLabel htmlFor="drawer-body">Note</FieldLabel>
            <Textarea id="drawer-body" rows={5} seed="df-body" />
          </Field>
        </form>
        <DrawerFooter>
          <Button seed="df-save">Save note</Button>
          <DrawerClose render={<Button variant="outline" seed="df-cancel" />}>Cancel</DrawerClose>
        </DrawerFooter>
      </DrawerContent>
    </Drawer>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `corners`: `"crossed" | "joined"` (default `"crossed"`). Square corners: sides pulled separately past each other, or the box drawn in one motion.
- `shadow`: `"hatch" | "solid" | "none"` (default `"hatch"`). What a lifted box leaves on the paper. "none" turns the lift off too.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
