Drawer
A draggable React drawer built on shadcn/ui and vaul: swipe-to-dismiss bottom sheets and side drawers with a handle bar and dimmed backdrop, plus a responsive dialog that becomes a drawer on phones and an Apple Maps-style snap-points sheet.
- Drop-in shadcn API
- vaul
- Drag to dismiss
- Snap points
- Responsive dialog
import { Clock, MapPin, Star } from "lucide-react";
import { Button } from "@/components/ui/button";
import { SnapDrawer } from "@/components/dusk/drawer/snap-drawer";
const reviews = [
{ name: "Mara", text: "Best flat white in the neighbourhood, and they remember your order." },
{ name: "Jun", text: "Quiet in the mornings. Plenty of sockets by the window." },
{ name: "Theo", text: "The cardamom bun is worth the queue." },
{ name: "Ines", text: "Friendly staff, oat milk at no extra cost." },
{ name: "Sam", text: "Gets busy after ten — come early if you want a table." },
];
export default function SnapDrawerDemo() {
return (
<SnapDrawer
trigger={<Button variant="outline">Show place details</Button>}
snapPoints={[0.28, 0.6, 1]}
snapLabels={["Peek", "Half height", "Full height"]}
title="Dusk Coffee Roasters"
description="Café · 4 min walk"
peek={
<div className="flex flex-wrap items-center gap-x-4 gap-y-1 text-sm text-muted-foreground">
<span className="flex items-center gap-1.5">
<Star className="size-4 fill-foreground text-foreground" aria-hidden /> 4.8 (312)
</span>
<span className="flex items-center gap-1.5">
<Clock className="size-4" aria-hidden /> Open until 18:00
</span>
<span className="flex items-center gap-1.5">
<MapPin className="size-4" aria-hidden /> 12 Lantern St
</span>
</div>
}
>
<h3 className="mb-3 text-sm font-semibold">Reviews</h3>
<ul className="flex flex-col gap-3">
{reviews.map((review) => (
<li key={review.name} className="rounded-xl border bg-white/[0.02] p-3 text-sm">
<p className="font-medium">{review.name}</p>
<p className="mt-1 text-muted-foreground">{review.text}</p>
</li>
))}
</ul>
</SnapDrawer>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/drawer.json1. Install the dependencies.
npm install vaul2. Copy the source into
components/ui/drawer.tsx.drawer.tsx "use client" import * as React from "react" import { Drawer as DrawerPrimitive } from "vaul" import { cn } from "@/lib/utils" /** * DuskUI Drawer — the shadcn/ui Drawer (vaul), styled for DuskUI. * * Same parts, props and data-slot attributes as shadcn (Drawer, DrawerTrigger, * DrawerPortal, DrawerOverlay, DrawerContent, DrawerHeader, DrawerFooter, * DrawerTitle, DrawerDescription, DrawerClose), plus: * - A handle bar on bottom drawers so it reads as draggable. * `showHandle={false}` hides it. * - A dimmed backdrop and a neutral shadow. * - Long content scrolls inside the drawer; the safe-area inset is respected. * - Under `prefers-reduced-motion` the drawer fades in and out instead of * sliding (dragging still follows your finger). * vaul handles drag-to-dismiss, snap points and velocity; Radix Dialog * underneath handles the focus trap, Escape and aria. */ function Drawer({ ...props }: React.ComponentProps<typeof DrawerPrimitive.Root>) { return <DrawerPrimitive.Root data-slot="drawer" {...props} /> } /** The element that opens the drawer. Use `asChild` to render your own Button. */ function DrawerTrigger({ ...props }: React.ComponentProps<typeof DrawerPrimitive.Trigger>) { return <DrawerPrimitive.Trigger data-slot="drawer-trigger" {...props} /> } /** Portals the overlay and content to `document.body`. */ function DrawerPortal({ ...props }: React.ComponentProps<typeof DrawerPrimitive.Portal>) { return <DrawerPrimitive.Portal data-slot="drawer-portal" {...props} /> } /** Closes the drawer. Use `asChild` to render your own Button. */ function DrawerClose({ ...props }: React.ComponentProps<typeof DrawerPrimitive.Close>) { return <DrawerPrimitive.Close data-slot="drawer-close" {...props} /> } /** * vaul injects its slide / fade keyframes as unlayered CSS, which beats * Tailwind's layered utilities, so the reduced-motion override needs `!`. */ const reducedMotion = "motion-reduce:data-[state=open]:[animation-name:fadeIn]! motion-reduce:data-[state=closed]:[animation-name:fadeOut]! motion-reduce:transition-none!" /** The dimmed backdrop. Fades with the drawer and as you drag it. */ function DrawerOverlay({ className, ...props }: React.ComponentProps<typeof DrawerPrimitive.Overlay>) { return ( <DrawerPrimitive.Overlay data-slot="drawer-overlay" className={cn("fixed inset-0 z-50 bg-black/60", className)} {...props} /> ) } /** The drawer surface. Slides in from the Drawer's `direction` (bottom by default). */ function DrawerContent({ className, children, showHandle = true, ...props }: React.ComponentProps<typeof DrawerPrimitive.Content> & { /** Show the handle bar at the top of bottom drawers. */ showHandle?: boolean }) { return ( <DrawerPortal data-slot="drawer-portal"> <DrawerOverlay /> <DrawerPrimitive.Content data-slot="drawer-content" className={cn( "group/drawer-content fixed z-50 flex h-auto flex-col bg-background shadow-lg outline-none", // Bottom "data-[vaul-drawer-direction=bottom]:inset-x-0 data-[vaul-drawer-direction=bottom]:bottom-0 data-[vaul-drawer-direction=bottom]:mt-24 data-[vaul-drawer-direction=bottom]:max-h-[85dvh] data-[vaul-drawer-direction=bottom]:rounded-t-2xl data-[vaul-drawer-direction=bottom]:border-t", "data-[vaul-drawer-direction=bottom]:pb-[env(safe-area-inset-bottom)]", // Top "data-[vaul-drawer-direction=top]:inset-x-0 data-[vaul-drawer-direction=top]:top-0 data-[vaul-drawer-direction=top]:mb-24 data-[vaul-drawer-direction=top]:max-h-[85dvh] data-[vaul-drawer-direction=top]:rounded-b-2xl data-[vaul-drawer-direction=top]:border-b", "data-[vaul-drawer-direction=top]:pt-[env(safe-area-inset-top)]", // Right "data-[vaul-drawer-direction=right]:inset-y-0 data-[vaul-drawer-direction=right]:right-0 data-[vaul-drawer-direction=right]:w-3/4 data-[vaul-drawer-direction=right]:border-l data-[vaul-drawer-direction=right]:sm:max-w-sm", // Left "data-[vaul-drawer-direction=left]:inset-y-0 data-[vaul-drawer-direction=left]:left-0 data-[vaul-drawer-direction=left]:w-3/4 data-[vaul-drawer-direction=left]:border-r data-[vaul-drawer-direction=left]:sm:max-w-sm", reducedMotion, className )} {...props} > {showHandle && ( <div aria-hidden data-slot="drawer-handle" className={cn( "mx-auto mt-3 mb-1 hidden h-1.5 w-12 shrink-0 rounded-full bg-white/15", "group-data-[vaul-drawer-direction=bottom]/drawer-content:block" )} /> )} {children} </DrawerPrimitive.Content> </DrawerPortal> ) } /** Wraps the title and description. Centered on bottom / top drawers, left-aligned from `md` up. */ function DrawerHeader({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="drawer-header" className={cn( "flex flex-col gap-1 p-4", "group-data-[vaul-drawer-direction=bottom]/drawer-content:text-center group-data-[vaul-drawer-direction=top]/drawer-content:text-center", "md:gap-1.5 md:text-left md:group-data-[vaul-drawer-direction=bottom]/drawer-content:text-left md:group-data-[vaul-drawer-direction=top]/drawer-content:text-left", className )} {...props} /> ) } /** Action row, pinned to the bottom of the drawer. */ function DrawerFooter({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="drawer-footer" className={cn("mt-auto flex flex-col gap-2 p-4", className)} {...props} /> ) } /** Accessible title, announced when the drawer opens. Required by Radix. */ function DrawerTitle({ className, ...props }: React.ComponentProps<typeof DrawerPrimitive.Title>) { return ( <DrawerPrimitive.Title data-slot="drawer-title" className={cn("text-lg leading-none font-semibold tracking-tight text-foreground", className)} {...props} /> ) } /** Supporting text linked to the drawer via aria-describedby. */ function DrawerDescription({ className, ...props }: React.ComponentProps<typeof DrawerPrimitive.Description>) { return ( <DrawerPrimitive.Description data-slot="drawer-description" className={cn("text-sm leading-relaxed text-muted-foreground", className)} {...props} /> ) } export { Drawer, DrawerPortal, DrawerOverlay, DrawerTrigger, DrawerClose, DrawerContent, DrawerHeader, DrawerFooter, DrawerTitle, DrawerDescription, }
Adds components/ui/drawer.tsx with the shadcn exports. vaul injects its slide keyframes as unlayered CSS; the reduced-motion fade override uses !important utilities for that reason.
Usage
import { Button } from "@/components/ui/button"
import {
Drawer,
DrawerClose,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer"
export function GoalDrawer() {
return (
<Drawer>
<DrawerTrigger asChild>
<Button variant="outline">Open</Button>
</DrawerTrigger>
<DrawerContent>
<DrawerHeader>
<DrawerTitle>Move goal</DrawerTitle>
<DrawerDescription>Set your daily activity goal.</DrawerDescription>
</DrawerHeader>
<DrawerFooter>
<DrawerClose asChild>
<Button>Done</Button>
</DrawerClose>
</DrawerFooter>
</DrawerContent>
</Drawer>
)
}Examples
Default
A bottom drawer with the handle bar. Drag it down past the threshold (or flick it) to dismiss, or use Escape, the backdrop or a DrawerClose button.
"use client";
import * as React from "react";
import { Minus, Plus } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
Drawer,
DrawerClose,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer";
export default function DrawerBasicDemo() {
const [goal, setGoal] = React.useState(350);
return (
<Drawer>
<DrawerTrigger asChild>
<Button variant="outline">Set daily goal</Button>
</DrawerTrigger>
<DrawerContent>
<div className="mx-auto w-full max-w-sm">
<DrawerHeader>
<DrawerTitle>Move goal</DrawerTitle>
<DrawerDescription>Set your daily activity goal. Drag down to dismiss.</DrawerDescription>
</DrawerHeader>
<div className="flex items-center justify-center gap-6 p-4">
<Button
variant="outline"
size="icon"
className="rounded-full"
aria-label="Decrease goal"
aria-disabled={goal <= 200 || undefined}
onClick={() => setGoal((g) => Math.max(200, g - 10))}
>
<Minus />
</Button>
<div className="w-28 text-center">
<div className="text-6xl font-bold tracking-tighter tabular-nums" aria-live="polite">
{goal}
</div>
<div className="text-xs text-muted-foreground">kcal per day</div>
</div>
<Button
variant="outline"
size="icon"
className="rounded-full"
aria-label="Increase goal"
aria-disabled={goal >= 600 || undefined}
onClick={() => setGoal((g) => Math.min(600, g + 10))}
>
<Plus />
</Button>
</div>
<DrawerFooter>
<DrawerClose asChild>
<Button>Save goal</Button>
</DrawerClose>
<DrawerClose asChild>
<Button variant="outline">Cancel</Button>
</DrawerClose>
</DrawerFooter>
</div>
</DrawerContent>
</Drawer>
);
}Directions
Set direction on Drawer to top, right, bottom or left. Side drawers are three quarters wide on phones and cap at max-w-sm.
import { Button } from "@/components/ui/button";
import {
Drawer,
DrawerClose,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerHeader,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer";
const directions = ["top", "right", "bottom", "left"] as const;
export default function DrawerDirectionsDemo() {
return (
<div className="grid grid-cols-2 gap-2 sm:grid-cols-4">
{directions.map((direction) => (
<Drawer key={direction} direction={direction}>
<DrawerTrigger asChild>
<Button variant="outline" className="capitalize">
{direction}
</Button>
</DrawerTrigger>
<DrawerContent>
<DrawerHeader>
<DrawerTitle className="capitalize">{direction} drawer</DrawerTitle>
<DrawerDescription>Swipe toward the {direction} edge to dismiss it.</DrawerDescription>
</DrawerHeader>
<div className="px-4 text-sm text-muted-foreground">
Side drawers make good filter panels; top drawers suit announcements.
</div>
<DrawerFooter>
<DrawerClose asChild>
<Button variant="outline">Close</Button>
</DrawerClose>
</DrawerFooter>
</DrawerContent>
</Drawer>
))}
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Responsive Dialog
A centered Dialog on desktop and a drag-to-dismiss Drawer on phones, from one set of parts. A useSyncExternalStore media-query hook keeps server and client markup identical, and the open state lives in the block so resizing swaps the surface without closing it. Header, body and footer rise in with a short stagger on both surfaces.
Resize below 768px to get the drawer.
"use client";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
ResponsiveDialog,
ResponsiveDialogBody,
ResponsiveDialogClose,
ResponsiveDialogContent,
ResponsiveDialogDescription,
ResponsiveDialogFooter,
ResponsiveDialogHeader,
ResponsiveDialogTitle,
ResponsiveDialogTrigger,
} from "@/components/dusk/drawer/responsive-dialog";
export default function ResponsiveDialogDemo() {
return (
<div className="flex flex-col items-center gap-3">
<ResponsiveDialog>
<ResponsiveDialogTrigger asChild>
<Button>Invite teammate</Button>
</ResponsiveDialogTrigger>
<ResponsiveDialogContent>
<ResponsiveDialogHeader>
<ResponsiveDialogTitle>Invite a teammate</ResponsiveDialogTitle>
<ResponsiveDialogDescription>They'll get an email with a link to join the workspace.</ResponsiveDialogDescription>
</ResponsiveDialogHeader>
<ResponsiveDialogBody className="grid gap-2">
<label htmlFor="invite-email" className="text-sm font-medium">
Email
</label>
<Input id="invite-email" type="email" placeholder="ada@example.com" autoComplete="off" />
</ResponsiveDialogBody>
<ResponsiveDialogFooter>
<ResponsiveDialogClose asChild>
<Button variant="outline">Cancel</Button>
</ResponsiveDialogClose>
<ResponsiveDialogClose asChild>
<Button>Send invite</Button>
</ResponsiveDialogClose>
</ResponsiveDialogFooter>
</ResponsiveDialogContent>
</ResponsiveDialog>
<p className="text-xs text-muted-foreground">Resize below 768px to get the drawer.</p>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/responsive-dialog.json1. Install the dependencies.
npm install motion vaul @radix-ui/react-dialog2. Copy the source into
components/dusk/drawer/responsive-dialog.tsx.responsive-dialog.tsx "use client"; import * as React from "react"; import { motion, useReducedMotion, type Variants } from "motion/react"; import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger, } from "@/components/ui/dialog"; import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger, } from "@/components/ui/drawer"; import { cn } from "@/lib/utils"; /** * `true` while `query` matches. Uses useSyncExternalStore with a server * snapshot, so the first client render matches the server HTML and React * re-renders with the real value right after hydration — no mismatch. */ export function useMediaQuery(query: string, serverValue = false) { const subscribe = React.useCallback( (onChange: () => void) => { const mql = window.matchMedia(query); mql.addEventListener("change", onChange); return () => mql.removeEventListener("change", onChange); }, [query] ); return React.useSyncExternalStore( subscribe, () => window.matchMedia(query).matches, () => serverValue ); } type ResponsiveDialogContextValue = { desktop: boolean }; const ResponsiveDialogContext = React.createContext<ResponsiveDialogContextValue | null>(null); function useResponsiveDialog() { const context = React.useContext(ResponsiveDialogContext); if (!context) throw new Error("ResponsiveDialog parts must be used inside <ResponsiveDialog>."); return context; } export interface ResponsiveDialogProps { open?: boolean; defaultOpen?: boolean; onOpenChange?: (open: boolean) => void; /** Media query that switches to the centered Dialog. */ desktopQuery?: string; children?: React.ReactNode; } /** * One component, two surfaces: a centered Dialog on desktop and a * drag-to-dismiss bottom Drawer (vaul) on phones. The open state lives here, * so rotating a tablet or resizing the window swaps the surface without * closing it. Content rises in with a short stagger on either surface. * Parts mirror the Dialog API (Trigger, Content, Header, Title, Description, * Body, Footer, Close). Focus trap, Escape and aria come from Radix Dialog * in both modes. */ export function ResponsiveDialog({ open: openProp, defaultOpen = false, onOpenChange, desktopQuery = "(min-width: 768px)", children, }: ResponsiveDialogProps) { const desktop = useMediaQuery(desktopQuery); const [uncontrolled, setUncontrolled] = React.useState(defaultOpen); const open = openProp ?? uncontrolled; const setOpen = React.useCallback( (next: boolean) => { if (openProp === undefined) setUncontrolled(next); onOpenChange?.(next); }, [openProp, onOpenChange] ); const context = React.useMemo(() => ({ desktop }), [desktop]); return ( <ResponsiveDialogContext.Provider value={context}> {desktop ? ( <Dialog open={open} onOpenChange={setOpen}> {children} </Dialog> ) : ( <Drawer open={open} onOpenChange={setOpen}> {children} </Drawer> )} </ResponsiveDialogContext.Provider> ); } /** Opens the dialog / drawer. Use `asChild` to render your own Button. */ export function ResponsiveDialogTrigger(props: React.ComponentProps<typeof DialogTrigger>) { const { desktop } = useResponsiveDialog(); return desktop ? <DialogTrigger {...props} /> : <DrawerTrigger {...props} />; } /** Closes the dialog / drawer. Use `asChild` to render your own Button. */ export function ResponsiveDialogClose(props: React.ComponentProps<typeof DialogClose>) { const { desktop } = useResponsiveDialog(); return desktop ? <DialogClose {...props} /> : <DrawerClose {...props} />; } const stagger: Variants = { hidden: {}, shown: { transition: { staggerChildren: 0.05, delayChildren: 0.08 } }, }; const rise = (reduceMotion: boolean): Variants => ({ hidden: reduceMotion ? { opacity: 0 } : { opacity: 0, y: 8 }, shown: reduceMotion ? { opacity: 1, transition: { duration: 0.2 } } : { opacity: 1, y: 0, transition: { duration: 0.2, ease: "easeOut" } }, }); export interface ResponsiveDialogContentProps { className?: string; children?: React.ReactNode; /** Desktop only: render the X button. On phones the drawer is dragged or tapped away. */ showCloseButton?: boolean; } /** The surface: DialogContent on desktop, DrawerContent (with handle) on phones. */ export function ResponsiveDialogContent({ className, children, showCloseButton = true }: ResponsiveDialogContentProps) { const { desktop } = useResponsiveDialog(); const body = ( <motion.div variants={stagger} initial="hidden" animate="shown" className="contents"> {children} </motion.div> ); return desktop ? ( <DialogContent showCloseButton={showCloseButton} className={cn("sm:max-w-md", className)}> {body} </DialogContent> ) : ( <DrawerContent className={className}>{body}</DrawerContent> ); } /** A block that rises in with the stagger. Use it for any custom section. */ function Rise({ className, children }: { className?: string; children?: React.ReactNode }) { const reduceMotion = useReducedMotion() ?? false; return ( <motion.div variants={rise(reduceMotion)} className={className}> {children} </motion.div> ); } /** Wraps the title and description. */ export function ResponsiveDialogHeader({ className, children }: { className?: string; children?: React.ReactNode }) { const { desktop } = useResponsiveDialog(); return <Rise>{desktop ? <DialogHeader className={className}>{children}</DialogHeader> : <DrawerHeader className={className}>{children}</DrawerHeader>}</Rise>; } /** Accessible title, announced on open. Required. */ export function ResponsiveDialogTitle(props: React.ComponentProps<typeof DialogTitle>) { const { desktop } = useResponsiveDialog(); return desktop ? <DialogTitle {...props} /> : <DrawerTitle {...props} />; } /** Supporting text linked via aria-describedby. */ export function ResponsiveDialogDescription(props: React.ComponentProps<typeof DialogDescription>) { const { desktop } = useResponsiveDialog(); return desktop ? <DialogDescription {...props} /> : <DrawerDescription {...props} />; } /** The main content. Padded on phones to line up with the drawer header. */ export function ResponsiveDialogBody({ className, children }: { className?: string; children?: React.ReactNode }) { const { desktop } = useResponsiveDialog(); return <Rise className={cn(!desktop && "overflow-y-auto px-4", className)}>{children}</Rise>; } /** Action row. Side by side on desktop, stacked full-width on phones. */ export function ResponsiveDialogFooter({ className, children }: { className?: string; children?: React.ReactNode }) { const { desktop } = useResponsiveDialog(); return ( <Rise> {desktop ? <DialogFooter className={className}>{children}</DialogFooter> : <DrawerFooter className={className}>{children}</DrawerFooter>} </Rise> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| open / defaultOpen / onOpenChange | boolean | false | On ResponsiveDialog: controlled or uncontrolled state, shared by both surfaces. |
| desktopQuery | string | "(min-width: 768px)" | Media query that switches to the Dialog. |
| showCloseButton | boolean | true | On ResponsiveDialogContent: the X button on desktop. |
| Trigger / Close | asChild button props | — | Open and close from either surface. |
| Header / Title / Description / Body / Footer | parts | — | Mirror the Dialog API; Body pads itself on phones. |
| useMediaQuery(query, serverValue?) | hook | — | The hydration-safe matchMedia hook, exported for reuse. |
Snap Drawer
A bottom sheet with snap points, like Apple Maps: drag between resting heights, flick to skip one, or tap the indicator dots — a pill glides between them and the handle widens as the sheet grows. The backdrop fades in from the second point, and content only scrolls at full height so dragging never fights scrolling. nonModal keeps the page usable behind it.
import { Clock, MapPin, Star } from "lucide-react";
import { Button } from "@/components/ui/button";
import { SnapDrawer } from "@/components/dusk/drawer/snap-drawer";
const reviews = [
{ name: "Mara", text: "Best flat white in the neighbourhood, and they remember your order." },
{ name: "Jun", text: "Quiet in the mornings. Plenty of sockets by the window." },
{ name: "Theo", text: "The cardamom bun is worth the queue." },
{ name: "Ines", text: "Friendly staff, oat milk at no extra cost." },
{ name: "Sam", text: "Gets busy after ten — come early if you want a table." },
];
export default function SnapDrawerDemo() {
return (
<SnapDrawer
trigger={<Button variant="outline">Show place details</Button>}
snapPoints={[0.28, 0.6, 1]}
snapLabels={["Peek", "Half height", "Full height"]}
title="Dusk Coffee Roasters"
description="Café · 4 min walk"
peek={
<div className="flex flex-wrap items-center gap-x-4 gap-y-1 text-sm text-muted-foreground">
<span className="flex items-center gap-1.5">
<Star className="size-4 fill-foreground text-foreground" aria-hidden /> 4.8 (312)
</span>
<span className="flex items-center gap-1.5">
<Clock className="size-4" aria-hidden /> Open until 18:00
</span>
<span className="flex items-center gap-1.5">
<MapPin className="size-4" aria-hidden /> 12 Lantern St
</span>
</div>
}
>
<h3 className="mb-3 text-sm font-semibold">Reviews</h3>
<ul className="flex flex-col gap-3">
{reviews.map((review) => (
<li key={review.name} className="rounded-xl border bg-white/[0.02] p-3 text-sm">
<p className="font-medium">{review.name}</p>
<p className="mt-1 text-muted-foreground">{review.text}</p>
</li>
))}
</ul>
</SnapDrawer>
);
}$ npx shadcn@latest add https://duskui.com/r/snap-drawer.json1. Install the dependencies.
npm install motion vaul2. Copy the source into
components/dusk/drawer/snap-drawer.tsx.snap-drawer.tsx "use client"; import * as React from "react"; import { Drawer as DrawerPrimitive } from "vaul"; import { motion, useReducedMotion } from "motion/react"; import { cn } from "@/lib/utils"; type SnapPoint = number | string; export interface SnapDrawerProps { /** Element that opens the drawer, usually a Button. Omit when controlling `open` yourself. */ trigger?: React.ReactElement; open?: boolean; defaultOpen?: boolean; onOpenChange?: (open: boolean) => void; /** * Heights the drawer rests at, smallest first: fractions of the screen * (0–1) or pixel strings like "160px". */ snapPoints?: SnapPoint[]; /** Controlled resting point. */ snap?: SnapPoint | null; onSnapChange?: (snap: SnapPoint | null) => void; /** Accessible names for each snap point, used by the indicator buttons. */ snapLabels?: string[]; title: React.ReactNode; description?: React.ReactNode; /** Shown at every height: the "peek" row under the title. */ peek?: React.ReactNode; /** The rest of the content; scrolls once the drawer is fully expanded. */ children?: React.ReactNode; /** Keep the page interactive while the drawer rests at its first point (a map sheet, a player). */ nonModal?: boolean; className?: string; } const defaultSnapPoints: SnapPoint[] = [0.3, 0.6, 1]; /** * A bottom sheet with snap points, like Apple Maps: drag it between resting * heights, flick to skip one, or tap the indicator dots. A pill glides * between the dots (Motion layoutId) and the handle widens as the sheet * grows. Content only scrolls at the top point, so dragging never fights * scrolling. Built on vaul, so Escape, the focus trap and aria come from * Radix Dialog. Under reduced motion the pill and handle jump. */ export function SnapDrawer({ trigger, open: openProp, defaultOpen = false, onOpenChange, snapPoints = defaultSnapPoints, snap: snapProp, onSnapChange, snapLabels, title, description, peek, children, nonModal = false, className, }: SnapDrawerProps) { const id = React.useId(); const reduceMotion = useReducedMotion(); const [uncontrolledOpen, setUncontrolledOpen] = React.useState(defaultOpen); const [uncontrolledSnap, setUncontrolledSnap] = React.useState<SnapPoint | null>(snapPoints[0] ?? null); const open = openProp ?? uncontrolledOpen; const snap = snapProp !== undefined ? snapProp : uncontrolledSnap; const index = Math.max(0, snapPoints.indexOf(snap ?? snapPoints[0])); const atTop = index === snapPoints.length - 1; const setOpen = (next: boolean) => { if (openProp === undefined) setUncontrolledOpen(next); // Reopen at the first point. if (!next && snapProp === undefined) setUncontrolledSnap(snapPoints[0] ?? null); onOpenChange?.(next); }; const setSnap = (next: SnapPoint | null) => { if (snapProp === undefined) setUncontrolledSnap(next); onSnapChange?.(next); }; const labelFor = (i: number) => snapLabels?.[i] ?? (i === 0 ? "Collapse" : i === snapPoints.length - 1 ? "Expand fully" : `Expand to step ${i + 1}`); return ( <DrawerPrimitive.Root open={open} onOpenChange={setOpen} snapPoints={snapPoints} activeSnapPoint={snap} setActiveSnapPoint={setSnap} modal={!nonModal} // Fade the backdrop in from the second point; the first acts like a peek. {...(nonModal ? {} : { fadeFromIndex: Math.min(1, snapPoints.length - 1) })} > {trigger && <DrawerPrimitive.Trigger asChild>{trigger}</DrawerPrimitive.Trigger>} <DrawerPrimitive.Portal> {!nonModal && <DrawerPrimitive.Overlay className="fixed inset-0 z-50 bg-black/60" />} <DrawerPrimitive.Content data-slot="drawer-content" // Tells Radix the missing description is intentional. {...(description ? {} : { "aria-describedby": undefined })} className={cn( "fixed inset-x-0 bottom-0 z-50 mx-auto flex h-full max-h-[97%] w-full max-w-xl flex-col rounded-t-2xl border border-b-0 bg-background outline-none", "shadow-lg", // vaul animates snaps with inline transitions; reduced motion makes them jump. "motion-reduce:transition-none!", className )} > <div className="flex shrink-0 flex-col items-center gap-2 pt-3 pb-1"> <motion.div aria-hidden className="h-1.5 rounded-full bg-white/20" initial={false} animate={{ width: 36 + index * 12, opacity: atTop ? 0.6 : 1 }} transition={reduceMotion ? { duration: 0 } : { type: "spring", stiffness: 420, damping: 42 }} /> </div> <div className="flex shrink-0 items-start justify-between gap-4 px-5 pt-1 pb-3"> <div className="min-w-0"> <DrawerPrimitive.Title className="text-lg leading-tight font-semibold tracking-tight">{title}</DrawerPrimitive.Title> {description && ( <DrawerPrimitive.Description className="mt-1 text-sm text-muted-foreground">{description}</DrawerPrimitive.Description> )} </div> <div role="group" aria-label="Drawer height" className="flex shrink-0 items-center gap-1 pt-1" data-vaul-no-drag> {snapPoints.map((point, i) => ( <button key={String(point)} type="button" aria-label={labelFor(i)} aria-pressed={i === index} onClick={() => setSnap(point)} className="relative flex size-6 items-center justify-center rounded-full outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50" > {i === index && ( <motion.span layoutId={`${id}-snap`} className="absolute inset-1 rounded-full bg-white/10" transition={reduceMotion ? { duration: 0 } : { type: "spring", stiffness: 500, damping: 44 }} /> )} <span className={cn( "relative size-1.5 rounded-full transition-colors duration-200", i === index ? "bg-primary" : "bg-white/25" )} /> </button> ))} </div> </div> {peek && <div className="shrink-0 px-5 pb-3">{peek}</div>} <div className={cn( "min-h-0 flex-1 px-5 pb-[max(1.25rem,env(safe-area-inset-bottom))]", atTop ? "overflow-y-auto overscroll-contain" : "overflow-hidden" )} > {children} </div> </DrawerPrimitive.Content> </DrawerPrimitive.Portal> </DrawerPrimitive.Root> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| trigger | ReactElement | — | Element that opens the drawer. Omit when controlling `open`. |
| open / defaultOpen / onOpenChange | boolean | false | Controlled or uncontrolled state. |
| snapPoints | (number | string)[] | [0.3, 0.6, 1] | Resting heights, smallest first: screen fractions or "160px". |
| snap / onSnapChange | number | string | null | — | Controlled resting point. |
| snapLabels | string[] | — | Accessible names for the indicator buttons. |
| title / description | ReactNode | — | Dialog title and description. |
| peek | ReactNode | — | Row shown at every height, under the title. |
| nonModal | boolean | false | No backdrop; the page stays interactive. |
API Reference
Same parts as shadcn/ui: Drawer, DrawerTrigger, DrawerPortal, DrawerOverlay, DrawerContent, DrawerHeader, DrawerFooter, DrawerTitle, DrawerDescription and DrawerClose. Drawer takes every vaul Root prop.
| Prop | Type | Default | Description |
|---|---|---|---|
| direction | "top" | "right" | "bottom" | "left" | "bottom" | On Drawer: the edge it slides from. |
| snapPoints / activeSnapPoint / setActiveSnapPoint | (number | string)[] | — | On Drawer: resting heights and the controlled current one. |
| dismissible | boolean | true | On Drawer: allow drag, Escape and backdrop to close it. |
| handleOnly | boolean | false | On Drawer: only vaul's Drawer.Handle starts a drag. |
| shouldScaleBackground | boolean | false | On Drawer: scale a [data-vaul-drawer-wrapper] element behind it. |
| showHandle | boolean | true | On DrawerContent: the handle bar on bottom drawers. |
Accessibility
- Built on vaul, which uses Radix Dialog: focus is trapped inside, Escape closes, and the title and description are wired to aria-labelledby / aria-describedby.
- Dragging is a pointer enhancement — every drawer can also be closed with Escape, the backdrop or a close button.
- The snap drawer's indicator dots are labelled buttons with aria-pressed, so keyboard users can change its height.
- Under prefers-reduced-motion drawers fade in and out instead of sliding, and snap changes jump.