{"version":3,"file":"PanelGroup-SrrGjruV.cjs","sources":["../src/components/PanelGroup/PanelGroup.tsx"],"sourcesContent":["'use client';\nimport { Children, cloneElement, forwardRef, isValidElement, useRef, useState } from 'react';\nimport type {\n  CSSProperties,\n  HTMLAttributes,\n  KeyboardEvent,\n  PointerEvent,\n  ReactElement,\n} from 'react';\nimport { withSx } from '../../utils/withSx';\nimport type { SxProp } from '../Layout/sx';\n\nexport type PanelDirection = 'horizontal' | 'vertical';\n\nconst STEP = 5; // keyboard resize step, in percent\n\ninterface Constraint {\n  min: number;\n  max: number;\n  def: number | null;\n}\n\nfunction initSizes(constraints: Constraint[]): number[] {\n  const n = constraints.length;\n  if (n === 0) return [];\n  const assigned = constraints.reduce((sum, c) => sum + (c.def ?? 0), 0);\n  const unset = constraints.filter((c) => c.def == null).length;\n  const each = unset ? Math.max(0, 100 - assigned) / unset : 0;\n  const raw = constraints.map((c) => (c.def == null ? each : c.def));\n  const total = raw.reduce((a, b) => a + b, 0) || 1;\n  return raw.map((s) => (s / total) * 100);\n}\n\n/** Move `delta`% across the gap `g` while keeping the pair's combined size constant. */\nfunction applyDelta(sizes: number[], g: number, delta: number, c: Constraint[]): number[] {\n  if (g < 0 || g + 1 >= sizes.length) return sizes;\n  const pair = sizes[g] + sizes[g + 1];\n  let a = Math.max(c[g].min, Math.min(c[g].max, sizes[g] + delta));\n  const b = Math.max(c[g + 1].min, Math.min(c[g + 1].max, pair - a));\n  // re-derive then clamp again so neither panel's own min/max is violated when\n  // the two constraints can't both be satisfied within the pair\n  a = Math.max(c[g].min, Math.min(c[g].max, pair - b));\n  const next = [...sizes];\n  next[g] = a;\n  next[g + 1] = b;\n  return next;\n}\n\nexport interface PanelProps extends HTMLAttributes<HTMLDivElement> {\n  /** Initial size as a percentage of the group. */\n  defaultSize?: number;\n  /** Minimum size in percent. Default 10. */\n  minSize?: number;\n  /** Maximum size in percent. Default 90. */\n  maxSize?: number;\n  /**\n   * Style shorthand, as on Box: scalar values merge into inline style, a\n   * responsive value injects a scoped class.\n   *\n   * PanelGroup clones each panel with a style carrying its flex-basis, and\n   * withSx lets an explicit style win over sx — so sizing stays the group's\n   * to decide and a panel cannot pin its own width through sx.\n   */\n  sx?: SxProp;\n}\n\nexport const Panel = forwardRef<HTMLDivElement, PanelProps>(function Panel(\n  // defaultSize/minSize/maxSize are read by PanelGroup; strip them so they don't hit the DOM\n  { defaultSize: _d, minSize: _min, maxSize: _max, className, style, sx, children, ...rest },\n  ref,\n) {\n  const { className: classes, style: merged, styleTag } = withSx(sx, style, 'cny-panel', className);\n  return (\n    <div ref={ref} className={classes} style={merged} {...rest}>\n      {styleTag}\n      {children}\n    </div>\n  );\n});\n\ninterface HandleInternal {\n  direction: PanelDirection;\n  valueNow: number;\n  valueMin: number;\n  valueMax: number;\n  onResizeStart: (e: PointerEvent<HTMLDivElement>) => void;\n  onKeyResize: (dir: 1 | -1, toEdge?: boolean) => void;\n}\n\nexport interface PanelResizeHandleProps extends HTMLAttributes<HTMLDivElement> {\n  disabled?: boolean;\n  /** One-off CSS, same as `Box`'s `sx` (scalar or responsive `{ base, sm, md, lg, xl }` map). */\n  sx?: SxProp;\n  /** @internal injected by PanelGroup */\n  __internal?: HandleInternal;\n}\n\nexport const PanelResizeHandle = forwardRef<HTMLDivElement, PanelResizeHandleProps>(\n  function PanelResizeHandle({ disabled, className, style, sx, children, __internal, ...rest }, ref) {\n    const horizontal = __internal?.direction !== 'vertical';\n    const { className: classes, style: mergedStyle, styleTag } = withSx(\n      sx, style, 'cny-panel-handle', horizontal ? 'cny-panel-handle-h' : 'cny-panel-handle-v', className,\n    );\n    const handleKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {\n      if (!__internal || disabled) return;\n      const inc = horizontal ? 'ArrowRight' : 'ArrowDown';\n      const dec = horizontal ? 'ArrowLeft' : 'ArrowUp';\n      if (e.key === inc) {\n        e.preventDefault();\n        __internal.onKeyResize(1);\n      } else if (e.key === dec) {\n        e.preventDefault();\n        __internal.onKeyResize(-1);\n      } else if (e.key === 'Home') {\n        e.preventDefault();\n        __internal.onKeyResize(-1, true);\n      } else if (e.key === 'End') {\n        e.preventDefault();\n        __internal.onKeyResize(1, true);\n      }\n    };\n    return (\n      <div\n        ref={ref}\n        role=\"separator\"\n        tabIndex={disabled ? -1 : 0}\n        aria-orientation={horizontal ? 'vertical' : 'horizontal'}\n        aria-valuenow={__internal?.valueNow}\n        aria-valuemin={__internal?.valueMin}\n        aria-valuemax={__internal?.valueMax}\n        aria-disabled={disabled || undefined}\n        className={classes}\n        style={mergedStyle}\n        onPointerDown={(e) => {\n          if (disabled) return;\n          e.preventDefault();\n          __internal?.onResizeStart(e);\n        }}\n        onKeyDown={handleKeyDown}\n        {...rest}\n      >\n        {styleTag}\n        {children}\n      </div>\n    );\n  },\n);\n\nexport interface PanelGroupProps extends HTMLAttributes<HTMLDivElement> {\n  /** Layout axis. Default `'horizontal'`. */\n  direction?: PanelDirection;\n  /**\n   * One-off CSS, same as `Box`'s `sx`: each value may be a scalar or a\n   * responsive breakpoint map (`{ base, sm, md, lg, xl }`). Scalar-only sx\n   * merges into inline style; responsive sx injects a scoped class.\n   */\n  sx?: SxProp;\n}\n\nexport const PanelGroup = forwardRef<HTMLDivElement, PanelGroupProps>(function PanelGroup(\n  { direction = 'horizontal', className, style, sx, children, ...rest },\n  ref,\n) {\n  const containerRef = useRef<HTMLDivElement | null>(null);\n  const items = Children.toArray(children).filter(isValidElement) as ReactElement[];\n\n  const constraints: Constraint[] = items\n    .filter((c) => c.type === Panel)\n    .map((c) => {\n      const p = c.props as PanelProps;\n      return { min: p.minSize ?? 10, max: p.maxSize ?? 90, def: p.defaultSize ?? null };\n    });\n\n  const [sizes, setSizes] = useState<number[]>(() => initSizes(constraints));\n\n  // `sizes` is seeded once; if the panel set changes at runtime, fall back to a\n  // fresh distribution rather than rendering shifted panels at zero width.\n  const effectiveSizes = sizes.length === constraints.length ? sizes : initSizes(constraints);\n\n  const dragRef = useRef<{ g: number; start: number; startSizes: number[] } | null>(null);\n\n  const startDrag = (g: number, e: PointerEvent<HTMLDivElement>) => {\n    const start = direction === 'horizontal' ? e.clientX : e.clientY;\n    dragRef.current = { g, start, startSizes: effectiveSizes };\n    const move = (ev: globalThis.PointerEvent) => {\n      const drag = dragRef.current;\n      const rect = containerRef.current?.getBoundingClientRect();\n      if (!drag || !rect) return;\n      const total = direction === 'horizontal' ? rect.width : rect.height;\n      if (!total) return;\n      const cur = direction === 'horizontal' ? ev.clientX : ev.clientY;\n      const delta = ((cur - drag.start) / total) * 100;\n      setSizes(applyDelta(drag.startSizes, drag.g, delta, constraints));\n    };\n    const up = () => {\n      dragRef.current = null;\n      window.removeEventListener('pointermove', move);\n      window.removeEventListener('pointerup', up);\n    };\n    window.addEventListener('pointermove', move);\n    window.addEventListener('pointerup', up);\n  };\n\n  // toEdge (Home/End): push the handle as far as the panels' min/max allow.\n  const keyResize = (g: number, dir: 1 | -1, toEdge = false) => {\n    setSizes(applyDelta(effectiveSizes, g, dir * (toEdge ? 100 : STEP), constraints));\n  };\n\n  const setRefs = (node: HTMLDivElement | null) => {\n    containerRef.current = node;\n    if (typeof ref === 'function') ref(node);\n    else if (ref) ref.current = node;\n  };\n\n  let panelOrder = -1;\n  let handleOrder = -1;\n  const rendered = items.map((child) => {\n    if (child.type === Panel) {\n      panelOrder += 1;\n      const k = panelOrder;\n      const style: CSSProperties = {\n        ...(child.props as PanelProps).style,\n        flexBasis: `${effectiveSizes[k] ?? 0}%`,\n        flexGrow: 0,\n        flexShrink: 0,\n      };\n      return cloneElement(child as ReactElement<PanelProps>, {\n        key: child.key ?? `panel-${k}`,\n        style,\n      });\n    }\n    if (child.type === PanelResizeHandle) {\n      handleOrder += 1;\n      const g = handleOrder;\n      const internal: HandleInternal = {\n        direction,\n        valueNow: Math.round(effectiveSizes[g] ?? 0),\n        valueMin: constraints[g]?.min ?? 0,\n        valueMax: constraints[g]?.max ?? 100,\n        onResizeStart: (e) => startDrag(g, e),\n        onKeyResize: (d, edge) => keyResize(g, d, edge),\n      };\n      return cloneElement(child as ReactElement<PanelResizeHandleProps>, {\n        key: child.key ?? `handle-${g}`,\n        __internal: internal,\n      });\n    }\n    return child;\n  });\n\n  const { className: classes, style: mergedStyle, styleTag } = withSx(\n    sx, style, 'cny-panel-group', direction === 'vertical' && 'is-vertical', className,\n  );\n\n  return (\n    <div ref={setRefs} className={classes} style={mergedStyle} {...rest}>\n      {styleTag}\n      {rendered}\n    </div>\n  );\n});\n\n/** No-op kept for drop-in compatibility with `react-resizable-panels`. */\nexport function disableGlobalCursorStyles(): void {\n  /* Canny PanelGroup never writes global cursor styles, so there is nothing to disable. */\n}\n"],"names":["STEP","initSizes","constraints","assigned","sum","c","unset","each","raw","total","a","b","s","applyDelta","sizes","g","delta","pair","next","Panel","forwardRef","_d","_min","_max","className","style","sx","children","rest","ref","classes","merged","styleTag","withSx","jsxs","PanelResizeHandle","disabled","__internal","horizontal","mergedStyle","handleKeyDown","e","inc","dec","PanelGroup","direction","containerRef","useRef","items","Children","isValidElement","p","setSizes","useState","effectiveSizes","dragRef","startDrag","start","move","ev","drag","rect","_a","up","keyResize","dir","toEdge","setRefs","node","panelOrder","handleOrder","rendered","child","k","cloneElement","internal","_b","d","edge","disableGlobalCursorStyles"],"mappings":"qHAcMA,EAAO,EAQb,SAASC,EAAUC,EAAqC,CAEtD,GADUA,EAAY,SACZ,EAAG,MAAO,CAAA,EACpB,MAAMC,EAAWD,EAAY,OAAO,CAACE,EAAKC,IAAMD,GAAOC,EAAE,KAAO,GAAI,CAAC,EAC/DC,EAAQJ,EAAY,OAAQG,GAAMA,EAAE,KAAO,IAAI,EAAE,OACjDE,EAAOD,EAAQ,KAAK,IAAI,EAAG,IAAMH,CAAQ,EAAIG,EAAQ,EACrDE,EAAMN,EAAY,IAAKG,GAAOA,EAAE,KAAO,KAAOE,EAAOF,EAAE,GAAI,EAC3DI,EAAQD,EAAI,OAAO,CAACE,EAAGC,IAAMD,EAAIC,EAAG,CAAC,GAAK,EAChD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