{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "drawer",
  "type": "registry:ui",
  "title": "Drawer",
  "description": "A temporary side surface for focused work.",
  "categories": [
    "disclosure"
  ],
  "dependencies": [
    "@radix-ui/react-dialog",
    "lucide-react",
    "motion"
  ],
  "registryDependencies": [
    "https://mcp.xzero.co.uk/r/xzero-motion-tokens.json"
  ],
  "files": [
    {
      "path": "registry/components/drawer/drawer.tsx",
      "type": "registry:component",
      "target": "@components/xzero/drawer/drawer.tsx",
      "content": "\"use client\";\n\nimport * as DialogPrimitive from \"@radix-ui/react-dialog\";\nimport { createContext, useCallback, useContext, useMemo, useRef, useState } from \"react\";\nimport type { ComponentPropsWithoutRef, ReactNode, RefObject } from \"react\";\nimport { AnimatePresence, animate, motion, useMotionValue, useReducedMotion } from \"motion/react\";\nimport type { PanInfo, Transition } from \"motion/react\";\nimport { X } from \"lucide-react\";\nimport { motionTokens } from \"../lib/motion-tokens\";\nimport styles from \"./drawer.module.css\";\n\n/**\n * The last item a drawer showed. Pass the selected item (or null when nothing is open) and render the drawer's content\n * from the result, so the content stays on screen while the panel slides out instead of vanishing the moment it closes.\n */\nexport function useDrawerItem<T>(item: T | null | undefined): T | null | undefined {\n  const [last, setLast] = useState(item);\n  if (item != null && item !== last) setLast(item);\n  return item ?? last;\n}\n\n/** Mirrors the open state so the panel can stay mounted while it slides out, retarget mid-flight, and close itself after a drag. */\nconst DrawerContext = createContext<{ open: boolean; flung: boolean; setOpen: (open: boolean) => void; fling: () => void; openedAt: RefObject<number> } | null>(null);\n\nexport function Drawer({ open: openProp, defaultOpen = false, onOpenChange, ...props }: ComponentPropsWithoutRef<typeof DialogPrimitive.Root>) {\n  const [uncontrolled, setUncontrolled] = useState(defaultOpen);\n  // A drag that dismisses the panel hands its velocity to the exit spring; every other close uses the shorter tween.\n  const [flung, setFlung] = useState(false);\n  // When the drawer last opened, so a click that reopens it mid-close is not also read as a click outside the leaving panel.\n  const openedAt = useRef(0);\n  const open = openProp ?? uncontrolled;\n  const setOpen = useCallback((next: boolean) => {\n    setFlung(false);\n    if (next) openedAt.current = performance.now();\n    if (openProp === undefined) setUncontrolled(next);\n    onOpenChange?.(next);\n  }, [openProp, onOpenChange]);\n  const fling = useCallback(() => { setOpen(false); setFlung(true); }, [setOpen]);\n  const value = useMemo(() => ({ open, flung, setOpen, fling, openedAt }), [open, flung, setOpen, fling]);\n  return <DrawerContext.Provider value={value}><DialogPrimitive.Root {...props} open={open} onOpenChange={setOpen} /></DrawerContext.Provider>;\n}\n\nexport const DrawerTrigger = DialogPrimitive.Trigger;\nexport const DrawerClose = DialogPrimitive.Close;\n\nexport interface DrawerContentProps\n  extends ComponentPropsWithoutRef<typeof DialogPrimitive.Content> {\n  title: string;\n  description?: string;\n  children: ReactNode;\n  side?: \"left\" | \"right\" | \"top\" | \"bottom\";\n  /** Renders the drawer inside this element instead of the page body, anchored to its edges. The element needs position: relative and overflow: hidden. */\n  container?: HTMLElement | null;\n}\n\nconst fade: Transition = { duration: motionTokens.duration.instant };\n/* A click or Escape returns the panel quickly; a flick keeps its velocity in a spring of the same length. The shadow fades over the last stretch so it leaves with the panel instead of popping away at unmount. */\nconst shadowOut: Transition = { duration: motionTokens.duration.standard, times: [0, .65, 1], ease: \"linear\" };\nconst leave: Transition = { default: { duration: motionTokens.duration.standard, ease: [...motionTokens.ease.standard] }, opacity: shadowOut };\nconst flingOut: Transition = { default: { ...motionTokens.spring.smooth, visualDuration: motionTokens.duration.standard }, opacity: shadowOut };\n\n/** When the title or description changes while open, the new copy rises in and the old copy leaves upward. */\nfunction SwapText({ text }: { text: string }) {\n  const reduced = useReducedMotion();\n  return (\n    <AnimatePresence mode=\"popLayout\" initial={false}>\n      <motion.span\n        key={text}\n        className={styles.swap}\n        initial={reduced ? false : { opacity: 0, y: \"0.3em\", filter: `blur(${motionTokens.blur.soft}px)` }}\n        animate={{ opacity: 1, y: 0, filter: \"blur(0px)\" }}\n        exit={reduced ? { opacity: 0, transition: { duration: 0 } } : { opacity: 0, y: \"-0.3em\", filter: `blur(${motionTokens.blur.subtle}px)`, transition: { duration: motionTokens.duration.fast, ease: [...motionTokens.ease.standard] } }}\n        transition={{ duration: motionTokens.duration.standard, ease: [...motionTokens.ease.enter] }}\n      >\n        {text}\n      </motion.span>\n    </AnimatePresence>\n  );\n}\n\nexport function DrawerContent({\n  title,\n  description,\n  children,\n  side = \"right\",\n  container,\n  className,\n  onInteractOutside,\n  ...props\n}: DrawerContentProps) {\n  const drawer = useContext(DrawerContext);\n  const reduced = useReducedMotion();\n  const panelRef = useRef<HTMLDivElement>(null);\n  const offset = useMotionValue<number | string>(0);\n  const pan = useRef<number | null>(null);\n  const axis = side === \"left\" || side === \"right\" ? \"x\" : \"y\";\n  const sign = side === \"right\" || side === \"bottom\" ? 1 : -1;\n  const offscreen = { [axis]: `${sign * 100}%` };\n  const classes = [styles.content, className].filter(Boolean).join(\" \");\n  const draggable = drawer !== null && !reduced;\n\n  const panelSize = () => (axis === \"x\" ? panelRef.current?.offsetWidth : panelRef.current?.offsetHeight) ?? 480;\n\n  // The header is the grab handle. It follows the pointer toward the edge and rubber-bands the other way.\n  function panStart(event: PointerEvent) {\n    const target = event.target instanceof Element ? event.target : null;\n    if (!draggable || target?.closest(\"button, a, input, select, textarea, [role='button']\")) return;\n    // The entrance animates in percent of the panel, so a grab mid-flight converts it to pixels.\n    const value = offset.get();\n    pan.current = typeof value === \"number\" ? value : parseFloat(value) / 100 * panelSize();\n    offset.stop();\n  }\n  function panMove(_: PointerEvent, info: PanInfo) {\n    if (pan.current === null) return;\n    const toward = (pan.current + info.offset[axis]) * sign;\n    offset.set(sign * (toward >= 0 ? toward : -Math.sqrt(-toward)));\n  }\n  // Past a third of the panel, or on a quick flick toward the edge, the drawer closes and keeps its velocity; otherwise it springs back.\n  function panEnd(_: PointerEvent, info: PanInfo) {\n    if (pan.current === null) return;\n    pan.current = null;\n    const toward = Number(offset.get()) * sign;\n    if (toward > panelSize() / 3 || (toward > 0 && info.velocity[axis] * sign > 500)) drawer?.fling();\n    else animate(offset, 0, motionTokens.spring.snappy);\n  }\n\n  const inner = <>\n    <motion.div className={[styles.header, draggable ? styles.handle : \"\"].filter(Boolean).join(\" \")} onPanStart={panStart} onPan={panMove} onPanEnd={panEnd}>\n      <div>\n        <DialogPrimitive.Title className={styles.title}><SwapText text={title} /></DialogPrimitive.Title>\n        {description ? (\n          <DialogPrimitive.Description className={styles.description}>\n            <SwapText text={description} />\n          </DialogPrimitive.Description>\n        ) : null}\n      </div>\n      <DialogPrimitive.Close className={styles.close} aria-label=\"Close drawer\">\n        <X size={16} strokeWidth={1.75} aria-hidden=\"true\" />\n      </DialogPrimitive.Close>\n    </motion.div>\n    <div className={styles.body}>{children}</div>\n  </>;\n\n  // Under a bare Radix root the open state is unknown here, so CSS keyframes keyed off data-state animate the layers instead.\n  if (drawer === null) {\n    return (\n      <DialogPrimitive.Portal container={container}>\n        <DialogPrimitive.Overlay className={`${styles.overlay} ${styles.keyframes}`} data-contained={container ? \"\" : undefined} />\n        <DialogPrimitive.Content {...props} onInteractOutside={onInteractOutside} data-side={side} data-contained={container ? \"\" : undefined} className={`${classes} ${styles.keyframes}`}>{inner}</DialogPrimitive.Content>\n      </DialogPrimitive.Portal>\n    );\n  }\n\n  // The panel springs fully opaque from its own edge and returns to it faster than it arrived, from wherever it is.\n  return (\n    <AnimatePresence custom={drawer.flung}>\n      {drawer.open && (\n        <DialogPrimitive.Portal key=\"drawer\" forceMount container={container}>\n          <DialogPrimitive.Overlay asChild forceMount>\n            <motion.div\n              className={styles.overlay}\n              data-contained={container ? \"\" : undefined}\n              initial={{ opacity: 0 }}\n              animate={{ opacity: 1 }}\n              exit={{ opacity: 0, transition: reduced ? fade : { duration: motionTokens.duration.fast, ease: [...motionTokens.ease.standard] } }}\n              transition={reduced ? fade : { duration: motionTokens.duration.standard, ease: [...motionTokens.ease.enter] }}\n            />\n          </DialogPrimitive.Overlay>\n          <DialogPrimitive.Content\n            {...props}\n            asChild\n            forceMount\n            // Radix reads a click outside on click, so the press that reopens a closing drawer would dismiss it again.\n            onInteractOutside={event => {\n              onInteractOutside?.(event);\n              if (event.detail.originalEvent.timeStamp < drawer.openedAt.current) event.preventDefault();\n            }}\n          >\n            <motion.div\n              ref={panelRef}\n              className={classes}\n              data-side={side}\n              data-contained={container ? \"\" : undefined}\n              style={{ [axis]: offset }}\n              variants={{ exit: (flung: boolean) => reduced ? { opacity: 0, transition: fade } : { ...offscreen, opacity: [1, 1, 0], transition: flung ? flingOut : leave } }}\n              initial={reduced ? { opacity: 0 } : { ...offscreen, opacity: 1 }}\n              animate={reduced ? { opacity: 1 } : { [axis]: 0, opacity: 1 }}\n              exit=\"exit\"\n              transition={reduced ? fade : motionTokens.spring.smooth}\n            >\n              {inner}\n            </motion.div>\n          </DialogPrimitive.Content>\n        </DialogPrimitive.Portal>\n      )}\n    </AnimatePresence>\n  );\n}\n\nexport default Drawer;\n"
    },
    {
      "path": "registry/components/drawer/drawer.module.css",
      "type": "registry:component",
      "target": "@components/xzero/drawer/drawer.module.css",
      "content": ".overlay {\n  position: fixed;\n  inset: 0;\n  z-index: 50;\n  background: oklch(10% 0 0 / .38);\n  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);\n}\n\n.content {\n  --drawer-size: min(30rem, calc(100vw - var(--space-4)));\n  /* Critically damped, like a sheet spring: it never overshoots past the edge it is anchored to. */\n  --ease-drawer: cubic-bezier(.32,.72,0,1);\n  --drawer-from: translateX(100%);\n  position: fixed;\n  z-index: 51;\n  display: flex;\n  flex-direction: column;\n  width: var(--drawer-size);\n  max-width: 100vw;\n  max-height: 100dvh;\n  overflow: hidden;\n  border: 1px solid var(--border);\n  background: var(--surface-raised);\n  color: var(--foreground);\n  box-shadow: var(--shadow-floating);\n}\n/* While a layer leaves it lets clicks through, so pressing the trigger again during the close reopens the drawer from wherever it is. */\n/* Radix writes pointer-events inline on the overlay, so the closing state has to win over it. */\n.overlay[data-state=\"closed\"], .content[data-state=\"closed\"] { pointer-events: none !important; }\n\n.content[data-side=\"right\"] { inset: 0 0 0 auto; border-width: 1px 0 1px 1px; border-radius: 22px 0 0 22px; }\n.content[data-side=\"left\"] { inset: 0 auto 0 0; border-width: 1px 1px 1px 0; border-radius: 0 22px 22px 0; }\n.content[data-side=\"top\"] { inset: 0 0 auto; width: 100%; max-width: none; max-height: min(32rem, 100dvh); border-width: 0 0 1px; border-radius: 0 0 22px 22px; }\n.content[data-side=\"bottom\"] { inset: auto 0 0; width: 100%; max-width: none; max-height: min(32rem, 100dvh); border-width: 1px 0 0; border-radius: 22px 22px 0 0; }\n.content[data-side=\"bottom\"]::before { position: absolute; top: 9px; left: 50%; width: 2.25rem; height: 4px; border-radius: var(--radius-pill); background: var(--border-strong); content: \"\"; opacity: .75; transform: translateX(-50%); }\n\n/* Motion springs the panel from its edge. Keyframes only run when DrawerContent sits under a bare Radix root. */\n.content[data-side=\"left\"] { --drawer-from: translateX(-100%); }\n.content[data-side=\"top\"] { --drawer-from: translateY(-100%); }\n.content[data-side=\"bottom\"] { --drawer-from: translateY(100%); }\n.keyframes.overlay[data-state=\"open\"] { animation: overlay-in var(--duration-standard) var(--ease-enter) both; }\n.keyframes.overlay[data-state=\"closed\"] { animation: overlay-out var(--duration-fast) var(--ease-standard) both; }\n.keyframes.content[data-state=\"open\"] { animation: drawer-in var(--duration-considered) var(--ease-drawer) both; }\n.keyframes.content[data-state=\"closed\"] { animation: drawer-out var(--duration-standard) var(--ease-standard) both; }\n.content:focus { outline: none; }\n/* Inside a container the layers anchor to that element instead of the viewport. */\n.overlay[data-contained], .content[data-contained] { position: absolute; }\n.overlay[data-contained] { z-index: 1; background: oklch(10% 0 0 / .16); backdrop-filter: none; }\n.content[data-contained] { --drawer-size: min(22rem, calc(100% - var(--space-8))); z-index: 2; max-height: 100%; box-shadow: none; }\n.content[data-contained][data-side=\"top\"], .content[data-contained][data-side=\"bottom\"] { max-height: min(32rem, calc(100% - var(--space-8))); }\n/* The container already draws the outer edges, so only the inner edge keeps a border. */\n.content[data-contained][data-side] { border-width: 0; }\n.content[data-contained][data-side=\"right\"] { border-left-width: 1px; }\n.content[data-contained][data-side=\"left\"] { border-right-width: 1px; }\n.content[data-contained][data-side=\"top\"] { border-bottom-width: 1px; }\n.content[data-contained][data-side=\"bottom\"] { border-top-width: 1px; }\n\n.header { display: flex; flex: 0 0 auto; align-items: flex-start; justify-content: space-between; gap: var(--space-5); padding: var(--space-6); border-bottom: 1px solid var(--border); }\n/* The header doubles as the drag handle, so touch drags move the panel instead of the page. */\n.handle { touch-action: none; user-select: none; }\n.content[data-side=\"bottom\"] .handle, .content[data-side=\"top\"] .handle { cursor: grab; }\n.title, .description { position: relative; }\n/* The copy sets its own line height so a page-level heading rule cannot squeeze the title. */\n.swap { display: block; line-height: var(--leading-body); }\n.title { margin: 0; font-family: var(--font-body); font-size: var(--text-lg); font-weight: 500; letter-spacing: var(--tracking-body); line-height: var(--leading-body); }\n.description { max-width: 32rem; margin: var(--space-2) 0 0; color: var(--text-secondary); font-size: var(--text-sm); line-height: var(--leading-body); }\n/* Centered on the first line of the title rather than its top edge. */\n.close { display: grid; flex: 0 0 auto; width: var(--space-8); height: var(--space-8); margin-top: calc((var(--text-lg) * var(--leading-body) - var(--space-8)) / 2); place-items: center; border: 1px solid var(--border); border-radius: var(--radius-control); background: color-mix(in oklab, var(--surface-muted) 65%, transparent); color: var(--text-secondary); cursor: pointer; transition: background-color var(--duration-fast) var(--ease-standard), color var(--duration-fast) var(--ease-standard), transform var(--duration-spring) var(--ease-spring); }\n@media (hover: hover) and (pointer: fine) { .close:hover { background: var(--surface-muted); color: var(--foreground); } }\n.close:active { transform: scale(.96); transition: background-color var(--duration-fast) var(--ease-standard), color var(--duration-fast) var(--ease-standard), transform var(--duration-instant) var(--ease-standard); }\n.body { min-height: 0; overflow: auto; padding: var(--space-6); font-size: var(--text-sm); line-height: var(--leading-body); overscroll-behavior: contain; scrollbar-width: thin; }\n\n@media (max-width: 40rem) {\n  .content { --drawer-size: calc(100vw - var(--space-3)); }\n  .content[data-side=\"right\"] { border-radius: 18px 0 0 18px; }\n  .content[data-side=\"left\"] { border-radius: 0 18px 18px 0; }\n  .content[data-side=\"top\"] { border-radius: 0 0 18px 18px; }\n  .content[data-side=\"bottom\"] { border-radius: 18px 18px 0 0; }\n  .header, .body { padding: var(--space-5); }\n}\n\n@keyframes overlay-in { from { opacity: 0; } }\n@keyframes overlay-out { to { opacity: 0; } }\n@keyframes drawer-in { from { transform: var(--drawer-from); } }\n/* The late fade lets the floating shadow leave with the panel instead of popping away at unmount. */\n@keyframes drawer-out { 65% { opacity: 1; } to { opacity: 0; transform: var(--drawer-from); } }\n@keyframes drawer-fade-in { from { opacity: 0; } }\n@keyframes drawer-fade-out { to { opacity: 0; } }\n\n@media (prefers-reduced-motion: reduce) {\n  .keyframes.overlay[data-state=\"open\"] { animation: overlay-in var(--duration-instant) linear both; }\n  .keyframes.overlay[data-state=\"closed\"] { animation: overlay-out 100ms linear both; }\n  .keyframes.content[data-state=\"open\"] { animation: drawer-fade-in var(--duration-instant) linear both; }\n  .keyframes.content[data-state=\"closed\"] { animation: drawer-fade-out 100ms linear both; }\n  .close, .close:active { transition: none; }\n  .close:active { transform: none; }\n}\n"
    }
  ],
  "docs": "Docs and live preview: https://ui.xzero.co.uk/components/drawer",
  "meta": {
    "tier": "free",
    "kind": "component",
    "tags": [
      "overlay",
      "surface"
    ],
    "docs": "https://ui.xzero.co.uk/components/drawer",
    "markdown": "https://ui.xzero.co.uk/components/drawer/markdown"
  }
}