{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dock",
  "title": "Dock",
  "description": "A panel docked at one edge of a box that pushes the page aside: its room moves on the layout role while the panel slides with its edge, and under 48rem it is a Drawer. Holds whether it is open, docked and as a Drawer, a shortcut that toggles it, and the wiring of the buttons that do.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/controllable.json",
    "https://kinoui.com/r/keys.json",
    "https://kinoui.com/r/refs.json",
    "https://kinoui.com/r/roomy.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/lib/dock.ts",
      "content": "import { useControllable } from '@/registry/lib/controllable'\nimport { useAriaKeyshortcuts, useShortcut } from '@/registry/lib/keys'\nimport { markMounted, mergeRefs } from '@/registry/lib/refs'\nimport { narrowerThan, useNarrowerThan } from '@/registry/lib/roomy'\nimport { type Ref, useId, useMemo, useState } from 'react'\n\n/*\n * A panel docked at one edge of a box, beside the page, which it pushes\n * aside: the Sidebar, and the AI chat side panel. On a wide box a gap in\n * the row holds the panel's room and moves its width on `layout`, while the\n * panel lies over the gap at its full width and slides with the gap's edge,\n * so nothing inside it is laid out again while it moves. Under 48rem the\n * panel is a Drawer from the same side, which the component draws. The box\n * is a container named `dock`, so a dock inside another dock's page reads\n * its own box.\n */\n\ntype DockSide = 'start' | 'end'\n\n/** Under this width, in rem, a dock's box is a phone's screen: the panel is a Drawer. */\nconst SHEET_BELOW_REM = 48\n\ninterface Dock {\n  /** Whether the panel is docked open beside the page, on a wide box. */\n  open: boolean\n  setOpen: (open: boolean) => void\n  /** Whether the box is as narrow as a phone, so the panel is a Drawer. */\n  sheet: boolean\n  sheetOpen: boolean\n  setSheetOpen: (open: boolean) => void\n  /** Opens or closes whichever the box shows: the docked panel or the Drawer. */\n  toggle: () => void\n  /** The panel's id, for its triggers' `aria-controls`. */\n  id: string\n  shortcut: string | null\n}\n\n/** Whether a box `width` px wide, with `rem` px to a rem, is a phone's screen. */\nfunction showsSheet(width: number, rem: number) {\n  return narrowerThan(width, SHEET_BELOW_REM, rem)\n}\n\n/**\n * Holds whether the panel is open, docked and as a Drawer, for the dock in\n * `box`. `shortcut` toggles whichever shows from anywhere in the box, or\n * nothing when null.\n */\nfunction useDock({\n  open: openProp,\n  defaultOpen,\n  onOpenChange,\n  shortcut,\n  box,\n}: {\n  open: boolean | undefined\n  defaultOpen: boolean\n  onOpenChange: ((open: boolean) => void) | undefined\n  shortcut: string | null\n  box: HTMLElement | null\n}): Dock {\n  const [open, setOpen] = useControllable(openProp, defaultOpen, onOpenChange)\n  // Before the first paint: the container query has already hidden the\n  // docked panel on a phone, and this only decides what its triggers open.\n  const sheet = useNarrowerThan(box, SHEET_BELOW_REM)\n  const [sheetOpen, setSheetOpen] = useState(false)\n  if (!sheet && sheetOpen) setSheetOpen(false)\n  const id = useId()\n\n  const toggle = () => {\n    if (sheet) setSheetOpen(!sheetOpen)\n    else setOpen(!open)\n  }\n\n  useShortcut(shortcut, toggle, box)\n\n  return { open, setOpen, sheet, sheetOpen, setSheetOpen, toggle, id, shortcut }\n}\n\n/**\n * What a button that opens and closes the dock spreads on itself: whether\n * the panel it controls is open, docked or as a Drawer, its id, the\n * shortcut as `aria-keyshortcuts`, `data-mounted` once it is on the page\n * (so a look it learns on load lands with `not-data-mounted:transition-none`),\n * and a press that toggles unless the caller's `onClick` prevented it.\n */\nfunction useDockTrigger<Event extends { defaultPrevented: boolean }, Details>(\n  dock: Pick<Dock, 'open' | 'sheet' | 'sheetOpen' | 'toggle' | 'id' | 'shortcut'>,\n  {\n    onClick,\n    ref,\n  }: {\n    onClick: ((event: Event, details: Details) => void) | undefined\n    ref: Ref<HTMLButtonElement> | undefined\n  },\n) {\n  const ariaKeys = useAriaKeyshortcuts(dock.shortcut)\n  const triggerRef = useMemo(\n    () => mergeRefs<HTMLButtonElement>(markMounted, ariaKeys, ref),\n    [ariaKeys, ref],\n  )\n  return {\n    'aria-expanded': dock.sheet ? dock.sheetOpen : dock.open,\n    'aria-controls': dock.id,\n    ref: triggerRef,\n    onClick: (event: Event, details: Details) => {\n      onClick?.(event, details)\n      if (!event.defaultPrevented) dock.toggle()\n    },\n  }\n}\n\n/** The first button that controls the dock with this `id`, where focus goes back when the panel closes with focus inside. */\nfunction dockTrigger(id: string) {\n  return document.querySelector<HTMLElement>(`[aria-controls=\"${CSS.escape(id)}\"]`)\n}\n\n/**\n * The box a dock sits in: a row, clipped, and the container its parts read.\n * It clips without scrolling, so focus moved into a panel still sliding in\n * can't scroll the box to it, which would show the panel at once.\n */\nconst dockBoxClassName =\n  '@container relative isolate flex min-w-0 overflow-clip [container-name:dock]'\n\n/**\n * The page's room for the panel, `--dock-width` wide while `data-state` is\n * `expanded` and none while `collapsed`, moving on `layout`. It takes\n * `data-side` and `aria-hidden`; on a phone's box it is gone.\n */\nconst dockGapClassName =\n  'hidden w-(--dock-width,20rem) shrink-0 transition-[width] duration-(--motion-layout-duration) ease-(--motion-layout-easing) data-[side=end]:order-last data-[state=collapsed]:w-0 motion-reduce:transition-none @3xl/dock:block'\n\n/**\n * The panel, `--dock-width` wide, over its gap at `data-side`. With\n * `data-hidden` it slides out past its edge on `layout`, as the gap closes.\n */\nconst dockPanelClassName =\n  'absolute inset-y-0 z-30 hidden w-(--dock-width,20rem) transition-[translate] duration-(--motion-layout-duration) ease-(--motion-layout-easing) data-[side=end]:end-0 data-[side=start]:start-0 motion-reduce:transition-none @3xl/dock:flex data-hidden:data-[side=end]:translate-x-full data-hidden:data-[side=start]:-translate-x-full rtl:data-hidden:data-[side=end]:-translate-x-full rtl:data-hidden:data-[side=start]:translate-x-full'\n\nexport type { Dock, DockSide }\nexport {\n  dockBoxClassName,\n  dockGapClassName,\n  dockPanelClassName,\n  dockTrigger,\n  showsSheet,\n  useDock,\n  useDockTrigger,\n}\n",
      "type": "registry:lib",
      "target": "@lib/dock.ts"
    }
  ],
  "type": "registry:lib"
}