{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "drawer",
  "title": "Drawer",
  "description": "A bottom sheet dialog for phones that can also be dragged down to close.",
  "dependencies": [
    "@radix-ui/react-dialog"
  ],
  "registryDependencies": [
    "@corsair-ui/utils"
  ],
  "files": [
    {
      "path": "registry/default/ui/drawer.tsx",
      "content": "\"use client\";\n\nimport * as DrawerPrimitive from \"@radix-ui/react-dialog\";\nimport {\n  createContext,\n  useCallback,\n  useContext,\n  useMemo,\n  useRef,\n  useState,\n  type ComponentProps,\n  type PointerEvent,\n  type Ref,\n} from \"react\";\n\nimport { cn } from \"@/registry/default/lib/utils\";\n\nfunction mergeRefs<T>(...refs: (Ref<T> | undefined)[]) {\n  return (node: T | null) => {\n    for (const ref of refs) {\n      if (typeof ref === \"function\") ref(node);\n      else if (ref) ref.current = node;\n    }\n  };\n}\n\ninterface DrawerContextValue {\n  open: boolean;\n  setOpen: (open: boolean) => void;\n}\n\nconst DrawerContext = createContext<DrawerContextValue | null>(null);\n\ninterface DrawerDragHandlers {\n  onPointerDown: (event: PointerEvent<HTMLElement>) => void;\n  onPointerMove: (event: PointerEvent<HTMLElement>) => void;\n  onPointerUp: (event: PointerEvent<HTMLElement>) => void;\n  onPointerCancel: (event: PointerEvent<HTMLElement>) => void;\n}\n\n/** Drag handlers from DrawerContent, picked up by DrawerHeader. */\nconst DrawerDragContext = createContext<DrawerDragHandlers | null>(null);\n\n/** Elements inside the drag area that keep their own pointer behaviour. */\nconst INTERACTIVE = \"a, button, input, select, textarea, label, [role=button], [contenteditable]\";\n\n/**\n * A panel that slides up from the bottom of the screen, for actions and\n * short forms on phones. It is a Radix dialog: focus moves into it and stays\n * trapped, Escape and a click on the overlay close it, and focus returns to\n * the trigger; screen readers announce it as a dialog named by\n * `DrawerTitle`. Pointer users can also drag it down by the handle or the\n * header to close it; that is an extra, never the only way out. With reduced\n * motion it appears without sliding and does not spring back. Controlled\n * with `open` / `onOpenChange` or uncontrolled with `defaultOpen`.\n *\n * @example\n * <Drawer>\n *   <DrawerTrigger asChild>\n *     <Button variant=\"outline\">Share</Button>\n *   </DrawerTrigger>\n *   <DrawerContent>\n *     <DrawerHeader>\n *       <DrawerTitle>Share this page</DrawerTitle>\n *       <DrawerDescription>Anyone with the link can view it.</DrawerDescription>\n *     </DrawerHeader>\n *     <DrawerFooter>\n *       <DrawerClose asChild>\n *         <Button variant=\"outline\">Done</Button>\n *       </DrawerClose>\n *     </DrawerFooter>\n *   </DrawerContent>\n * </Drawer>\n */\nfunction Drawer({\n  open: openProp,\n  defaultOpen = false,\n  onOpenChange,\n  ...props\n}: ComponentProps<typeof DrawerPrimitive.Root>) {\n  const [openState, setOpenState] = useState(defaultOpen);\n  const open = openProp ?? openState;\n  const setOpen = useCallback(\n    (next: boolean) => {\n      if (openProp === undefined) setOpenState(next);\n      onOpenChange?.(next);\n    },\n    [openProp, onOpenChange]\n  );\n  const value = useMemo(() => ({ open, setOpen }), [open, setOpen]);\n\n  return (\n    <DrawerContext.Provider value={value}>\n      <DrawerPrimitive.Root data-slot=\"drawer\" open={open} onOpenChange={setOpen} {...props} />\n    </DrawerContext.Provider>\n  );\n}\n\nfunction DrawerTrigger(props: ComponentProps<typeof DrawerPrimitive.Trigger>) {\n  return <DrawerPrimitive.Trigger data-slot=\"drawer-trigger\" {...props} />;\n}\n\nfunction DrawerPortal(props: ComponentProps<typeof DrawerPrimitive.Portal>) {\n  return <DrawerPrimitive.Portal data-slot=\"drawer-portal\" {...props} />;\n}\n\n/** Closes the drawer. Use `asChild` to turn your own button into a close button. */\nfunction DrawerClose(props: ComponentProps<typeof DrawerPrimitive.Close>) {\n  return <DrawerPrimitive.Close data-slot=\"drawer-close\" {...props} />;\n}\n\nfunction DrawerOverlay({ className, ...props }: ComponentProps<typeof DrawerPrimitive.Overlay>) {\n  return (\n    <DrawerPrimitive.Overlay\n      data-slot=\"drawer-overlay\"\n      className={cn(\n        \"fixed inset-0 z-50 bg-black/50\",\n        // animate-in/out sit behind motion-safe: so reduced motion really turns them off.\n        \"motion-safe:data-[state=open]:animate-in data-[state=open]:fade-in-0\",\n        \"motion-safe:data-[state=closed]:animate-out data-[state=closed]:fade-out-0\",\n        className\n      )}\n      {...props}\n    />\n  );\n}\n\ninterface DrawerContentProps extends ComponentProps<typeof DrawerPrimitive.Content> {\n  /**\n   * Classes for the overlay behind the drawer: opacity, blur, z-index. The\n   * overlay is also `data-slot=\"drawer-overlay\"` for styling from outside.\n   */\n  overlayClassName?: string;\n  /** Shows the grab handle at the top. It is decorative: hidden from screen readers. */\n  showHandle?: boolean;\n  /** Share of the drawer's height (0 to 1) it has to be dragged down to close on release. */\n  closeThreshold?: number;\n}\n\n/**\n * The panel itself, rendered in a portal above an overlay, fixed to the\n * bottom of the screen and at most 85% of its height. It needs a\n * `DrawerTitle`; hide it with `sr-only` if the design has no visible\n * heading. While dragged it has `data-dragging=\"true\"` and the offset in\n * the `--drawer-drag` CSS variable. For long content, put it in a\n * `div className=\"overflow-y-auto\"` below the header.\n */\nfunction DrawerContent({\n  className,\n  children,\n  overlayClassName,\n  showHandle = true,\n  closeThreshold = 0.25,\n  ref,\n  ...props\n}: DrawerContentProps) {\n  const drawer = useContext(DrawerContext);\n  const content = useRef<HTMLDivElement>(null);\n  const mergedRef = useMemo(() => mergeRefs(ref, content), [ref]);\n  const drag = useRef<{ pointerId: number; startY: number; height: number; offset: number } | null>(\n    null\n  );\n  const frame = useRef(0);\n\n  const write = useCallback((offset: number) => {\n    const element = content.current;\n    if (element) element.style.setProperty(\"--drawer-drag\", `${offset}px`);\n  }, []);\n\n  const end = useCallback(\n    (event: PointerEvent<HTMLElement>, cancelled: boolean) => {\n      const current = drag.current;\n      if (!current || current.pointerId !== event.pointerId) return;\n      drag.current = null;\n      cancelAnimationFrame(frame.current);\n      if (event.currentTarget.hasPointerCapture(event.pointerId)) {\n        event.currentTarget.releasePointerCapture(event.pointerId);\n      }\n      content.current?.removeAttribute(\"data-dragging\");\n      const threshold = Math.min(Math.max(closeThreshold, 0), 1) * current.height;\n      if (!cancelled && current.offset > 0 && current.offset >= threshold && drawer) {\n        // Leave the offset in place so the exit animation starts where the finger let go.\n        write(current.offset);\n        drawer.setOpen(false);\n      } else {\n        // Spring back: the transition runs now that data-dragging is gone.\n        write(0);\n      }\n    },\n    [closeThreshold, drawer, write]\n  );\n\n  const handlers = useMemo<DrawerDragHandlers>(\n    () => ({\n      onPointerDown(event) {\n        if (event.button !== 0 || !content.current) return;\n        const target = event.target as Element;\n        if (target !== event.currentTarget && target.closest(INTERACTIVE)) return;\n        drag.current = {\n          pointerId: event.pointerId,\n          startY: event.clientY,\n          height: content.current.getBoundingClientRect().height,\n          offset: 0,\n        };\n        event.currentTarget.setPointerCapture(event.pointerId);\n        content.current.setAttribute(\"data-dragging\", \"true\");\n      },\n      onPointerMove(event) {\n        const current = drag.current;\n        if (!current || current.pointerId !== event.pointerId) return;\n        current.offset = Math.max(0, event.clientY - current.startY);\n        cancelAnimationFrame(frame.current);\n        frame.current = requestAnimationFrame(() => write(current.offset));\n      },\n      onPointerUp(event) {\n        end(event, false);\n      },\n      onPointerCancel(event) {\n        end(event, true);\n      },\n    }),\n    [end, write]\n  );\n\n  return (\n    <DrawerPortal>\n      <DrawerOverlay className={overlayClassName} />\n      <DrawerPrimitive.Content\n        ref={mergedRef}\n        data-slot=\"drawer-content\"\n        className={cn(\n          \"bg-background fixed inset-x-0 bottom-0 z-50 flex max-h-[85svh] flex-col rounded-t-xl border-t shadow-lg outline-none\",\n          \"[transform:translate3d(0,var(--drawer-drag,0px),0)]\",\n          \"transition-transform ease-out data-[dragging=true]:transition-none motion-reduce:transition-none\",\n          \"motion-safe:data-[state=open]:animate-in data-[state=open]:slide-in-from-bottom\",\n          \"motion-safe:data-[state=closed]:animate-out data-[state=closed]:slide-out-to-bottom\",\n          className\n        )}\n        {...props}\n      >\n        {showHandle ? (\n          <div\n            aria-hidden=\"true\"\n            data-slot=\"drawer-handle\"\n            className=\"flex shrink-0 cursor-grab touch-none justify-center pt-4 pb-2 select-none active:cursor-grabbing\"\n            {...handlers}\n          >\n            <div className=\"bg-muted h-1.5 w-12 rounded-full\" />\n          </div>\n        ) : null}\n        <DrawerDragContext.Provider value={handlers}>{children}</DrawerDragContext.Provider>\n      </DrawerPrimitive.Content>\n    </DrawerPortal>\n  );\n}\n\n/**\n * The top of the drawer, with the title and description. Dragging it down\n * with a pointer moves the drawer, like the handle; buttons and links inside\n * keep working.\n */\nfunction DrawerHeader({\n  className,\n  onPointerDown,\n  onPointerMove,\n  onPointerUp,\n  onPointerCancel,\n  ...props\n}: ComponentProps<\"div\">) {\n  const handlers = useContext(DrawerDragContext);\n  return (\n    <div\n      data-slot=\"drawer-header\"\n      className={cn(\n        \"flex flex-col gap-1.5 p-4 text-center sm:text-left\",\n        handlers && \"touch-none\",\n        className\n      )}\n      onPointerDown={(event) => {\n        onPointerDown?.(event);\n        if (!event.defaultPrevented) handlers?.onPointerDown(event);\n      }}\n      onPointerMove={(event) => {\n        onPointerMove?.(event);\n        handlers?.onPointerMove(event);\n      }}\n      onPointerUp={(event) => {\n        onPointerUp?.(event);\n        handlers?.onPointerUp(event);\n      }}\n      onPointerCancel={(event) => {\n        onPointerCancel?.(event);\n        handlers?.onPointerCancel(event);\n      }}\n      {...props}\n    />\n  );\n}\n\nfunction DrawerFooter({ className, ...props }: ComponentProps<\"div\">) {\n  return (\n    <div\n      data-slot=\"drawer-footer\"\n      className={cn(\"mt-auto flex flex-col gap-2 p-4\", className)}\n      {...props}\n    />\n  );\n}\n\n/** Names the drawer for screen readers. */\nfunction DrawerTitle({ className, ...props }: ComponentProps<typeof DrawerPrimitive.Title>) {\n  return (\n    <DrawerPrimitive.Title\n      data-slot=\"drawer-title\"\n      className={cn(\"text-foreground font-semibold\", className)}\n      {...props}\n    />\n  );\n}\n\n/** Read out after the title when the drawer opens. */\nfunction DrawerDescription({\n  className,\n  ...props\n}: ComponentProps<typeof DrawerPrimitive.Description>) {\n  return (\n    <DrawerPrimitive.Description\n      data-slot=\"drawer-description\"\n      className={cn(\"text-muted-foreground text-sm\", className)}\n      {...props}\n    />\n  );\n}\n\nexport {\n  Drawer,\n  DrawerClose,\n  DrawerContent,\n  DrawerDescription,\n  DrawerFooter,\n  DrawerHeader,\n  DrawerOverlay,\n  DrawerPortal,\n  DrawerTitle,\n  DrawerTrigger,\n  type DrawerContentProps,\n};\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}