Sheet
The shadcn/ui Sheet (side drawer) with a short slide, dimmed backdrop and scrollable content — plus two animated React sheet blocks built with Motion and Radix: stacked sheets where the parent scales back as a new one opens, and a side panel you can drag closed.
- Drop-in shadcn API
- Four sides
- Radix focus trap
- Drag to close
- Motion blocks
"use client";
import type * as React from "react";
import { ChevronRight, CreditCard, KeyRound, Smartphone } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
StackedSheet,
StackedSheetClose,
StackedSheetContent,
StackedSheetTrigger,
} from "@/components/dusk/sheet/stacked-sheet";
function Row({ icon, title, hint }: { icon: React.ReactNode; title: string; hint: string }) {
return (
<span className="flex w-full items-center gap-3 rounded-lg border px-3 py-3 text-left transition-colors hover:bg-accent/60">
<span className="flex size-8 items-center justify-center rounded-md border bg-white/[0.03] text-muted-foreground [&_svg]:size-4">
{icon}
</span>
<span className="flex-1">
<span className="block text-sm font-medium">{title}</span>
<span className="block text-xs text-muted-foreground">{hint}</span>
</span>
<ChevronRight className="size-4 text-muted-foreground" aria-hidden />
</span>
);
}
export default function StackedSheetDemo() {
return (
<StackedSheet>
<StackedSheetTrigger asChild>
<Button variant="outline">Account settings</Button>
</StackedSheetTrigger>
<StackedSheetContent title="Account" description="Open a section — this sheet steps back underneath.">
<div className="grid gap-2">
<StackedSheet>
<StackedSheetTrigger className="rounded-lg outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50">
<Row icon={<KeyRound />} title="Security" hint="Password and two-factor" />
</StackedSheetTrigger>
<StackedSheetContent title="Security" description="Keep your account safe.">
<div className="grid gap-2">
<StackedSheet>
<StackedSheetTrigger className="rounded-lg outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50">
<Row icon={<Smartphone />} title="Two-factor auth" hint="Add an authenticator app" />
</StackedSheetTrigger>
<StackedSheetContent
title="Two-factor auth"
description="Enter the 6-digit code from your app."
footer={
<StackedSheetClose asChild>
<Button onClick={() => toast.success("Two-factor enabled")}>Verify</Button>
</StackedSheetClose>
}
>
<label htmlFor="otp" className="mb-2 block text-sm font-medium">Code</label>
<Input id="otp" inputMode="numeric" autoComplete="one-time-code" placeholder="123 456" />
</StackedSheetContent>
</StackedSheet>
<p className="px-1 pt-2 text-xs text-muted-foreground">Last password change: 3 months ago.</p>
</div>
</StackedSheetContent>
</StackedSheet>
<StackedSheet>
<StackedSheetTrigger className="rounded-lg outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50">
<Row icon={<CreditCard />} title="Billing" hint="Pro plan · renews Dec 1" />
</StackedSheetTrigger>
<StackedSheetContent title="Billing" description="Manage your plan and invoices.">
<ul className="grid gap-2 text-sm">
{["Nov 2026", "Oct 2026", "Sep 2026"].map((month) => (
<li key={month} className="flex justify-between rounded-lg border px-3 py-2.5">
<span>{month}</span>
<span className="text-muted-foreground">$24.00</span>
</li>
))}
</ul>
</StackedSheetContent>
</StackedSheet>
</div>
</StackedSheetContent>
</StackedSheet>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/sheet.json1. Install the dependencies.
npm install @radix-ui/react-dialog lucide-react2. Copy the source into
components/ui/sheet.tsx.sheet.tsx "use client" import * as React from "react" import * as SheetPrimitive from "@radix-ui/react-dialog" import { XIcon } from "lucide-react" import { cn } from "@/lib/utils" /** * DuskUI Sheet — a drop-in upgrade of the shadcn/ui Sheet. * * Same parts, props and data-slot attributes as shadcn (including `side`), plus: * - Slides in with a short ease-out and a slight fade, and closes faster than * it opens so dismissing never feels sluggish. * - A dimmed backdrop and a neutral shadow. * - Long content scrolls inside the sheet instead of overflowing the screen. * - A larger close button with hover and focus-visible states, and an * optional `showCloseButton={false}`. * - Respects safe-area insets on notched phones. * - Under `prefers-reduced-motion` it only fades (no slide). * CSS only (tw-animate-css), powered by Radix Dialog for focus trap, Escape and aria. */ function Sheet({ ...props }: React.ComponentProps<typeof SheetPrimitive.Root>) { return <SheetPrimitive.Root data-slot="sheet" {...props} /> } /** The element that opens the sheet. Use `asChild` to render your own Button. */ function SheetTrigger({ ...props }: React.ComponentProps<typeof SheetPrimitive.Trigger>) { return <SheetPrimitive.Trigger data-slot="sheet-trigger" {...props} /> } /** Closes the sheet. Use `asChild` to render your own Button. */ function SheetClose({ ...props }: React.ComponentProps<typeof SheetPrimitive.Close>) { return <SheetPrimitive.Close data-slot="sheet-close" {...props} /> } /** Portals the overlay and content to `document.body`. */ function SheetPortal({ ...props }: React.ComponentProps<typeof SheetPrimitive.Portal>) { return <SheetPrimitive.Portal data-slot="sheet-portal" {...props} /> } /** The dimmed backdrop. Clicking it closes the sheet. */ function SheetOverlay({ className, ...props }: React.ComponentProps<typeof SheetPrimitive.Overlay>) { return ( <SheetPrimitive.Overlay data-slot="sheet-overlay" className={cn( "fixed inset-0 z-50 bg-black/60", "data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:duration-250", "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:duration-200", className )} {...props} /> ) } const sideClasses = { right: [ "inset-y-0 right-0 h-full w-3/4 border-l sm:max-w-sm", "pr-[env(safe-area-inset-right)]", "data-[state=open]:slide-in-from-right data-[state=closed]:slide-out-to-right", ], left: [ "inset-y-0 left-0 h-full w-3/4 border-r sm:max-w-sm", "pl-[env(safe-area-inset-left)]", "data-[state=open]:slide-in-from-left data-[state=closed]:slide-out-to-left", ], top: [ "inset-x-0 top-0 h-auto max-h-[calc(100dvh-2rem)] border-b", "pt-[env(safe-area-inset-top)]", "data-[state=open]:slide-in-from-top data-[state=closed]:slide-out-to-top", ], bottom: [ "inset-x-0 bottom-0 h-auto max-h-[calc(100dvh-2rem)] border-t", "pb-[env(safe-area-inset-bottom)]", "data-[state=open]:slide-in-from-bottom data-[state=closed]:slide-out-to-bottom", ], } as const /** The sheet surface. Slides in from `side` with its overlay. */ function SheetContent({ className, children, side = "right", showCloseButton = true, ...props }: React.ComponentProps<typeof SheetPrimitive.Content> & { /** Edge of the screen the sheet slides in from. */ side?: "top" | "right" | "bottom" | "left" /** Render the X button in the top-right corner. */ showCloseButton?: boolean }) { return ( <SheetPortal> <SheetOverlay /> <SheetPrimitive.Content data-slot="sheet-content" data-side={side} className={cn( "bg-background fixed z-50 flex flex-col gap-4 overflow-y-auto overscroll-contain shadow-lg outline-none", "ease-out", "data-[state=open]:animate-in data-[state=open]:fade-in-50 data-[state=open]:duration-250", "data-[state=closed]:animate-out data-[state=closed]:fade-out-50 data-[state=closed]:duration-200", sideClasses[side], // Reduced motion: fade only, no slide. "motion-reduce:data-[state=open]:fade-in-0 motion-reduce:data-[state=closed]:fade-out-0", "motion-reduce:data-[state=open]:slide-in-from-left-0 motion-reduce:data-[state=open]:slide-in-from-right-0 motion-reduce:data-[state=open]:slide-in-from-top-0 motion-reduce:data-[state=open]:slide-in-from-bottom-0", "motion-reduce:data-[state=closed]:slide-out-to-left-0 motion-reduce:data-[state=closed]:slide-out-to-right-0 motion-reduce:data-[state=closed]:slide-out-to-top-0 motion-reduce:data-[state=closed]:slide-out-to-bottom-0", className )} {...props} > {children} {showCloseButton && ( <SheetPrimitive.Close data-slot="sheet-close" className={cn( "absolute top-3 right-3 inline-flex size-8 items-center justify-center rounded-md text-muted-foreground outline-none", "transition-[color,background-color,box-shadow] duration-150 hover:bg-accent hover:text-foreground", "focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none", "[&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4" )} > <XIcon /> <span className="sr-only">Close</span> </SheetPrimitive.Close> )} </SheetPrimitive.Content> </SheetPortal> ) } /** Wraps the title and description. Leaves room for the close button. */ function SheetHeader({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="sheet-header" className={cn("flex flex-col gap-1.5 p-4 pr-12", className)} {...props} /> ) } /** Action row, pinned to the bottom of the sheet. */ function SheetFooter({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="sheet-footer" className={cn("mt-auto flex flex-col gap-2 p-4", className)} {...props} /> ) } /** Accessible title, announced when the sheet opens. Required by Radix. */ function SheetTitle({ className, ...props }: React.ComponentProps<typeof SheetPrimitive.Title>) { return ( <SheetPrimitive.Title data-slot="sheet-title" className={cn("text-foreground text-lg leading-none font-semibold tracking-tight", className)} {...props} /> ) } /** Supporting text linked to the sheet via aria-describedby. */ function SheetDescription({ className, ...props }: React.ComponentProps<typeof SheetPrimitive.Description>) { return ( <SheetPrimitive.Description data-slot="sheet-description" className={cn("text-muted-foreground text-sm leading-relaxed", className)} {...props} /> ) } export { Sheet, SheetTrigger, SheetClose, SheetContent, SheetHeader, SheetFooter, SheetTitle, SheetDescription, SheetPortal, SheetOverlay, }
Replaces components/ui/sheet.tsx with the same exports and props (including side), so the shadcn Sidebar's mobile drawer keeps working. Adds an optional showCloseButton and exports SheetPortal / SheetOverlay. Animations are CSS (tw-animate-css), so the base needs no Motion.
Usage
import { Button } from "@/components/ui/button"
import {
Sheet,
SheetContent,
SheetDescription,
SheetHeader,
SheetTitle,
SheetTrigger,
} from "@/components/ui/sheet"
export function FiltersSheet() {
return (
<Sheet>
<SheetTrigger asChild>
<Button variant="outline">Filters</Button>
</SheetTrigger>
<SheetContent side="left">
<SheetHeader>
<SheetTitle>Filters</SheetTitle>
<SheetDescription>Narrow down the results.</SheetDescription>
</SheetHeader>
{/* … */}
</SheetContent>
</Sheet>
)
}Examples
Default
Slides in with a short ease-out over a dimmed backdrop and closes faster than it opened. The footer stays pinned to the bottom, long content scrolls inside the sheet, and the close button has proper hover and focus states.
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
Sheet,
SheetClose,
SheetContent,
SheetDescription,
SheetFooter,
SheetHeader,
SheetTitle,
SheetTrigger,
} from "@/components/ui/sheet";
export default function SheetBasicDemo() {
return (
<Sheet>
<SheetTrigger asChild>
<Button variant="outline">Edit profile</Button>
</SheetTrigger>
<SheetContent>
<SheetHeader>
<SheetTitle>Edit profile</SheetTitle>
<SheetDescription>Changes are visible to your team right away.</SheetDescription>
</SheetHeader>
<div className="grid gap-4 px-4">
<div className="grid gap-2">
<label htmlFor="sheet-name" className="text-sm font-medium">Name</label>
<Input id="sheet-name" defaultValue="Ada Kim" />
</div>
<div className="grid gap-2">
<label htmlFor="sheet-handle" className="text-sm font-medium">Username</label>
<Input id="sheet-handle" defaultValue="@ada" />
</div>
</div>
<SheetFooter>
<SheetClose asChild>
<Button>Save changes</Button>
</SheetClose>
<SheetClose asChild>
<Button variant="outline">Cancel</Button>
</SheetClose>
</SheetFooter>
</SheetContent>
</Sheet>
);
}Sides
Use side to slide in from top, right (default), bottom or left. Each side respects the safe-area inset on notched phones and casts its shadow away from its edge.
import { Button } from "@/components/ui/button";
import {
Sheet,
SheetContent,
SheetDescription,
SheetHeader,
SheetTitle,
SheetTrigger,
} from "@/components/ui/sheet";
const sides = ["top", "right", "bottom", "left"] as const;
export default function SheetSidesDemo() {
return (
<div className="grid grid-cols-2 gap-2">
{sides.map((side) => (
<Sheet key={side}>
<SheetTrigger asChild>
<Button variant="outline" className="capitalize">
{side}
</Button>
</SheetTrigger>
<SheetContent side={side}>
<SheetHeader>
<SheetTitle className="capitalize">{side} sheet</SheetTitle>
<SheetDescription>
Slides in from the {side} on an expo-out curve and closes faster than it opened.
</SheetDescription>
</SheetHeader>
<div className="px-4 pb-6 text-sm text-muted-foreground">
Press Escape, click the backdrop or use the close button. Focus returns to the button
that opened it.
</div>
</SheetContent>
</Sheet>
))}
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Stacked Sheet
Nest a StackedSheet inside another one's content and the new sheet slides over it while the one underneath scales back, shifts aside and dims — three levels deep in this demo. Nested sheets get a back button, and closing one brings the sheet below forward again. Escape and outside clicks close only the top sheet.
"use client";
import type * as React from "react";
import { ChevronRight, CreditCard, KeyRound, Smartphone } from "lucide-react";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
StackedSheet,
StackedSheetClose,
StackedSheetContent,
StackedSheetTrigger,
} from "@/components/dusk/sheet/stacked-sheet";
function Row({ icon, title, hint }: { icon: React.ReactNode; title: string; hint: string }) {
return (
<span className="flex w-full items-center gap-3 rounded-lg border px-3 py-3 text-left transition-colors hover:bg-accent/60">
<span className="flex size-8 items-center justify-center rounded-md border bg-white/[0.03] text-muted-foreground [&_svg]:size-4">
{icon}
</span>
<span className="flex-1">
<span className="block text-sm font-medium">{title}</span>
<span className="block text-xs text-muted-foreground">{hint}</span>
</span>
<ChevronRight className="size-4 text-muted-foreground" aria-hidden />
</span>
);
}
export default function StackedSheetDemo() {
return (
<StackedSheet>
<StackedSheetTrigger asChild>
<Button variant="outline">Account settings</Button>
</StackedSheetTrigger>
<StackedSheetContent title="Account" description="Open a section — this sheet steps back underneath.">
<div className="grid gap-2">
<StackedSheet>
<StackedSheetTrigger className="rounded-lg outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50">
<Row icon={<KeyRound />} title="Security" hint="Password and two-factor" />
</StackedSheetTrigger>
<StackedSheetContent title="Security" description="Keep your account safe.">
<div className="grid gap-2">
<StackedSheet>
<StackedSheetTrigger className="rounded-lg outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50">
<Row icon={<Smartphone />} title="Two-factor auth" hint="Add an authenticator app" />
</StackedSheetTrigger>
<StackedSheetContent
title="Two-factor auth"
description="Enter the 6-digit code from your app."
footer={
<StackedSheetClose asChild>
<Button onClick={() => toast.success("Two-factor enabled")}>Verify</Button>
</StackedSheetClose>
}
>
<label htmlFor="otp" className="mb-2 block text-sm font-medium">Code</label>
<Input id="otp" inputMode="numeric" autoComplete="one-time-code" placeholder="123 456" />
</StackedSheetContent>
</StackedSheet>
<p className="px-1 pt-2 text-xs text-muted-foreground">Last password change: 3 months ago.</p>
</div>
</StackedSheetContent>
</StackedSheet>
<StackedSheet>
<StackedSheetTrigger className="rounded-lg outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50">
<Row icon={<CreditCard />} title="Billing" hint="Pro plan · renews Dec 1" />
</StackedSheetTrigger>
<StackedSheetContent title="Billing" description="Manage your plan and invoices.">
<ul className="grid gap-2 text-sm">
{["Nov 2026", "Oct 2026", "Sep 2026"].map((month) => (
<li key={month} className="flex justify-between rounded-lg border px-3 py-2.5">
<span>{month}</span>
<span className="text-muted-foreground">$24.00</span>
</li>
))}
</ul>
</StackedSheetContent>
</StackedSheet>
</div>
</StackedSheetContent>
</StackedSheet>
);
}$ npx shadcn@latest add https://duskui.com/r/stacked-sheet.json1. Install the dependencies.
npm install motion @radix-ui/react-dialog lucide-react2. Copy the source into
components/dusk/sheet/stacked-sheet.tsx.stacked-sheet.tsx "use client"; import * as React from "react"; import * as DialogPrimitive from "@radix-ui/react-dialog"; import { AnimatePresence, motion, useReducedMotion, type Transition } from "motion/react"; import { ChevronLeft, XIcon } from "lucide-react"; import { cn } from "@/lib/utils"; type Side = "left" | "right"; type StackedSheetContextValue = { /** 0 for the first sheet, 1 for a sheet opened from it, and so on. */ depth: number; side: Side; open: boolean; /** How many sheets are open on top of this one. */ above: number; setAbove: (count: number) => void; }; const StackedSheetContext = React.createContext<StackedSheetContextValue | null>(null); function useStackedSheet() { const context = React.useContext(StackedSheetContext); if (!context) throw new Error("StackedSheet parts must be used inside <StackedSheet>."); return context; } const slide: Transition = { type: "spring", duration: 0.35, bounce: 0 }; const recede: Transition = { type: "spring", duration: 0.35, bounce: 0 }; export interface StackedSheetProps { open?: boolean; defaultOpen?: boolean; onOpenChange?: (open: boolean) => void; /** Edge the sheets slide in from. Nested sheets inherit it from the first one. */ side?: Side; children?: React.ReactNode; } /** * Side sheets that stack. Open a StackedSheet from inside another one and the * new sheet slides over it while the one underneath scales back, shifts aside * and dims, so you can see the path you took. Closing a sheet brings the one * below forward again. Each level is a Radix Dialog: focus moves into the top * sheet, Escape and outside clicks close only the top one, and focus returns * to the trigger that opened it. Under reduced motion sheets fade and nothing * scales. */ export function StackedSheet({ open: openProp, defaultOpen = false, onOpenChange, side: sideProp, children }: StackedSheetProps) { const parent = React.useContext(StackedSheetContext); const [internalOpen, setInternalOpen] = React.useState(defaultOpen); const [above, setAbove] = React.useState(0); const open = openProp ?? internalOpen; const depth = parent ? parent.depth + 1 : 0; const side = parent?.side ?? sideProp ?? "right"; // Tell the sheet underneath how many sheets are now stacked on top of it. const reportToParent = parent?.setAbove; React.useEffect(() => { reportToParent?.(open ? above + 1 : 0); }, [reportToParent, open, above]); React.useEffect(() => () => reportToParent?.(0), [reportToParent]); const value = React.useMemo(() => ({ depth, side, open, above, setAbove }), [depth, side, open, above]); return ( <StackedSheetContext.Provider value={value}> <DialogPrimitive.Root open={open} onOpenChange={(next) => { if (openProp === undefined) setInternalOpen(next); onOpenChange?.(next); }} > {children} </DialogPrimitive.Root> </StackedSheetContext.Provider> ); } /** The element that opens this sheet. Use `asChild` to render your own Button. */ export const StackedSheetTrigger = DialogPrimitive.Trigger; /** Closes this sheet. Use `asChild` to render your own Button. */ export const StackedSheetClose = DialogPrimitive.Close; export interface StackedSheetContentProps { title: React.ReactNode; description?: React.ReactNode; /** The sheet body. Scrolls when taller than the screen. */ children?: React.ReactNode; /** Actions pinned to the bottom. */ footer?: React.ReactNode; /** Render the X button. Nested sheets also get a back button. */ showCloseButton?: boolean; className?: string; } /** The sheet surface. Slides in, and recedes when another sheet opens on top. */ export function StackedSheetContent({ title, description, children, footer, showCloseButton = true, className, }: StackedSheetContentProps) { const { depth, side, open, above } = useStackedSheet(); const reduceMotion = useReducedMotion() ?? false; const offscreen = side === "right" ? "calc(100% + 1rem)" : "calc(-100% - 1rem)"; const shift = side === "right" ? -28 : 28; const covered = above > 0; return ( <AnimatePresence> {open && ( // forceMount hands mounting to AnimatePresence so the exit animation can play. <DialogPrimitive.Portal forceMount> <DialogPrimitive.Overlay forceMount asChild> <motion.div className={cn("fixed inset-0 z-50", depth === 0 ? "bg-black/60 backdrop-blur-[2px]" : "bg-black/20")} initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={{ duration: 0.25 }} /> </DialogPrimitive.Overlay> <DialogPrimitive.Content forceMount asChild> <motion.div className={cn( "fixed inset-y-0 z-50 flex w-full max-w-md p-2 outline-none", side === "right" ? "right-0" : "left-0" )} initial={reduceMotion ? { opacity: 0 } : { x: offscreen }} animate={reduceMotion ? { opacity: 1 } : { x: 0 }} exit={reduceMotion ? { opacity: 0 } : { x: offscreen }} transition={slide} > <motion.div className={cn( "relative flex min-h-0 flex-1 flex-col overflow-hidden rounded-xl border bg-background", "shadow-lg", className )} initial={false} animate={ reduceMotion ? {} : { scale: covered ? 1 - 0.04 * Math.min(above, 3) : 1, x: covered ? shift * Math.min(above, 3) : 0 } } transition={recede} > <div className="flex shrink-0 items-start gap-3 px-5 pt-5 pb-3"> {depth > 0 && ( <DialogPrimitive.Close className="-mt-1 -ml-2 inline-flex size-8 shrink-0 items-center justify-center rounded-md text-muted-foreground outline-none transition-colors hover:bg-accent hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50" > <ChevronLeft className="size-4" aria-hidden /> <span className="sr-only">Back</span> </DialogPrimitive.Close> )} <div className="grid flex-1 gap-1.5"> <DialogPrimitive.Title className="text-lg leading-none font-semibold tracking-tight"> {title} </DialogPrimitive.Title> {description ? ( <DialogPrimitive.Description className="text-sm leading-relaxed text-muted-foreground"> {description} </DialogPrimitive.Description> ) : null} </div> {showCloseButton && ( <DialogPrimitive.Close className="-mt-1 -mr-2 inline-flex size-8 shrink-0 items-center justify-center rounded-md text-muted-foreground outline-none transition-colors hover:bg-accent hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50"> <XIcon className="size-4" aria-hidden /> <span className="sr-only">Close</span> </DialogPrimitive.Close> )} </div> <div className="min-h-0 flex-1 overflow-y-auto overscroll-contain px-5 py-3">{children}</div> {footer && ( <div className="flex shrink-0 flex-col-reverse gap-2 border-t px-5 py-4 sm:flex-row sm:justify-end"> {footer} </div> )} {/* Dims the sheet while another one covers it. */} <motion.div aria-hidden className="pointer-events-none absolute inset-0 bg-black" initial={false} animate={{ opacity: covered ? 0.45 : 0 }} transition={{ duration: 0.3 }} /> </motion.div> </motion.div> </DialogPrimitive.Content> </DialogPrimitive.Portal> )} </AnimatePresence> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| open / defaultOpen / onOpenChange | boolean / boolean / (open: boolean) => void | — | Open state (on StackedSheet). |
| side | "right" | "left" | "right" | Edge the sheets slide from. Nested sheets inherit it. |
| StackedSheetTrigger / StackedSheetClose | Radix Trigger / Close props | — | Use asChild to render your own Button. |
| title / description | ReactNode | — | Accessible title and description (on StackedSheetContent). |
| footer | ReactNode | — | Actions pinned to the bottom (on StackedSheetContent). |
| showCloseButton | boolean | true | Render the X button (on StackedSheetContent). |
Side Panel
A side sheet you can drag closed. Grab the edge handle or the header and pull toward the edge: a flick or a drag past closeThreshold dismisses it, anything less springs back, and the backdrop fades as you drag. The drag is a separate motion value from the slide-in, so the two never fight.
"use client";
import * as React from "react";
import { MessageSquare } from "lucide-react";
import { Button } from "@/components/ui/button";
import { SidePanel } from "@/components/dusk/sheet/side-panel";
const comments = [
{ name: "Ada Kim", time: "2m", text: "Love the new spring on the dock — feels much snappier." },
{ name: "Sam Patel", time: "18m", text: "Can we try a shorter exit on the dialog? 150ms maybe." },
{ name: "Lee Wong", time: "1h", text: "Reduced motion mode looks right on my end." },
{ name: "Maya Ruiz", time: "3h", text: "Shipping this to staging after lunch." },
];
export default function SidePanelDemo() {
const [open, setOpen] = React.useState(false);
return (
<SidePanel
open={open}
onOpenChange={setOpen}
trigger={
<Button variant="outline">
<MessageSquare />
Comments
</Button>
}
title="Comments"
description="Drag the edge handle or header to the right, or flick, to close."
footer={<Button onClick={() => setOpen(false)}>Done</Button>}
>
<ul className="grid gap-3">
{comments.map((comment) => (
<li key={comment.name} className="rounded-xl border bg-card/50 p-3">
<div className="mb-1 flex items-center justify-between text-xs">
<span className="font-medium text-foreground">{comment.name}</span>
<span className="text-muted-foreground">{comment.time}</span>
</div>
<p className="text-sm leading-relaxed text-muted-foreground">{comment.text}</p>
</li>
))}
</ul>
</SidePanel>
);
}$ npx shadcn@latest add https://duskui.com/r/side-panel.json1. Install the dependencies.
npm install motion @radix-ui/react-dialog lucide-react2. Copy the source into
components/dusk/sheet/side-panel.tsx.side-panel.tsx "use client"; import * as React from "react"; import * as DialogPrimitive from "@radix-ui/react-dialog"; import { animate, AnimatePresence, motion, useDragControls, useMotionValue, useReducedMotion, useTransform, type PanInfo, } from "motion/react"; import { XIcon } from "lucide-react"; import { cn } from "@/lib/utils"; type Side = "left" | "right"; export interface SidePanelProps { /** Element that opens the panel, usually a Button. Omit when controlling `open` yourself. */ trigger?: React.ReactElement; open?: boolean; defaultOpen?: boolean; onOpenChange?: (open: boolean) => void; title: React.ReactNode; description?: React.ReactNode; /** The panel body. Scrolls when taller than the screen. */ children?: React.ReactNode; /** Actions pinned to the bottom. */ footer?: React.ReactNode; /** Edge the panel slides in from. */ side?: Side; /** Render an X button next to the title. */ showCloseButton?: boolean; /** Fraction of the panel's width it must be dragged to close (0–1). */ closeThreshold?: number; className?: string; } const slide = { type: "spring", duration: 0.35, bounce: 0 } as const; /** * A side sheet you can drag closed. Grab the edge handle or the header and * pull the panel toward its edge — a fast flick or a drag past * `closeThreshold` dismisses it, anything less springs back, and the backdrop * fades as you drag. Built on Radix Dialog, so Escape, the backdrop and the * close button work, focus is trapped inside and returns to the trigger. With * reduced motion it simply fades. */ export function SidePanel({ trigger, open: openProp, defaultOpen = false, onOpenChange, title, description, children, footer, side = "right", showCloseButton = true, closeThreshold = 0.35, className, }: SidePanelProps) { const reduceMotion = useReducedMotion(); const [internalOpen, setInternalOpen] = React.useState(defaultOpen); const open = openProp ?? internalOpen; const panelRef = React.useRef<HTMLDivElement>(null); const dragControls = useDragControls(); // Drag offset in px, kept apart from the enter/exit slide so they don't fight. const dragX = useMotionValue(0); const sign = side === "right" ? 1 : -1; const backdropOpacity = useTransform(dragX, (x) => Math.max(0, 1 - (x * sign) / 360)); const offscreen = side === "right" ? "100%" : "-100%"; const setOpen = (next: boolean) => { if (openProp === undefined) setInternalOpen(next); onOpenChange?.(next); }; const handleDragEnd = (_: PointerEvent, info: PanInfo) => { const width = panelRef.current?.offsetWidth ?? 400; if (info.offset.x * sign > width * closeThreshold || info.velocity.x * sign > 600) { setOpen(false); } else { animate(dragX, 0, { type: "spring", duration: 0.3, bounce: 0.1 }); } }; const startDrag = (event: React.PointerEvent) => { // Let buttons (the close X) receive their clicks. if ((event.target as HTMLElement).closest("button, a, input, textarea, select")) return; dragControls.start(event); }; return ( <DialogPrimitive.Root open={open} onOpenChange={setOpen}> {trigger && <DialogPrimitive.Trigger asChild>{trigger}</DialogPrimitive.Trigger>} <AnimatePresence onExitComplete={() => dragX.set(0)}> {open && ( // forceMount hands mounting to AnimatePresence so exit animations can play. <DialogPrimitive.Portal forceMount> <DialogPrimitive.Overlay forceMount asChild> <motion.div className="fixed inset-0 z-50" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={{ duration: 0.25 }} > <motion.div className="absolute inset-0 bg-black/60 backdrop-blur-[2px]" style={{ opacity: backdropOpacity }} /> </motion.div> </DialogPrimitive.Overlay> <DialogPrimitive.Content forceMount asChild> <motion.div ref={panelRef} className={cn( "fixed inset-y-0 z-50 flex w-[calc(100%-2.5rem)] max-w-sm outline-none", side === "right" ? "right-0" : "left-0" )} initial={reduceMotion ? { opacity: 0 } : { x: offscreen }} animate={reduceMotion ? { opacity: 1 } : { x: 0 }} exit={reduceMotion ? { opacity: 0 } : { x: offscreen }} transition={slide} > {/* The surface carries the drag, so the slide-in `x` above stays independent. */} <motion.div className={cn( "relative flex min-h-0 flex-1 flex-col bg-background", side === "right" ? "border-l shadow-lg" : "border-r shadow-lg", className )} style={{ x: dragX }} drag="x" dragControls={dragControls} dragListener={false} dragConstraints={side === "right" ? { left: 0 } : { right: 0 }} dragElastic={side === "right" ? { left: 0.04, right: 1 } : { left: 1, right: 0.04 }} dragMomentum={false} onDragEnd={handleDragEnd} > {/* Edge handle. touch-none stops the page from scrolling instead. */} <div aria-hidden onPointerDown={startDrag} className={cn( "group/handle absolute inset-y-0 flex w-5 cursor-grab touch-none items-center justify-center active:cursor-grabbing", side === "right" ? "-left-5" : "-right-5" )} > <span className="h-12 w-1.5 rounded-full bg-white/20 transition-[height,background-color] duration-200 group-hover/handle:h-16 group-hover/handle:bg-white/40 group-active/handle:bg-white/60" /> </div> {/* The header is a drag zone too. */} <div onPointerDown={startDrag} className="flex shrink-0 cursor-grab touch-none items-start gap-4 px-5 pt-5 pb-3 select-none active:cursor-grabbing" > <div className="grid flex-1 gap-1.5"> <DialogPrimitive.Title className="text-lg leading-none font-semibold tracking-tight"> {title} </DialogPrimitive.Title> {description ? ( <DialogPrimitive.Description className="text-sm leading-relaxed text-muted-foreground"> {description} </DialogPrimitive.Description> ) : null} </div> {showCloseButton && ( <DialogPrimitive.Close className="-mt-1 -mr-2 inline-flex size-8 shrink-0 items-center justify-center rounded-md text-muted-foreground outline-none transition-colors hover:bg-accent hover:text-foreground focus-visible:ring-[3px] focus-visible:ring-ring/50"> <XIcon className="size-4" aria-hidden /> <span className="sr-only">Close</span> </DialogPrimitive.Close> )} </div> <div className="min-h-0 flex-1 overflow-y-auto overscroll-contain px-5 py-3">{children}</div> {footer && ( <div className="flex shrink-0 flex-col-reverse gap-2 border-t px-5 pt-4 pb-[max(1rem,env(safe-area-inset-bottom))] sm:flex-row sm:justify-end"> {footer} </div> )} </motion.div> </motion.div> </DialogPrimitive.Content> </DialogPrimitive.Portal> )} </AnimatePresence> </DialogPrimitive.Root> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| trigger | ReactElement | — | Element that opens the panel. |
| open / defaultOpen / onOpenChange | boolean / boolean / (open: boolean) => void | — | Open state. |
| title / description | ReactNode | — | Accessible title and description. |
| children / footer | ReactNode | — | Scrollable body and pinned actions. |
| side | "right" | "left" | "right" | Edge the panel slides from and drags toward. |
| closeThreshold | number | 0.35 | Fraction of the width it must be dragged to close. |
| showCloseButton | boolean | true | Render the X button. |
API Reference
Same parts as shadcn/ui: Sheet, SheetTrigger, SheetClose, SheetContent, SheetHeader, SheetFooter, SheetTitle and SheetDescription, plus SheetPortal and SheetOverlay. Each forwards the Radix Dialog props.
| Prop | Type | Default | Description |
|---|---|---|---|
| open / defaultOpen / onOpenChange | boolean / boolean / (open: boolean) => void | — | Open state (on Sheet). |
| side | "top" | "right" | "bottom" | "left" | "right" | Edge the sheet slides in from (on SheetContent). |
| showCloseButton | boolean | true | Render the X button (on SheetContent). |
| onOpenAutoFocus / onEscapeKeyDown / onPointerDownOutside | (event) => void | — | Radix hooks; preventDefault() to customise (on SheetContent). |
Accessibility
- Built on Radix Dialog:
role="dialog"witharia-modal, labelled by its title and described by its description. - Focus moves into the sheet, is trapped while it's open and returns to the trigger on close; the page behind can't scroll.
- Escape, the backdrop and the close button close it. In Stacked Sheet they close only the top sheet, and nested sheets have a labelled Back button.
- Side Panel's drag handle is a pointer enhancement — keyboard and screen reader users close it with Escape or the close button.
- Under prefers-reduced-motion sheets fade instead of sliding, and stacked sheets don't scale.