Drawer
A sheet from the side or the bottom.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/, installsreact-domand the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add drawerOr install the package
Don’t want the file? Import from
asta-ui/drawerand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { Drawer } from "asta-ui/drawer";Or copy it by hand
Install
react-dom, then copy this file. Keep the paths, or fix the@/imports. Then add the tokens fromglobals.css."use client"; import { useEffect, useId, useLayoutEffect, useRef, useState, type KeyboardEvent, type PointerEvent, type ReactNode } from "react"; import { createPortal } from "react-dom"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); const EASE = "cubic-bezier(.16,1,.3,1)"; const FOCUSABLE = 'a[href],button:not([disabled]),input:not([disabled]),select:not([disabled]),textarea:not([disabled]),[tabindex]:not([tabindex="-1"])'; export type DrawerSide = "right" | "left" | "bottom"; export interface DrawerProps { open: boolean; onOpenChange: (open: boolean) => void; /** Which edge the sheet slides in from. */ side?: DrawerSide; title: ReactNode; description?: ReactNode; children?: ReactNode; /** Buttons row pinned to the bottom of the sheet. */ footer?: ReactNode; /** Keep the overlay inside the nearest positioned parent instead of the whole viewport. */ contained?: boolean; /** Bottom sheets can be dragged down to close. Set false to turn that off. */ draggable?: boolean; className?: string; } const still = () => typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches; const offscreen = (side: DrawerSide) => side === "right" ? "translateX(calc(100% + 16px))" : side === "left" ? "translateX(calc(-100% - 16px))" : "translateY(calc(100% + 16px))"; /** * A side sheet that slides in from the right, the left or the bottom over a fading overlay. Escape or a click * on the overlay closes it, Tab stays inside while it's open, and focus goes back to whatever opened it. * Bottom sheets have a handle you can drag down to dismiss. Put `data-autofocus` on the field that should * take focus first. */ export function Drawer({ open, onOpenChange, side = "right", title, description, children, footer, contained = false, draggable = true, className }: DrawerProps) { // Stays mounted through the exit animation, then unmounts itself. const [mounted, setMounted] = useState(open); if (open && !mounted) setMounted(true); const uid = useId(); const panel = useRef<HTMLDivElement>(null); const overlay = useRef<HTMLDivElement>(null); const opener = useRef<HTMLElement | null>(null); const drag = useRef<{ y: number; t: number; dy: number; v: number; h: number } | null>(null); // Enter: slide in and take focus. Layout effect, so the active element is still the opener. useLayoutEffect(() => { const p = panel.current; if (!open || !p) return; if (!p.contains(document.activeElement)) opener.current = document.activeElement as HTMLElement | null; const first = p.querySelector<HTMLElement>("[data-autofocus]") ?? p.querySelector<HTMLElement>(`[data-body] ${FOCUSABLE}`) ?? p; first.focus({ preventScroll: true }); // Reopened mid-exit: drop the exit animation and any leftover drag offset. for (const el of [p, overlay.current]) { el?.getAnimations().forEach((a) => a.cancel()); el?.style.removeProperty(el === p ? "transform" : "opacity"); } if (still()) return; p.animate([{ transform: offscreen(side) }, { transform: "none" }], { duration: 560, easing: EASE }); overlay.current?.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 360, easing: EASE }); }, [open, side]); // Exit: slide out from wherever the sheet is (a drag may have moved it), then unmount and return focus. useEffect(() => { if (open || !mounted) return; const p = panel.current; if (!p) return; const from = getComputedStyle(p).transform; const ms = still() ? 0 : 340; overlay.current?.animate([{ opacity: getComputedStyle(overlay.current).opacity }, { opacity: 0 }], { duration: ms, easing: EASE, fill: "forwards" }); const a = p.animate([{ transform: from === "none" ? "none" : from }, { transform: offscreen(side) }], { duration: ms, easing: EASE, fill: "forwards" }); a.onfinish = () => { setMounted(false); opener.current?.focus({ preventScroll: true }); opener.current = null; }; return () => { a.onfinish = null; }; }, [open, mounted, side]); // Lock page scroll while a viewport drawer is up. useEffect(() => { if (!mounted || contained) return; const prev = document.body.style.overflow; document.body.style.overflow = "hidden"; return () => { document.body.style.overflow = prev; }; }, [mounted, contained]); const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") { e.stopPropagation(); onOpenChange(false); return; } if (e.key !== "Tab" || !panel.current) return; const items = [...panel.current.querySelectorAll<HTMLElement>(FOCUSABLE)].filter((el) => el.offsetParent !== null || el === document.activeElement); if (!items.length) return e.preventDefault(); const first = items[0]; const last = items[items.length - 1]; if (e.shiftKey && (document.activeElement === first || document.activeElement === panel.current)) { e.preventDefault(); last.focus(); } else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); } }; // Drag to close (bottom sheet): the sheet and the overlay follow the pointer, no re-renders. const canDrag = side === "bottom" && draggable; const onDragStart = (e: PointerEvent<HTMLDivElement>) => { if (!canDrag || e.button !== 0 || !open) return; e.currentTarget.setPointerCapture(e.pointerId); // Measure the sheet once per drag; moves only write transform and opacity. drag.current = { y: e.clientY, t: e.timeStamp, dy: 0, v: 0, h: panel.current?.offsetHeight || 1 }; panel.current?.getAnimations().forEach((a) => a.cancel()); }; const onDragMove = (e: PointerEvent<HTMLDivElement>) => { const d = drag.current; const p = panel.current; if (!d || !p) return; const raw = e.clientY - d.y; // Pulling up resists, pulling down follows 1:1. const dy = raw < 0 ? -Math.sqrt(-raw) * 2 : raw; d.v = (dy - d.dy) / Math.max(1, e.timeStamp - d.t); d.t = e.timeStamp; d.dy = dy; p.style.transform = `translateY(${dy}px)`; if (overlay.current) overlay.current.style.opacity = String(Math.max(0, 1 - Math.max(0, dy) / d.h)); }; const onDragEnd = () => { const d = drag.current; const p = panel.current; drag.current = null; if (!d || !p) return; if (d.dy > d.h * 0.3 || d.v > 0.6) return onOpenChange(false); const from = p.style.transform; p.style.transform = ""; if (overlay.current) overlay.current.style.opacity = ""; if (!still()) p.animate([{ transform: from || "none" }, { transform: "none" }], { duration: 420, easing: EASE }); }; if (!mounted || typeof document === "undefined") return null; const bottom = side === "bottom"; const layer = ( <div className={cx(contained ? "absolute" : "fixed", "inset-0 z-50 overflow-hidden", contained && "rounded-[inherit]")}> <div ref={overlay} aria-hidden onClick={() => onOpenChange(false)} className="absolute inset-0 bg-[color-mix(in_srgb,var(--color-ink)_28%,transparent)] will-change-[opacity]" /> <div ref={panel} role="dialog" aria-modal="true" aria-labelledby={`${uid}-title`} aria-describedby={description ? `${uid}-desc` : undefined} tabIndex={-1} onKeyDown={onKey} className={cx( "absolute flex flex-col overflow-hidden rounded-[14px] bg-paper outline-none will-change-transform", "shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_8%,transparent),0_24px_64px_-24px_color-mix(in_srgb,var(--color-ink)_45%,transparent)]", side === "right" && "inset-y-2 right-2 w-[min(400px,calc(100%-48px))]", side === "left" && "inset-y-2 left-2 w-[min(400px,calc(100%-48px))]", bottom && "inset-x-2 bottom-2 mx-auto max-h-[calc(100%-56px)] max-w-[520px]", className, )} > {bottom && ( <div onPointerDown={onDragStart} onPointerMove={onDragMove} onPointerUp={onDragEnd} onPointerCancel={onDragEnd} aria-hidden className={cx("flex shrink-0 justify-center pb-1 pt-2.5 [touch-action:none]", canDrag && "cursor-grab active:cursor-grabbing")} > <span className="h-1 w-9 rounded-full bg-[color-mix(in_srgb,var(--color-ink)_16%,transparent)]" /> </div> )} <div className={cx("flex shrink-0 items-start justify-between gap-4 px-5", bottom ? "pt-2" : "pt-5")}> <div className="min-w-0"> <h2 id={`${uid}-title`} className="text-[16px] font-medium tracking-[-0.02em] text-ink"> {title} </h2> {description && ( <p id={`${uid}-desc`} className="mt-1 text-[13.5px] leading-relaxed text-mute"> {description} </p> )} </div> <button type="button" aria-label="Close" onClick={() => onOpenChange(false)} className="-mr-1.5 -mt-1 flex h-8 w-8 shrink-0 items-center justify-center rounded-lg text-mute outline-none transition-colors duration-200 hover:bg-paper-2 hover:text-ink focus-visible:ring-2 focus-visible:ring-brand/50" > <svg viewBox="0 0 24 24" className="h-3.5 w-3.5" fill="none" stroke="currentColor" strokeWidth="2.55" strokeLinecap="round" strokeLinejoin="round" aria-hidden> <path d="m6 6 12 12" /> <path d="M18 6 6 18" /> </svg> </button> </div> <div data-body className="min-h-0 flex-1 overflow-y-auto overscroll-contain px-5 pb-5 pt-4"> {children} </div> {footer && <div className="flex shrink-0 flex-col-reverse gap-2 px-5 pb-5 sm:flex-row sm:justify-end">{footer}</div>} </div> </div> ); return contained ? layer : createPortal(layer, document.body); } export default Drawer;Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Drawer component to my project. A sheet from the side or the bottom. It includes: right, left, bottom, drag to close, focus trap, Escape, contained. Install it: ```sh pnpm dlx asta-ui@latest add drawer --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/drawer.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { Button } from "@/components/astabyte/button"; import { Drawer, type DrawerSide } from "@/components/astabyte/drawer"; ``` Usage example and props: https://ui.astabyte.com/drawer Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Or use plain HTML + CSS
Not on React? Use it from Django, Rails, Laravel, Go
html/template, htmx or a static page. Loadasta.cssandasta.jsonce (thehead.htmltab), then paste the markup. Theui.astabyte.com/cdnlink is temporary: pin a version from jsDelivr for production. More in HTML + CSS.<!-- Needs asta.js: data-asta-open opens the drawer, data-asta-close/Escape/backdrop close it, the handle drags the bottom sheet. --> <div style="display:flex;flex-wrap:wrap;gap:12px;align-items:center"> <button class="asta-btn asta-btn-outline" data-asta-open="ex-drawer-right"><span class="asta-btn-label">Open right</span></button> <button class="asta-btn asta-btn-outline" data-asta-open="ex-drawer-left"><span class="asta-btn-label">Open left</span></button> <button class="asta-btn asta-btn-outline" data-asta-open="ex-drawer-bottom"><span class="asta-btn-label">Bottom sheet</span></button> <dialog id="ex-drawer-right" class="asta-dialog asta-drawer" data-side="right" aria-labelledby="ex-drawer-right-title" aria-describedby="ex-drawer-right-desc"> <div class="asta-drawer-inner"> <div class="asta-drawer-header"><div> <h2 id="ex-drawer-right-title" class="asta-drawer-title">Invite people</h2> <p id="ex-drawer-right-desc" class="asta-drawer-description">They get an email with a link to join Astabyte.</p> </div></div> <div class="asta-drawer-body"> <div class="asta-field"><label class="asta-label" for="ex-inv">Email</label><label class="asta-input"><input class="asta-input-control" id="ex-inv" name="email" type="email" autocomplete="email" inputmode="email" autocapitalize="none" spellcheck="false" placeholder="[email protected]"></label><p class="asta-hint">Invites expire after seven days.</p></div> </div> <div class="asta-drawer-footer"> <button class="asta-btn asta-btn-outline" data-asta-close><span class="asta-btn-label">Cancel</span></button> <button class="asta-btn asta-btn-accent"><span class="asta-btn-label">Send invite</span></button> </div> </div> <button type="button" class="asta-drawer-close" data-asta-close aria-label="Close"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" aria-hidden="true"><path d="m4 4 8 8M12 4l-8 8"/></svg></button> </dialog> <dialog id="ex-drawer-left" class="asta-dialog asta-drawer" data-side="left" aria-labelledby="ex-drawer-left-title"> <div class="asta-drawer-inner"> <div class="asta-drawer-header"><div><h2 id="ex-drawer-left-title" class="asta-drawer-title">Filters</h2></div></div> <div class="asta-drawer-body"><p style="font-size:14px;color:var(--ink-2)">Pick what to show in the list.</p></div> </div> <button type="button" class="asta-drawer-close" data-asta-close aria-label="Close"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" aria-hidden="true"><path d="m4 4 8 8M12 4l-8 8"/></svg></button> </dialog> <dialog id="ex-drawer-bottom" class="asta-dialog asta-drawer" data-side="bottom" aria-labelledby="ex-drawer-bottom-title" aria-describedby="ex-drawer-bottom-desc"> <div class="asta-drawer-inner"> <div class="asta-drawer-handle" data-asta-drawer-handle aria-hidden="true"><span></span></div> <div class="asta-drawer-header"><div> <h2 id="ex-drawer-bottom-title" class="asta-drawer-title">Share file</h2> <p id="ex-drawer-bottom-desc" class="asta-drawer-description">Drag the handle down to close.</p> </div></div> <div class="asta-drawer-body"><p style="font-size:14px;color:var(--ink-2)">Anyone with the link can view.</p></div> </div> <button type="button" class="asta-drawer-close" data-asta-close aria-label="Close"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" aria-hidden="true"><path d="m4 4 8 8M12 4l-8 8"/></svg></button> </dialog> </div>
Usage
The demo above, as code.
"use client";
import { useState } from "react";
import { Button } from "@/components/astabyte/button";
import { Drawer, type DrawerSide } from "@/components/astabyte/drawer";
const ITEMS = [
{ k: "Linen overshirt · M", v: "$89.00" },
{ k: "Canvas tote", v: "$34.00" },
{ k: "Shipping", v: "Free" },
];
export default function DrawerDemo() {
const [side, setSide] = useState<DrawerSide | null>(null);
const close = () => setSide(null);
return (
<div className="relative flex min-h-[460px] w-full items-center justify-center overflow-hidden rounded-xl p-6 sm:p-10">
<div className="flex w-full max-w-sm flex-col gap-3 rounded-xl bg-paper-2 p-5">
<p className="text-[12px] text-mute">Order #10482 · Paid</p>
<p className="text-[15px] font-medium tracking-[-0.01em] text-ink">2 items · $123.00</p>
<div className="mt-2 flex flex-wrap gap-2">
<Button size="sm" onClick={() => setSide("right")}>
View order
</Button>
<Button size="sm" variant="outline" onClick={() => setSide("left")}>
Filters
</Button>
<Button size="sm" variant="ghost" onClick={() => setSide("bottom")}>
Share
</Button>
</div>
</div>
<Drawer
contained
open={side === "right"}
onOpenChange={close}
side="right"
title="Order #10482"
description="Placed today at 09:41"
footer={
<>
<Button variant="outline" onClick={close}>
Refund
</Button>
<Button data-autofocus onClick={close}>
Mark as shipped
</Button>
</>
}
>
<div className="rounded-[10px] bg-paper-2 px-4 py-2">
{ITEMS.map((i) => (
<div key={i.k} className="flex justify-between gap-4 py-1.5 text-[13.5px]">
<span className="text-mute">{i.k}</span>
<span className="tabular-nums text-ink">{i.v}</span>
</div>
))}
</div>
</Drawer>
<Drawer contained open={side === "left"} onOpenChange={close} side="left" title="Filters">
{["Paid", "Shipped", "Refunded"].map((s) => (
<label key={s} className="flex items-center gap-3 py-2 text-[14px] text-ink">
<input type="checkbox" defaultChecked={s === "Paid"} className="h-4 w-4 accent-[var(--color-brand)]" />
{s}
</label>
))}
</Drawer>
<Drawer contained open={side === "bottom"} onOpenChange={close} side="bottom" title="Share receipt" description="Drag the handle down to close.">
<div className="flex gap-2">
<Button variant="soft" className="flex-1" onClick={close}>
Copy link
</Button>
<Button variant="soft" className="flex-1" onClick={close}>
Email
</Button>
</div>
</Drawer>
</div>
);
}