無料押す
サイドパネル
左、右、下からスライドする浮き上がったドロワーで、暗くしたスクリムの上に重なります。
使いどころ
- 一覧の横に出すフィルター、カート、詳細表示
- モバイルのナビゲーションや設定
注意点
- フォーカスを閉じ込め、閉じたらトリガーに戻す
- ドロワーを複数重ねて開かない
エージェントに追加してもらう
インストールコマンド、使いどころ、props、デザインルールを一度にコピーできます。Claude Code、Cursor、Codex に貼り付けてください。
CLI でインストール
shadcn CLI を使います。ソースはプロジェクトにコピーされるので、すべての行があなたのものです。
$ npx shadcn@latest add https://sunui.synvize.com/r/side-panel.json手動でインストール
- 1. 依存パッケージをインストールします。
$ npm install clsx tailwind-merge framer-motion - 2.
cnヘルパーがなければlib/utils.tsに追加します。shadcn/ui を使っていない場合は、色のトークン用に sunui-theme.css も追加します(手順)。そのあとコンポーネントをコピーします。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; }
似ているブロック
- Pro
- Pro





