Drawer
A sheet of paper dragged in from an edge of the screen and flicked away again.
"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>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ drawer"use client";
import { createContext, useContext, useMemo, type ComponentProps } from "react";
import { Drawer as DrawerPrimitive } from "@base-ui/react/drawer";
import { cn } from "@/lib/utils";
import { penStyle, useInkSeed, usePen, type Pen } from "@/hooks/use-ink-box";
import { inkClassName, InkSvg, Stroke } from "@/lib/ink";
import { InkPanel } from "@/lib/ink-panel";
import { lineStroke } from "@/lib/ink-sketch";
type Direction = "top" | "right" | "bottom" | "left";
const swipe = { top: "up", right: "right", bottom: "down", left: "left" } as const;
const DrawerContext = createContext<Direction>("bottom");
/**
* A panel dragged in from an edge of the screen, and flicked away again.
* `direction` is the edge it comes from; it follows your finger as you drag
* it, and goes if you let go past halfway or flick it.
*/
function Drawer({ direction = "bottom", ...props }: Omit<DrawerPrimitive.Root.Props, "swipeDirection"> & { direction?: Direction }) {
return (
<DrawerContext value={direction}>
<DrawerPrimitive.Root data-slot="drawer" swipeDirection={swipe[direction]} {...props} />
</DrawerContext>
);
}
function DrawerTrigger({ ...props }: DrawerPrimitive.Trigger.Props) {
return <DrawerPrimitive.Trigger data-slot="drawer-trigger" {...props} />;
}
function DrawerPortal({ ...props }: DrawerPrimitive.Portal.Props) {
return <DrawerPrimitive.Portal data-slot="drawer-portal" {...props} />;
}
function DrawerClose({ ...props }: DrawerPrimitive.Close.Props) {
return <DrawerPrimitive.Close data-slot="drawer-close" {...props} />;
}
/** Tracing paper over the page; it clears as the drawer is dragged away. */
function DrawerOverlay({ className, ...props }: DrawerPrimitive.Backdrop.Props) {
return (
<DrawerPrimitive.Backdrop
data-slot="drawer-overlay"
className={inkClassName(
"fixed inset-0 z-50 min-h-dvh bg-paper/65 opacity-[calc(1-var(--drawer-swipe-progress,0))] backdrop-blur-[1.5px] transition-opacity duration-(--dur-state) ease-out",
"data-swiping:duration-0 data-ending-style:opacity-0 data-ending-style:ease-in data-starting-style:opacity-0 supports-[-webkit-touch-callout:none]:absolute",
className,
)}
{...props}
/>
);
}
const viewportClass: Record<Direction, string> = {
bottom: "items-end justify-center",
top: "items-start justify-center",
right: "items-stretch justify-end",
left: "items-stretch justify-start",
};
// The popup runs past the screen's edge by --bleed, so dragging it further
// than it can go never shows a gap, and its drawn corners stay off screen.
const popupClass: Record<Direction, string> = {
bottom:
"w-full max-h-[85dvh] -mb-(--bleed) pb-(--bleed) [transform:translateY(var(--drawer-swipe-movement-y))] data-starting-style:[transform:translateY(calc(100%-var(--bleed)))] data-ending-style:[transform:translateY(calc(100%-var(--bleed)))]",
top: "w-full max-h-[85dvh] -mt-(--bleed) pt-(--bleed) [transform:translateY(var(--drawer-swipe-movement-y))] data-starting-style:[transform:translateY(calc(-100%+var(--bleed)))] data-ending-style:[transform:translateY(calc(-100%+var(--bleed)))]",
right:
"h-full w-[min(24rem,calc(100vw-3rem))] -mr-(--bleed) pr-(--bleed) [transform:translateX(var(--drawer-swipe-movement-x))] data-starting-style:[transform:translateX(calc(100%-var(--bleed)))] data-ending-style:[transform:translateX(calc(100%-var(--bleed)))]",
left: "h-full w-[min(24rem,calc(100vw-3rem))] -ml-(--bleed) pl-(--bleed) [transform:translateX(var(--drawer-swipe-movement-x))] data-starting-style:[transform:translateX(calc(-100%+var(--bleed)))] data-ending-style:[transform:translateX(calc(-100%+var(--bleed)))]",
};
/** A sheet of paper dragged in from an edge, pencilled round and lifted off its shadow. */
function DrawerContent({
className,
children,
seed,
roughness,
passes,
radius,
corners,
shadow,
draw,
weight,
speed,
...props
}: DrawerPrimitive.Popup.Props & Omit<Pen, "fill"> & { seed?: string | number }) {
const direction = useContext(DrawerContext);
const pen = usePen({ roughness, passes, radius, corners, shadow: shadow ?? "none", draw, weight, speed });
return (
<DrawerPortal>
<DrawerOverlay />
<DrawerPrimitive.Viewport data-slot="drawer-viewport" className={cn("fixed inset-0 z-50 flex overflow-hidden", viewportClass[direction])}>
<DrawerPrimitive.Popup
data-slot="drawer-content"
data-direction={direction}
className={inkClassName(
"group/drawer-content ink-paper relative isolate flex touch-auto flex-col overflow-y-auto overscroll-contain text-base text-popover-foreground outline-none [--bleed:3rem]",
"transition-transform duration-(--dur-enter) ease-out-expo data-swiping:duration-0 data-swiping:select-none data-ending-style:duration-(--dur-state) data-ending-style:ease-in",
"motion-reduce:transition-none supports-[-webkit-touch-callout:none]:[--bleed:0px]",
popupClass[direction],
className,
)}
{...props}
>
<InkPanel pen={pen} seed={seed} estimate={direction === "left" || direction === "right" ? [384, 640] : [640, 320]} offset={0} maxRadius={18} passes={2} className="text-ink-line" />
{direction === "bottom" && <DrawerHandle seed={seed} />}
<DrawerPrimitive.Content className="relative flex flex-1 flex-col">{children}</DrawerPrimitive.Content>
</DrawerPrimitive.Popup>
</DrawerPrimitive.Viewport>
</DrawerPortal>
);
}
/** A short pen line at the top of a bottom drawer, to show it can be dragged. */
function DrawerHandle({ seed }: { seed?: string | number }) {
const pen = usePen({});
const s = useInkSeed(seed === undefined ? undefined : `${seed}-handle`);
const d = useMemo(() => lineStroke(s, [0, 3], [56, 3.4], { bow: 0.8, jitter: 0.3 }), [s]);
return (
<span aria-hidden="true" data-slot="drawer-handle" className="relative mx-auto mt-3 block h-1.5 w-14 shrink-0 text-ink-4">
<InkSvg box={[0, 0, 56, 6]} stretch className="inset-0 size-full" style={penStyle(pen)}>
<Stroke d={d} draw="mount" duration={380} width={2.4} />
<Stroke d={lineStroke(s + 1, [3, 3.6], [53, 3.2], { bow: 0.6, jitter: 0.3 })} draw="mount" delay={160} duration={320} width={1.2} opacity={0.6} />
</InkSvg>
</span>
);
}
function DrawerHeader({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="drawer-header" className={cn("flex flex-col gap-1.5 p-5 text-center group-data-[direction=left]/drawer-content:text-left group-data-[direction=right]/drawer-content:text-left sm:text-left", className)} {...props} />;
}
function DrawerFooter({ className, ...props }: ComponentProps<"div">) {
return <div data-slot="drawer-footer" className={cn("mt-auto flex flex-col gap-3 p-5", className)} {...props} />;
}
function DrawerTitle({ className, ...props }: DrawerPrimitive.Title.Props) {
return <DrawerPrimitive.Title data-slot="drawer-title" className={inkClassName("text-xl leading-snug font-bold", className)} {...props} />;
}
function DrawerDescription({ className, ...props }: DrawerPrimitive.Description.Props) {
return <DrawerPrimitive.Description data-slot="drawer-description" className={inkClassName("text-base text-ink-3", className)} {...props} />;
}
export { Drawer, DrawerPortal, DrawerOverlay, DrawerTrigger, DrawerClose, DrawerContent, DrawerHeader, DrawerFooter, DrawerTitle, DrawerDescription };Usage
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>Examples
Directions
A drawer from each edge of the screen.
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.
"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>
);
}API reference
- directiondefault
"bottom" "top" | "right" | "bottom" | "left"The edge it comes from, and the way you drag it to put it away.
- open / defaultOpendefault
false booleanOpen or closed, controlled or not.
- snapPoints
(number | string)[]Heights a bottom drawer rests at. Passed through to Base UI.
- DrawerContent seed
string | numberPins the drawing.
Every other prop goes to Base UI's Drawer.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, shadow, weight and speed, as props or from the nearest InkProvider. What each one does.