{"name":"resizable","type":"registry:ui","files":[{"path":"resizable.tsx","type":"registry:ui","content":"\"use client\"\n\nimport * as React from \"react\"\nimport {\n  GripHorizontal as GripHorizontalIcon,\n  GripVertical as GripVerticalIcon,\n} from \"honestui/icons\"\n\nimport { cn } from \"@/lib/utils\"\n\ntype ResizableOrientation = \"horizontal\" | \"vertical\"\n\ntype PanelConstraints = {\n  defaultSize: number | undefined\n  minSize: number\n  maxSize: number\n  collapsible: boolean\n  collapsedSize: number\n}\n\ntype PanelEntry = PanelConstraints & { element: HTMLElement }\n\ntype ResizableContextValue = {\n  orientation: ResizableOrientation\n  order: string[]\n  sizes: Record<string, number>\n  panels: React.RefObject<Map<string, PanelEntry>>\n  register: (id: string, entry: PanelEntry) => () => void\n  update: (id: string, entry: PanelEntry) => void\n  resizePair: (beforeIndex: number, nextBeforeSize: number, base?: Record<string, number>) => void\n  resizePanel: (id: string, size: number) => void\n  setDragging: (dragging: boolean) => void\n}\n\nconst ResizableContext = React.createContext<ResizableContextValue | null>(null)\n\nfunction useResizableGroup() {\n  const context = React.useContext(ResizableContext)\n  if (!context) {\n    throw new Error(\"Resizable parts must be used within <ResizablePanelGroup />\")\n  }\n  return context\n}\n\nconst STORAGE_PREFIX = \"hui-resizable:\"\n\nfunction round(value: number) {\n  return Math.round(value * 1000) / 1000\n}\n\n// A collapsible panel dragged below its minimum snaps to whichever of\n// collapsed or minimum is closer, so it never rests at an in-between size.\nfunction constrain(size: number, constraints: PanelConstraints) {\n  if (constraints.collapsible && size < constraints.minSize) {\n    return size < (constraints.minSize + constraints.collapsedSize) / 2\n      ? constraints.collapsedSize\n      : constraints.minSize\n  }\n  return Math.min(constraints.maxSize, Math.max(constraints.minSize, size))\n}\n\nfunction readStoredLayout(key: string | undefined, count: number) {\n  if (!key) return null\n  try {\n    const parsed: unknown = JSON.parse(window.localStorage.getItem(STORAGE_PREFIX + key) ?? \"null\")\n    if (\n      Array.isArray(parsed) &&\n      parsed.length === count &&\n      parsed.every((value) => typeof value === \"number\" && Number.isFinite(value))\n    ) {\n      return parsed as number[]\n    }\n  } catch {\n    // Storage can be unavailable or hold stale data; fall back to defaults.\n  }\n  return null\n}\n\nfunction writeStoredLayout(key: string | undefined, layout: number[]) {\n  if (!key) return\n  try {\n    window.localStorage.setItem(STORAGE_PREFIX + key, JSON.stringify(layout))\n  } catch {\n    // Persisting is a convenience; a blocked write must not break resizing.\n  }\n}\n\ntype ResizablePanelGroupProps = React.ComponentProps<\"div\"> & {\n  orientation?: ResizableOrientation\n  /** Alias of `orientation`, for code moving from other libraries. */\n  direction?: ResizableOrientation\n  /** Called with every panel size, in percent, after the layout changes. */\n  onLayout?: (sizes: number[]) => void\n  /** Remembers the layout in localStorage under this key. */\n  autoSaveId?: string\n}\n\nfunction ResizablePanelGroup({\n  orientation: orientationProp,\n  direction,\n  onLayout,\n  autoSaveId,\n  className,\n  children,\n  ...props\n}: ResizablePanelGroupProps) {\n  const orientation = orientationProp ?? direction ?? \"horizontal\"\n  const panels = React.useRef(new Map<string, PanelEntry>())\n  const [order, setOrder] = React.useState<string[]>([])\n  const [sizes, setSizes] = React.useState<Record<string, number>>({})\n  const sizesRef = React.useRef(sizes)\n  const draggingRef = React.useRef(false)\n  const onLayoutRef = React.useRef(onLayout)\n\n  React.useEffect(() => {\n    onLayoutRef.current = onLayout\n  })\n\n  const commit = React.useCallback(\n    (next: Record<string, number>, ids: string[]) => {\n      sizesRef.current = next\n      setSizes(next)\n      const layout = ids.map((id) => round(next[id] ?? 0))\n      onLayoutRef.current?.(layout)\n      if (!draggingRef.current) writeStoredLayout(autoSaveId, layout)\n    },\n    [autoSaveId]\n  )\n\n  const register = React.useCallback((id: string, entry: PanelEntry) => {\n    panels.current.set(id, entry)\n    setOrder(sortByDocument(panels.current))\n    return () => {\n      panels.current.delete(id)\n      setOrder(sortByDocument(panels.current))\n    }\n  }, [])\n\n  const update = React.useCallback((id: string, entry: PanelEntry) => {\n    panels.current.set(id, entry)\n  }, [])\n\n  // Assigns sizes whenever panels mount or unmount. Panels with a\n  // defaultSize render at that size on the server, so a complete set of\n  // defaults that adds up to 100 lays out without a shift on hydration.\n  React.useEffect(() => {\n    if (order.length === 0) return\n    const stored = readStoredLayout(autoSaveId, order.length)\n    let layout: number[]\n    if (stored) {\n      layout = stored\n    } else {\n      const defaults = order.map((id) => panels.current.get(id)?.defaultSize)\n      const assigned = defaults.reduce<number>((sum, size) => sum + (size ?? 0), 0)\n      const unassigned = defaults.filter((size) => size === undefined).length\n      const share = unassigned > 0 ? Math.max(0, 100 - assigned) / unassigned : 0\n      layout = defaults.map((size) => size ?? share)\n    }\n    const total = layout.reduce((sum, size) => sum + size, 0) || 1\n    const next: Record<string, number> = {}\n    order.forEach((id, index) => {\n      next[id] = (layout[index] / total) * 100\n    })\n    commit(next, order)\n  }, [order, autoSaveId, commit])\n\n  const resizePair = React.useCallback(\n    (beforeIndex: number, nextBeforeSize: number, base = sizesRef.current) => {\n      const beforeId = order[beforeIndex]\n      const afterId = order[beforeIndex + 1]\n      const before = beforeId ? panels.current.get(beforeId) : undefined\n      const after = afterId ? panels.current.get(afterId) : undefined\n      if (!before || !after) return\n      const total = (base[beforeId] ?? 0) + (base[afterId] ?? 0)\n      let beforeSize = constrain(nextBeforeSize, before)\n      let afterSize = constrain(total - beforeSize, after)\n      beforeSize = total - afterSize\n      // If the neighbor's limits pushed back, recheck the first panel.\n      if (constrain(beforeSize, before) !== beforeSize) {\n        beforeSize = base[beforeId] ?? beforeSize\n        afterSize = total - beforeSize\n      }\n      if (\n        Math.abs(beforeSize - (sizesRef.current[beforeId] ?? 0)) < 0.001 &&\n        Math.abs(afterSize - (sizesRef.current[afterId] ?? 0)) < 0.001\n      ) {\n        return\n      }\n      commit({ ...sizesRef.current, [beforeId]: beforeSize, [afterId]: afterSize }, order)\n    },\n    [order, commit]\n  )\n\n  const resizePanel = React.useCallback(\n    (id: string, size: number) => {\n      const index = order.indexOf(id)\n      if (index < 0) return\n      if (index < order.length - 1) {\n        resizePair(index, size)\n      } else if (index > 0) {\n        const total = (sizesRef.current[order[index - 1]] ?? 0) + (sizesRef.current[id] ?? 0)\n        resizePair(index - 1, total - size)\n      }\n    },\n    [order, resizePair]\n  )\n\n  const setDragging = React.useCallback(\n    (dragging: boolean) => {\n      draggingRef.current = dragging\n      if (!dragging) {\n        writeStoredLayout(\n          autoSaveId,\n          order.map((id) => round(sizesRef.current[id] ?? 0))\n        )\n      }\n    },\n    [autoSaveId, order]\n  )\n\n  return (\n    <ResizableContext.Provider\n      value={{\n        orientation,\n        order,\n        sizes,\n        panels,\n        register,\n        update,\n        resizePair,\n        resizePanel,\n        setDragging,\n      }}\n    >\n      <div\n        data-slot=\"resizable-panel-group\"\n        data-orientation={orientation}\n        className={cn(\n          \"flex size-full overflow-hidden\",\n          orientation === \"vertical\" ? \"flex-col\" : \"flex-row\",\n          className\n        )}\n        {...props}\n      >\n        {children}\n      </div>\n    </ResizableContext.Provider>\n  )\n}\n\nfunction sortByDocument(panels: Map<string, PanelEntry>) {\n  return [...panels.entries()]\n    .sort(([, a], [, b]) =>\n      a.element.compareDocumentPosition(b.element) & Node.DOCUMENT_POSITION_FOLLOWING ? -1 : 1\n    )\n    .map(([id]) => id)\n}\n\ntype ResizablePanelHandle = {\n  getSize: () => number\n  resize: (size: number) => void\n  collapse: () => void\n  expand: () => void\n  isCollapsed: () => boolean\n}\n\ntype ResizablePanelProps = React.ComponentProps<\"div\"> & {\n  /** Starting size in percent of the group. */\n  defaultSize?: number\n  minSize?: number\n  maxSize?: number\n  /** Lets the panel collapse below its minimum to `collapsedSize`. */\n  collapsible?: boolean\n  collapsedSize?: number\n  onResize?: (size: number) => void\n  onCollapse?: () => void\n  onExpand?: () => void\n  /** Receives an imperative handle for collapsing, expanding, and resizing. */\n  panelRef?: React.Ref<ResizablePanelHandle>\n}\n\nfunction ResizablePanel({\n  defaultSize,\n  minSize = 0,\n  maxSize = 100,\n  collapsible = false,\n  collapsedSize = 0,\n  onResize,\n  onCollapse,\n  onExpand,\n  panelRef,\n  id: idProp,\n  className,\n  style,\n  ...props\n}: ResizablePanelProps) {\n  const { register, update, sizes, resizePanel } = useResizableGroup()\n  const generatedId = React.useId()\n  const id = idProp ?? generatedId\n  const [element, setElement] = React.useState<HTMLDivElement | null>(null)\n  const size = sizes[id]\n  const collapsed = collapsible && size !== undefined && size <= collapsedSize + 0.01\n  const expandedSize = React.useRef<number | undefined>(undefined)\n\n  const constraints = React.useMemo<PanelConstraints>(\n    () => ({ defaultSize, minSize, maxSize, collapsible, collapsedSize }),\n    [defaultSize, minSize, maxSize, collapsible, collapsedSize]\n  )\n\n  React.useEffect(() => {\n    if (!element) return\n    return register(id, { ...constraints, element })\n    // Constraint changes are synced below without remounting the panel.\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [element, id, register])\n\n  React.useEffect(() => {\n    if (element) update(id, { ...constraints, element })\n  }, [constraints, element, id, update])\n\n  // Remember the last open size so expand() returns to it.\n  React.useEffect(() => {\n    if (size !== undefined && !collapsed) expandedSize.current = size\n  }, [size, collapsed])\n\n  const previousSize = React.useRef(size)\n  const previousCollapsed = React.useRef(collapsed)\n  const callbacks = React.useRef({ onResize, onCollapse, onExpand })\n  React.useEffect(() => {\n    callbacks.current = { onResize, onCollapse, onExpand }\n  })\n  React.useEffect(() => {\n    if (size === undefined) return\n    if (previousSize.current !== undefined && previousSize.current !== size) {\n      callbacks.current.onResize?.(round(size))\n    }\n    if (previousSize.current !== undefined && previousCollapsed.current !== collapsed) {\n      if (collapsed) callbacks.current.onCollapse?.()\n      else callbacks.current.onExpand?.()\n    }\n    previousSize.current = size\n    previousCollapsed.current = collapsed\n  }, [size, collapsed])\n\n  React.useImperativeHandle(\n    panelRef,\n    () => ({\n      getSize: () => round(size ?? defaultSize ?? 0),\n      resize: (next: number) => resizePanel(id, next),\n      collapse: () => {\n        if (collapsible) resizePanel(id, collapsedSize)\n      },\n      expand: () => {\n        if (collapsed) resizePanel(id, Math.max(minSize, expandedSize.current ?? minSize))\n      },\n      isCollapsed: () => collapsed,\n    }),\n    [size, defaultSize, resizePanel, id, collapsible, collapsedSize, collapsed, minSize]\n  )\n\n  // Before the group measures, the panel uses its defaultSize directly.\n  const grow = size ?? defaultSize ?? 1\n\n  return (\n    <div\n      ref={setElement}\n      id={id}\n      data-slot=\"resizable-panel\"\n      data-collapsed={collapsed ? \"\" : undefined}\n      className={cn(\"min-h-0 min-w-0 overflow-hidden\", className)}\n      style={{ flexGrow: grow, flexShrink: 1, flexBasis: 0, ...style }}\n      {...props}\n    />\n  )\n}\n\ntype ResizableHandleProps = React.ComponentProps<\"div\"> & {\n  /** Shows a grip pill centered on the divider. */\n  withHandle?: boolean\n  /** Percent moved per arrow key press. Shift moves twice as far. */\n  keyboardStep?: number\n  disabled?: boolean\n}\n\nfunction ResizableHandle({\n  withHandle = false,\n  keyboardStep = 5,\n  disabled = false,\n  className,\n  onKeyDown,\n  onPointerDown,\n  \"aria-label\": ariaLabel,\n  ...props\n}: ResizableHandleProps) {\n  const { orientation, order, sizes, panels, resizePair, resizePanel, setDragging } =\n    useResizableGroup()\n  const [element, setElement] = React.useState<HTMLDivElement | null>(null)\n  const [dragging, setDraggingState] = React.useState(false)\n  const horizontal = orientation === \"horizontal\"\n\n  // The handle controls the pair of panels around it, found by DOM order.\n  const beforeIndex = element\n    ? order.filter((id) => {\n        const panel = panels.current.get(id)?.element\n        return panel ? Boolean(panel.compareDocumentPosition(element) & Node.DOCUMENT_POSITION_FOLLOWING) : false\n      }).length - 1\n    : -1\n  const beforeId = beforeIndex >= 0 ? order[beforeIndex] : undefined\n  const afterId = order[beforeIndex + 1]\n  const before = beforeId ? panels.current.get(beforeId) : undefined\n  const after = afterId ? panels.current.get(afterId) : undefined\n  const beforeSize = beforeId ? sizes[beforeId] : undefined\n\n  const isRtl = () =>\n    horizontal && element !== null && getComputedStyle(element).direction === \"rtl\"\n\n  const handleKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {\n    onKeyDown?.(event)\n    if (event.defaultPrevented || disabled || !before || !after || beforeSize === undefined) return\n    const step = keyboardStep * (event.shiftKey ? 2 : 1)\n    const rtl = isRtl()\n    const decreaseKey = horizontal ? (rtl ? \"ArrowRight\" : \"ArrowLeft\") : \"ArrowUp\"\n    const increaseKey = horizontal ? (rtl ? \"ArrowLeft\" : \"ArrowRight\") : \"ArrowDown\"\n    let target: number | undefined\n    if (event.key === decreaseKey) target = beforeSize - step\n    else if (event.key === increaseKey) target = beforeSize + step\n    else if (event.key === \"Home\") target = before.collapsible ? before.collapsedSize : before.minSize\n    else if (event.key === \"End\") target = before.maxSize\n    else if (event.key === \"Enter\") {\n      const panel = before.collapsible ? before : after.collapsible ? after : undefined\n      const panelId = panel === before ? beforeId : afterId\n      if (panel && panelId) {\n        const current = sizes[panelId] ?? 0\n        const isCollapsed = current <= panel.collapsedSize + 0.01\n        resizePanel(panelId, isCollapsed ? Math.max(panel.minSize, panel.defaultSize ?? panel.minSize) : panel.collapsedSize)\n        event.preventDefault()\n      }\n      return\n    }\n    if (target === undefined) return\n    event.preventDefault()\n    // A collapsible panel steps straight between collapsed and minimum.\n    if (before.collapsible && event.key === decreaseKey && beforeSize <= before.minSize + 0.01) {\n      target = before.collapsedSize\n    } else if (before.collapsible && event.key === increaseKey && beforeSize < before.minSize) {\n      target = before.minSize\n    }\n    resizePair(beforeIndex, target)\n  }\n\n  const handlePointerDown = (event: React.PointerEvent<HTMLDivElement>) => {\n    onPointerDown?.(event)\n    if (event.defaultPrevented || disabled || event.button !== 0 || !element) return\n    const group = element.parentElement\n    if (!group || beforeIndex < 0 || !afterId) return\n    event.preventDefault()\n    // preventDefault stops native focus, so focus by hand. focusVisible: false\n    // keeps a mouse press from drawing the keyboard focus ring mid-drag.\n    element.focus({ preventScroll: true, focusVisible: false } as FocusOptions)\n    element.setPointerCapture(event.pointerId)\n\n    const rect = group.getBoundingClientRect()\n    const groupSize = horizontal ? rect.width : rect.height\n    const start = horizontal ? event.clientX : event.clientY\n    const base = { ...sizes }\n    const startSize = base[order[beforeIndex]] ?? 0\n    const direction = isRtl() ? -1 : 1\n\n    // The cursor and selection lock apply to the whole page during a drag,\n    // so it does not flicker or select text as the pointer leaves the handle.\n    const body = document.body\n    const previousCursor = body.style.cursor\n    const previousSelect = body.style.userSelect\n    body.style.cursor = horizontal ? \"col-resize\" : \"row-resize\"\n    body.style.userSelect = \"none\"\n    setDragging(true)\n    setDraggingState(true)\n\n    const move = (moveEvent: PointerEvent) => {\n      const current = horizontal ? moveEvent.clientX : moveEvent.clientY\n      const delta = ((current - start) / Math.max(1, groupSize)) * 100 * direction\n      resizePair(beforeIndex, startSize + delta, base)\n    }\n    const end = () => {\n      element.removeEventListener(\"pointermove\", move)\n      element.removeEventListener(\"pointerup\", end)\n      element.removeEventListener(\"pointercancel\", end)\n      body.style.cursor = previousCursor\n      body.style.userSelect = previousSelect\n      setDragging(false)\n      setDraggingState(false)\n    }\n    element.addEventListener(\"pointermove\", move)\n    element.addEventListener(\"pointerup\", end)\n    element.addEventListener(\"pointercancel\", end)\n  }\n\n  return (\n    <div\n      ref={setElement}\n      role=\"separator\"\n      tabIndex={disabled ? -1 : 0}\n      aria-orientation={horizontal ? \"vertical\" : \"horizontal\"}\n      aria-controls={beforeId}\n      aria-valuenow={beforeSize === undefined ? undefined : Math.round(beforeSize)}\n      aria-valuemin={before ? Math.round(before.collapsible ? before.collapsedSize : before.minSize) : undefined}\n      aria-valuemax={before ? Math.round(before.maxSize) : undefined}\n      aria-label={ariaLabel ?? \"Resize panels\"}\n      aria-disabled={disabled || undefined}\n      data-slot=\"resizable-handle\"\n      data-orientation={orientation}\n      data-dragging={dragging ? \"\" : undefined}\n      data-disabled={disabled ? \"\" : undefined}\n      onKeyDown={handleKeyDown}\n      onPointerDown={handlePointerDown}\n      className={cn(\n        // The visible divider is a hairline; an invisible ::after widens the\n        // pointer target to 12px (24px for touch) without moving the layout.\n        \"relative z-1 flex shrink-0 touch-none items-center justify-center bg-[var(--hui-color-border-base-primary)] outline-none select-none motion-safe:[transition:background-color_var(--hui-duration-fast)_var(--hui-ease-out),box-shadow_var(--hui-duration-fast)_var(--hui-ease-out)] after:absolute after:content-['']\",\n        horizontal\n          ? \"w-px cursor-col-resize after:inset-y-0 after:start-1/2 after:w-3 after:-translate-x-1/2 rtl:after:translate-x-1/2 pointer-coarse:after:w-6\"\n          : \"h-px cursor-row-resize after:inset-x-0 after:top-1/2 after:h-3 after:-translate-y-1/2 pointer-coarse:after:h-6\",\n        // Hover and drag only darken the hairline: the cursor already says\n        // the divider moves, so it needs no accent color. Keyboard focus keeps\n        // the standard focus ring so it is visible without a pointer.\n        \"not-data-disabled:hover:bg-[var(--hui-color-foreground-base-tertiary)] data-dragging:bg-[var(--hui-color-foreground-base-tertiary)] focus-visible:[outline:var(--hui-focus-ring)] focus-visible:outline-offset-[var(--hui-focus-ring-offset-accent)]\",\n        // The grip follows the same quiet hover and drag state.\n        \"not-data-disabled:hover:[&>[data-slot=resizable-handle-grip]]:border-[var(--hui-color-border-field-hover,var(--hui-color-border-base-focus))] data-dragging:[&>[data-slot=resizable-handle-grip]]:border-[var(--hui-color-border-field-hover,var(--hui-color-border-base-focus))]\",\n        \"data-disabled:cursor-default forced-colors:bg-[CanvasText] forced-colors:focus-visible:bg-[Highlight]\",\n        className\n      )}\n      {...props}\n    >\n      {withHandle && (\n        <div\n          aria-hidden=\"true\"\n          data-slot=\"resizable-handle-grip\"\n          className={cn(\n            \"z-1 flex items-center justify-center rounded-[var(--hui-radius-1)] border-[0.5px] border-[var(--hui-color-border-field,var(--hui-color-border-base-tertiary))] bg-[var(--hui-color-background-base-primary)] text-[var(--hui-color-foreground-base-secondary)] shadow-[var(--hui-shadow-feather)] [&_svg]:size-2.5\",\n            horizontal ? \"h-6 w-3\" : \"h-3 w-6\"\n          )}\n        >\n          {horizontal ? <GripVerticalIcon /> : <GripHorizontalIcon />}\n        </div>\n      )}\n    </div>\n  )\n}\n\nexport {\n  ResizablePanelGroup,\n  ResizablePanel,\n  ResizableHandle,\n  type ResizablePanelHandle,\n}\n","target":"components/ui/resizable.tsx"}],"dependencies":["honestui"]}