Drawer
Modal panel that slides in from the right, left or bottom edge, with a header, scrolling body and footer, and swipe to dismiss.
pnpm dlx shadcn@latest add @haze/drawer---
import { X } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
import Button from "./button.astro";
interface Props {
/** Unique on the page. Any button with `command="show-modal" commandfor="<id>"` opens this drawer. */
id: string;
title: string;
description?: string;
/** The screen edge the panel is attached to. */
side?: "right" | "left" | "bottom";
/** Set to false when the opening button lives somewhere else on the page. */
trigger?: boolean;
/** Visible trigger text. The "trigger" slot replaces it. */
triggerLabel?: string;
triggerVariant?: "primary" | "secondary" | "outline" | "ghost";
closeLabel?: string;
class?: string;
}
const {
id,
title,
description,
side = "right",
trigger = true,
triggerLabel = "Open panel",
triggerVariant = "outline",
closeLabel = "Close",
class: className,
} = Astro.props;
// The auto margin on one side pushes the modal dialog against the opposite screen edge.
const sides = {
right: "my-0 ml-auto mr-0 h-dvh max-h-dvh w-[min(24rem,calc(100vw-3rem))] translate-x-full border-l open:translate-x-[var(--haze-drag,0px)] starting:open:translate-x-full",
left: "my-0 ml-0 mr-auto h-dvh max-h-dvh w-[min(24rem,calc(100vw-3rem))] -translate-x-full border-r open:translate-x-[var(--haze-drag,0px)] starting:open:-translate-x-full",
bottom: "mx-0 mb-0 mt-auto max-h-[85dvh] w-full max-w-none translate-y-full rounded-t-2xl border-t open:translate-y-[var(--haze-drag,0px)] starting:open:translate-y-full",
};
---
{
trigger && (
<Button variant={triggerVariant} command="show-modal" commandfor={id} aria-haspopup="dialog">
<slot name="trigger">{triggerLabel}</slot>
</Button>
)
}
<dialog
id={id}
data-haze-drawer
data-side={side}
aria-labelledby={`${id}-title`}
aria-describedby={description ? `${id}-description` : undefined}
closedby="any"
class={cn(
"border-border bg-popover p-0 text-popover-foreground shadow-float transition-[translate,overlay,display] transition-discrete duration-300 ease-snappy data-[dragging]:transition-none data-[dragging]:select-none data-[settling]:duration-200 motion-reduce:transition-none",
"backdrop:bg-background/60 backdrop:opacity-0 backdrop:transition-[opacity,overlay,display] backdrop:transition-discrete backdrop:duration-300 open:backdrop:opacity-100 starting:open:backdrop:opacity-0",
sides[side],
className,
)}
>
<div class="relative flex h-full max-h-[inherit] flex-col">
<header data-drawer-handle class={cn("grid gap-2 border-b border-border p-5 pr-14", side === "bottom" ? "touch-none" : "touch-pan-y")}>
{side === "bottom" && <span aria-hidden="true" class="-mt-2 h-1 w-10 justify-self-center rounded-full bg-border-strong"></span>}
<h2 id={`${id}-title`} class="text-[1.0625rem] leading-[1.4] font-medium tracking-[-0.01em] text-foreground">{title}</h2>
{description && <p id={`${id}-description`} class="text-[0.8125rem] leading-[1.4] text-muted-foreground">{description}</p>}
</header>
<div class="min-h-0 flex-1 overflow-y-auto p-5 text-[0.8125rem] leading-[1.4]">
<slot />
</div>
{
Astro.slots.has("footer") && (
<footer class="flex flex-col-reverse gap-2 border-t border-border px-5 py-4 sm:flex-row sm:justify-end">
<slot name="footer" />
</footer>
)
}
<form method="dialog" class="absolute right-5 top-5">
<button
type="submit"
aria-label={closeLabel}
class="inline-flex size-6 cursor-pointer items-center justify-center rounded-lg border border-frame bg-muted/50 text-muted-foreground transition-[background-color,color,scale] duration-150 ease-snappy hover:bg-muted hover:text-foreground active:scale-[0.96] active:duration-100 motion-reduce:transition-none motion-reduce:active:scale-100 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring"
>
<X class="size-3.5" aria-hidden="true" />
</button>
</form>
</div>
</dialog>
<script>
// Fallback only, identical to dialog.astro. Current browsers open and close the panel from the
// `command` / `commandfor` attributes and handle `closedby="any"` themselves.
const root = document.documentElement;
if (!root.hasAttribute("data-haze-invokers")) {
root.setAttribute("data-haze-invokers", "");
if (!("command" in HTMLButtonElement.prototype)) {
document.addEventListener("click", (event) => {
const button = (event.target as Element | null)?.closest?.("button[commandfor]");
const target = button && document.getElementById(button.getAttribute("commandfor") ?? "");
if (!(target instanceof HTMLDialogElement)) return;
const command = button!.getAttribute("command");
if (command === "show-modal" && !target.open) target.showModal();
if (command === "close" && target.open) target.close();
});
}
if (!("closedBy" in HTMLDialogElement.prototype)) {
// A click on the backdrop lands on the <dialog> element itself, because its content fills it.
document.addEventListener("click", (event) => {
const dialog = event.target;
if (dialog instanceof HTMLDialogElement && dialog.open && dialog.getAttribute("closedby") === "any") dialog.close();
});
}
}
// Swipe to dismiss toward the edge the panel came from. Only the header is a drag handle, so the
// scrolling body keeps its own gestures. Escape and the close button stay as the keyboard routes.
function enhanceDrawer(dialog: HTMLDialogElement) {
if (dialog.hasAttribute("data-enhanced")) return;
dialog.setAttribute("data-enhanced", "");
const handle = dialog.querySelector<HTMLElement>("[data-drawer-handle]");
if (!handle) return;
const side = dialog.dataset.side;
const reduced = matchMedia("(prefers-reduced-motion: reduce)");
const horizontal = side !== "bottom";
const sign = side === "left" ? -1 : 1;
let pointer = -1, start = 0, distance = 0;
let samples: { t: number; p: number }[] = [];
const settle = () => { dialog.removeAttribute("data-settling"); };
handle.addEventListener("pointerdown", (event) => {
if (event.button !== 0 || !dialog.open || (event.target as Element).closest("a, button, input, select, textarea")) return;
pointer = event.pointerId;
start = horizontal ? event.clientX : event.clientY;
distance = 0;
samples = [{ t: performance.now(), p: start }];
handle.setPointerCapture(event.pointerId);
dialog.removeAttribute("data-settling");
dialog.setAttribute("data-dragging", "");
});
handle.addEventListener("pointermove", (event) => {
if (event.pointerId !== pointer) return;
const position = horizontal ? event.clientX : event.clientY;
distance = Math.max(0, (position - start) * sign);
samples.push({ t: performance.now(), p: position });
if (!reduced.matches) dialog.style.setProperty("--haze-drag", distance * sign + "px");
});
const finish = (event: PointerEvent, cancelled: boolean) => {
if (event.pointerId !== pointer) return;
pointer = -1;
if (handle.hasPointerCapture(event.pointerId)) handle.releasePointerCapture(event.pointerId);
// Velocity over the last 100 ms of the gesture, toward the edge.
const now = performance.now();
const recent = samples.filter((sample) => now - sample.t <= 100);
// A pause before release leaves no recent samples (or one), so the velocity is 0.
const first = recent[0], last = recent.at(-1);
const velocity = first && last && last.t > first.t ? ((last.p - first.p) * sign) / (last.t - first.t) : 0;
const size = horizontal ? dialog.offsetWidth : dialog.offsetHeight;
const dismiss = !cancelled && distance > 0 && (distance > size * 0.3 || velocity > 0.5);
if (dismiss) {
dialog.removeAttribute("data-dragging");
dialog.close();
return;
}
if (distance > 0 && !reduced.matches) {
dialog.setAttribute("data-settling", "");
dialog.addEventListener("transitionend", settle, { once: true });
}
dialog.removeAttribute("data-dragging");
dialog.style.removeProperty("--haze-drag");
};
handle.addEventListener("pointerup", (event) => finish(event, false));
handle.addEventListener("pointercancel", (event) => finish(event, true));
dialog.addEventListener("close", () => {
pointer = -1;
dialog.removeAttribute("data-dragging");
dialog.removeAttribute("data-settling");
dialog.style.removeProperty("--haze-drag");
});
}
const initDrawers = () => document.querySelectorAll<HTMLDialogElement>("[data-haze-drawer]").forEach(enhanceDrawer);
initDrawers();
if (!root.hasAttribute("data-haze-drawer-listener")) {
root.setAttribute("data-haze-drawer-listener", "");
document.addEventListener("astro:page-load", initDrawers);
}
</script>When to use
- Details of a list row, such as an issue or an order, without leaving the list
- Filters or settings with more fields than fit in a popover
- A bottom sheet of actions on small screens
When not to use
- A short yes-or-no confirmation; use dialog
- The site's main mobile menu when it only holds links; a popover is lighter
- Content people need to see next to the page; a modal drawer blocks the page behind it
Installation
Add the Haze registry to components.json once, then install Drawer with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/drawernpx shadcn@latest add @haze/draweryarn dlx shadcn@latest add @haze/drawerbunx --bun shadcn@latest add @haze/drawerFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Unique on the page. Buttons target it with commandfor="<id>". |
| title* | string | – | Heading; also the drawer's accessible name. |
| description | string | – | One line under the title. |
| side | "right" | "left" | "bottom" | "right" | Screen edge the panel is attached to. |
| trigger | boolean | true | Set to false when the opening button lives elsewhere. |
| triggerLabel | string | "Open panel" | Trigger text when the trigger slot is not used. |
| triggerVariant | "primary" | "secondary" | "outline" | "ghost" | "outline" | Trigger button style. |
| closeLabel | string | "Close" | Accessible name of the close button; translate it. |
| class | string | – | Extra classes on the panel, for example a wider width. |
| default slot | slot | – | Body content; it scrolls when it is taller than the panel. |
| footer slot | slot | – | Action buttons pinned to the bottom. |
| trigger slot | slot | – | Custom trigger content. |
Keyboard interactions
- EnterSpace
- Open the drawer from the trigger
- TabShiftTab
- Move between controls inside the drawer
- Escape
- Close the drawer and return focus to the trigger
Accessibility
- The panel slides in from its edge over 300 ms and slides out again, and its backdrop fades with it; reduced-motion users get no animation.
- Dragging the header toward the edge the panel came from (down for bottom, right for right, left for left) closes it: past 30 % of its size or faster than 0.5 px/ms it dismisses, otherwise it springs back over 200 ms. Reduced-motion users get no follow animation but the same threshold. The swipe is an extra; Escape, the close button and a backdrop click always work.
- It is a modal <dialog>: while it is open the rest of the page cannot be reached by pointer, keyboard or screen reader.
- Side panels leave at least 48 px of the page visible, so a tap on the backdrop can always close them.
- Only the body scrolls; the title and the footer actions stay in view.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Drawer" with `npx shadcn@latest add @haze/drawer`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Drawer from "@/components/haze/ui/drawer.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/drawer.md
Customize with real content
Adapt the Haze drawer ui for our product. Replace the demo values of these props with our real content: id, title, description, side, trigger, triggerLabel, triggerVariant, closeLabel, class, default slot, footer slot, trigger slot. Keep the layout, the heading level rules and these accessibility details: The panel slides in from its edge over 300 ms and slides out again, and its backdrop fades with it; reduced-motion users get no animation. Dragging the header toward the edge the panel came from (down for bottom, right for right, left for left) closes it: past 30 % of its size or faster than 0.5 px/ms it dismisses, otherwise it springs back over 200 ms. Reduced-motion users get no follow animation but the same threshold. The swipe is an extra; Escape, the close button and a backdrop click always work. It is a modal <dialog>: while it is open the rest of the page cannot be reached by pointer, keyboard or screen reader. Side panels leave at least 48 px of the page visible, so a tap on the backdrop can always close them. Only the body scrolls; the title and the footer actions stay in view. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using drawer, check it fits: use it when details of a list row, such as an issue or an order, without leaving the list; filters or settings with more fields than fit in a popover; a bottom sheet of actions on small screens. Avoid it when a short yes-or-no confirmation; use dialog; the site's main mobile menu when it only holds links; a popover is lighter; content people need to see next to the page; a modal drawer blocks the page behind it. Alternatives: @haze/dialog, @haze/popover.
Dependencies
- npm: @lucide/astro
- @haze/button
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.