FreePress
Side Panel
Drawer on a raised surface from the left, right or bottom over a dimmed scrim, with focus trap, Escape to close, scroll lock and focus return.
When to use it
- Filters, carts and detail views beside a list
- Mobile navigation and settings
Watch out
- Trap focus and restore it to the trigger on close
- Avoid stacking more than one drawer
Let your agent add it
One copy holds the install command, when to use it, the props and the design rules. Paste it into Claude Code, Cursor or Codex.
Install with the CLI
Uses the shadcn CLI. The source is copied into your project, so you own every line.
$ npx shadcn@latest add https://sunui.synvize.com/r/side-panel.jsonOr install manually
- 1. Install the dependencies.
$ npm install clsx tailwind-merge framer-motion - 2. Add a
cnhelper atlib/utils.tsif you do not have one. Not using shadcn/ui? Also add sunui-theme.css for the colour tokens (how). Then copy the component.side-panel.tsx"use client"; import { AnimatePresence, motion, useReducedMotion, type Variants } from "framer-motion"; import { useEffect, useId, useRef, useState, type KeyboardEvent, type ReactNode, type RefObject } from "react"; import { createPortal } from "react-dom"; import { cn } from "@/lib/utils"; export type SidePanelSide = "left" | "right" | "bottom"; export interface SidePanelProps { open: boolean; onOpenChange: (open: boolean) => void; title: ReactNode; description?: ReactNode; children?: ReactNode; /** Actions area fixed at the bottom */ footer?: ReactNode; side?: SidePanelSide; size?: "sm" | "md" | "lg"; /** Whether clicking the overlay closes it */ dismissOnOverlay?: boolean; /** Element to focus first on open, defaults to the first focusable element in the panel */ initialFocusRef?: RefObject<HTMLElement | null>; /** Render into document.body; when false, render in place (the container needs relative and overflow-hidden) */ portal?: boolean; /** UI language: en, zh-TW, zh-CN, ja; tags such as zh-Hant-HK and ja-JP are also accepted */ locale?: string; /** Override the built-in strings; takes precedence over locale */ labels?: Partial<SidePanelLabels>; className?: string; } export interface SidePanelLabels { close: string; } type SunLocale = "en" | "zh-TW" | "zh-CN" | "ja"; /** Map any language tag to one of the four built-in languages; everything else uses English */ function resolveLocale(tag?: string): SunLocale { const t = (tag ?? "en").toLowerCase(); if (t.startsWith("zh")) return /hant|tw|hk|mo/.test(t) ? "zh-TW" : "zh-CN"; if (t.startsWith("ja")) return "ja"; return "en"; } const MESSAGES: Record<SunLocale, SidePanelLabels> = { en: { close: "Close panel" }, "zh-TW": { close: "關閉面板" }, "zh-CN": { close: "关闭面板" }, ja: { close: "パネルを閉じる" }, }; const sizeClass: Record<SidePanelSide, Record<"sm" | "md" | "lg", string>> = { left: { sm: "w-72", md: "w-96", lg: "w-[32rem]" }, right: { sm: "w-72", md: "w-96", lg: "w-[32rem]" }, bottom: { sm: "h-[35%]", md: "h-[55%]", lg: "h-[80%]" }, }; const sideClass: Record<SidePanelSide, string> = { left: "inset-y-0 left-0 max-w-[calc(100%-3rem)] border-r", right: "inset-y-0 right-0 max-w-[calc(100%-3rem)] border-l", bottom: "inset-x-0 bottom-0 rounded-t-md border-t", }; // Design language (DESIGN.md): a sheet slides in from the edge; surface, border and text colors const surface = "bg-card shadow-[0_1px_2px_rgb(0_0_0/0.05),0_4px_12px_-6px_rgb(0_0_0/0.06)] dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)]"; const edge = "border-foreground/10"; const ink = "text-foreground"; const inkMuted = "text-muted-foreground"; const gentle = { type: "spring", stiffness: 240, damping: 30 } as const; const offscreen: Record<SidePanelSide, { x?: string; y?: string }> = { left: { x: "-100%" }, right: { x: "100%" }, bottom: { y: "100%" }, }; const panelVariants: Variants = { hidden: (c: { side: SidePanelSide; reduce: boolean }) => c.reduce ? { opacity: 0, transition: { duration: 0 } } : { ...offscreen[c.side], opacity: 1, transition: gentle }, visible: (c: { reduce: boolean }) => ({ x: 0, y: 0, opacity: 1, transition: c.reduce ? { duration: 0 } : gentle, }), }; const FOCUSABLE = 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'; // Scroll-lock count shared when several panels are open at once let lockCount = 0; let previousOverflow = ""; export function SidePanel({ open, onOpenChange, title, description, children, footer, side = "right", size = "md", dismissOnOverlay = true, initialFocusRef, portal = true, locale = "en", labels, className, }: SidePanelProps) { const reduce = useReducedMotion() ?? false; const titleId = useId(); const descId = useId(); const panelRef = useRef<HTMLDivElement>(null); const restoreRef = useRef<HTMLElement | null>(null); const [mounted, setMounted] = useState(false); const closeLabel = { ...MESSAGES[resolveLocale(locale)], ...labels }.close; useEffect(() => setMounted(true), []); // Scroll lock and focus restore useEffect(() => { if (!open) return; restoreRef.current = document.activeElement as HTMLElement | null; if (portal) { if (lockCount === 0) { previousOverflow = document.body.style.overflow; document.body.style.overflow = "hidden"; } lockCount++; } const frame = requestAnimationFrame(() => { const target = initialFocusRef?.current ?? panelRef.current?.querySelector<HTMLElement>(FOCUSABLE) ?? panelRef.current; target?.focus(); }); return () => { cancelAnimationFrame(frame); if (portal) { lockCount--; if (lockCount === 0) document.body.style.overflow = previousOverflow; } restoreRef.current?.focus?.(); }; }, [open, portal, initialFocusRef]); const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => { if (e.key === "Escape") { e.stopPropagation(); onOpenChange(false); return; } if (e.key !== "Tab" || !panelRef.current) return; // Focus trap: Tab cycles inside the panel const nodes = Array.from(panelRef.current.querySelectorAll<HTMLElement>(FOCUSABLE)).filter( (n) => n.offsetParent !== null, ); if (nodes.length === 0) { e.preventDefault(); return; } const first = nodes[0]; const last = nodes[nodes.length - 1]; if (e.shiftKey && (document.activeElement === first || document.activeElement === panelRef.current)) { e.preventDefault(); last.focus(); } else if (!e.shiftKey && document.activeElement === last) { e.preventDefault(); first.focus(); } }; const tree = ( <AnimatePresence> {open && ( <div className={cn(portal ? "fixed" : "absolute", "inset-0 z-50")} onKeyDown={onKeyDown}> <motion.div aria-hidden="true" className="absolute inset-0 bg-foreground/30 dark:bg-black/55" initial={{ opacity: 0 }} animate={{ opacity: 1 }} exit={{ opacity: 0 }} transition={{ duration: reduce ? 0 : 0.2, ease: "easeOut" }} onClick={() => dismissOnOverlay && onOpenChange(false)} /> <motion.div ref={panelRef} role="dialog" aria-modal="true" aria-labelledby={titleId} aria-describedby={description ? descId : undefined} tabIndex={-1} custom={{ side, reduce }} variants={panelVariants} initial="hidden" animate="visible" exit="hidden" className={cn( "absolute flex flex-col shadow-[0_1px_2px_rgb(0_0_0/0.05),0_4px_12px_-6px_rgb(0_0_0/0.06)] dark:shadow-[inset_0_1px_0_rgb(255_255_255/0.06)] outline-none", surface, edge, ink, sideClass[side], sizeClass[side][size], className, )} > <header className={cn("flex items-start gap-4 border-b px-5 py-4", edge)}> <div className="min-w-0 flex-1"> <h2 id={titleId} className="text-base leading-tight font-semibold tracking-tight"> {title} </h2> {description && ( <p id={descId} className={cn("mt-1 text-sm", inkMuted)}> {description} </p> )} </div> <button type="button" onClick={() => onOpenChange(false)} aria-label={closeLabel} className={cn( "-mt-1 -mr-1.5 flex size-8 shrink-0 items-center justify-center rounded-md outline-none transition-[color,background-color,border-color,opacity,scale] hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring motion-safe:active:scale-[0.97]", inkMuted, )} > <svg viewBox="0 0 24 24" className="size-4" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" aria-hidden="true" strokeLinejoin="round"> <path d="M6.5 6.5l11 11M17.5 6.5l-11 11" /> </svg> </button> </header> <div className="min-h-0 flex-1 overflow-y-auto overscroll-contain px-5 py-4">{children}</div> {footer && <footer className={cn("border-t px-5 py-3", edge)}>{footer}</footer>} </motion.div> </div> )} </AnimatePresence> ); if (!portal) return tree; return mounted ? createPortal(tree, document.body) : null; }
Similar blocks
- Pro
- Pro





