{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ds-dashrow",
  "title": "Dashrow",
  "description": "Responsive layout container for dashboard panels with resizable split ratios and N-child support.",
  "dependencies": [
    "class-variance-authority"
  ],
  "registryDependencies": [
    "@lema-ds/ui-i18n",
    "@lema-ds/ds-locale-provider"
  ],
  "files": [
    {
      "path": "components/ds/ds-dashrow.tsx",
      "content": "\"use client\"\n\nimport { useRef, useEffect, Children, useMemo, Fragment } from \"react\"\nimport { cva, type VariantProps } from \"class-variance-authority\"\nimport type { ReactNode } from \"react\"\nimport { cn } from \"@/lib/utils\"\nimport { UI_I18N, type UILocale } from \"@/lib/ui-i18n\"\nimport { useUILocale } from \"@/components/ui/ds-locale-provider\"\n\n// ── Types ──────────────────────────────────────────────────────────────────\n\nexport type DashrowAlignment = \"left\" | \"right\" | \"equal\"\nexport type DashrowGap = \"none\" | \"sm\" | \"md\" | \"lg\"\nexport type DashrowPadding = \"none\" | \"sm\" | \"md\" | \"lg\"\n\nexport interface DashrowProps extends DashrowVariants {\n  /**\n   * Width ratio between children on desktop (lg+).\n   * - `left`  — first child gets 60%, rest share 40%\n   * - `right` — first child gets 40%, last gets 60%\n   * - `equal` — all children share equally\n   * @default \"left\"\n   */\n  alignment?: DashrowAlignment\n  /** @default \"md\" */\n  gap?: DashrowGap\n  /** @default \"none\" */\n  padding?: DashrowPadding\n  /**\n   * A unique key used to persist panel sizes in sessionStorage.\n   * If omitted, sizes are not persisted across renders.\n   */\n  storageKey?: string\n  /** @default true */\n  resizable?: boolean\n  locale?: UILocale\n  children: ReactNode\n  className?: string\n}\n\n// ── Variants ───────────────────────────────────────────────────────────────\n\nexport const dashrowContainerVariants = cva(\n  [\"flex w-full flex-col lg:flex-row\", \"transition-all duration-300\"],\n  {\n    variants: {\n      alignment: {\n        left: \"\",\n        right: \"\",\n        equal: \"\",\n      },\n      gap: {\n        none: \"gap-0\",\n        sm: \"gap-2\",\n        md: \"gap-4\",\n        lg: \"gap-6\",\n      },\n      padding: {\n        none: \"p-0\",\n        sm: \"p-2\",\n        md: \"p-4\",\n        lg: \"p-6\",\n      },\n    },\n    defaultVariants: {\n      alignment: \"left\",\n      gap: \"md\",\n      padding: \"none\",\n    },\n  }\n)\n\nexport const dashrowChildVariants = cva(\n  \"flex w-full min-w-0 flex-col lg:w-auto [&>*]:min-h-0 [&>*]:flex-1\",\n  {\n    variants: {\n      alignment: {\n        left: \"\",\n        right: \"\",\n        equal: \"\",\n      },\n      position: {\n        first: \"\",\n        last: \"\",\n        middle: \"\",\n      },\n    },\n    compoundVariants: [\n      { alignment: \"left\", position: \"first\", class: \"lg:flex-[3]\" },\n      { alignment: \"left\", position: \"last\", class: \"lg:flex-[2]\" },\n      { alignment: \"left\", position: \"middle\", class: \"lg:flex-[2]\" },\n      { alignment: \"right\", position: \"first\", class: \"lg:flex-[2]\" },\n      { alignment: \"right\", position: \"last\", class: \"lg:flex-[3]\" },\n      { alignment: \"right\", position: \"middle\", class: \"lg:flex-[2]\" },\n      { alignment: \"equal\", position: \"first\", class: \"lg:flex-1\" },\n      { alignment: \"equal\", position: \"last\", class: \"lg:flex-1\" },\n      { alignment: \"equal\", position: \"middle\", class: \"lg:flex-1\" },\n    ],\n    defaultVariants: {\n      alignment: \"left\",\n      position: \"first\",\n    },\n  }\n)\n\nexport const dashrowDividerVariants = cva([\n  \"hidden shrink-0 items-center justify-center lg:flex\",\n  \"w-3 cursor-col-resize rounded-full select-none\",\n  \"transition-colors duration-200\",\n  \"hover:bg-primary/10\",\n  \"data-[dragging=true]:bg-primary/15\",\n  \"focus-visible:ring-2 focus-visible:outline-none\",\n  \"focus-visible:ring-ring focus-visible:ring-offset-1\",\n])\n\nexport const dashrowDividerHandleVariants = cva([\n  \"h-8 w-1 rounded-full\",\n  \"bg-border\",\n  \"transition-colors duration-200\",\n  \"group-hover/divider:bg-primary/40\",\n])\n\nexport type DashrowVariants = VariantProps<typeof dashrowContainerVariants>\nexport type DashrowChildVariants = VariantProps<typeof dashrowChildVariants>\n\n// ── Helpers ────────────────────────────────────────────────────────────────\n\n/** Computes the initial aria-valuenow (0–100) for a divider based on alignment. */\nfunction getInitialValueNow(\n  alignment: DashrowAlignment,\n  dividerIndex: number,\n  count: number\n): number {\n  const weights = Array.from({ length: count }, (_, i) => {\n    if (alignment === \"equal\") return 1\n    if (alignment === \"left\") return i === 0 ? 3 : 2\n    return i === count - 1 ? 3 : 2 // right\n  })\n  const total = weights.reduce((a, b) => a + b, 0)\n  const sumBefore = weights\n    .slice(0, dividerIndex + 1)\n    .reduce((a, b) => a + b, 0)\n  return Math.round((sumBefore / total) * 100)\n}\n\n// ── Dashrow ────────────────────────────────────────────────────────────────\n\nexport function Dashrow({\n  alignment = \"left\",\n  gap = \"md\",\n  padding = \"none\",\n  resizable = true,\n  storageKey,\n  children,\n  locale: localeProp,\n  className,\n}: DashrowProps) {\n  const locale = useUILocale(localeProp)\n  const containerRef = useRef<HTMLDivElement>(null)\n  const childRefs = useRef<(HTMLDivElement | null)[]>([])\n  const isDraggingRef = useRef(false)\n  const manualWidthsRef = useRef<(string | null)[]>([])\n  const activeDividerRef = useRef<{\n    index: number\n    startX: number\n    leftPct: number\n    rightPct: number\n  } | null>(null)\n\n  const childrenArray = Children.toArray(children)\n  const count = childrenArray.length\n\n  useEffect(() => {\n    if (!storageKey) return\n    try {\n      const saved = sessionStorage.getItem(`dashrow:${storageKey}`)\n      if (!saved) return\n      const widths: string[] = JSON.parse(saved)\n      widths.forEach((w, i) => {\n        const el = childRefs.current[i]\n        if (el) {\n          el.style.flex = \"none\"\n          el.style.width = w\n          manualWidthsRef.current[i] = w\n        }\n      })\n    } catch {\n      // ignore corrupt storage\n    }\n  }, [storageKey])\n\n  useEffect(() => {\n    if (!resizable || count < 2) return\n\n    const container = containerRef.current\n    if (!container) return\n\n    const dividers = container.querySelectorAll<HTMLElement>(\n      \"[data-dashrow-divider]\"\n    )\n\n    function getClientX(e: MouseEvent | TouchEvent) {\n      return \"touches\" in e ? e.touches[0].clientX : e.clientX\n    }\n\n    function onStart(dividerIndex: number) {\n      return (e: MouseEvent | TouchEvent) => {\n        if (\"button\" in e && e.button !== 0) return\n        e.preventDefault()\n        isDraggingRef.current = true\n\n        const left = childRefs.current[dividerIndex]\n        const right = childRefs.current[dividerIndex + 1]\n        const rect = container!.getBoundingClientRect()\n\n        activeDividerRef.current = {\n          index: dividerIndex,\n          startX: getClientX(e),\n          leftPct: left\n            ? (left.getBoundingClientRect().width / rect.width) * 100\n            : 50,\n          rightPct: right\n            ? (right.getBoundingClientRect().width / rect.width) * 100\n            : 50,\n        }\n\n        dividers[dividerIndex]?.setAttribute(\"data-dragging\", \"true\")\n        document.body.style.cursor = \"col-resize\"\n        document.body.style.userSelect = \"none\"\n      }\n    }\n\n    function onMove(e: MouseEvent | TouchEvent) {\n      if (!isDraggingRef.current || !activeDividerRef.current) return\n      const { index, startX, leftPct, rightPct } = activeDividerRef.current\n\n      const rect = container!.getBoundingClientRect()\n      const delta = ((getClientX(e) - startX) / rect.width) * 100\n      const newLeft = Math.min(\n        Math.max(leftPct + delta, 15),\n        leftPct + rightPct - 15\n      )\n      const newRight = leftPct + rightPct - newLeft\n\n      const leftEl = childRefs.current[index]\n      const rightEl = childRefs.current[index + 1]\n      if (leftEl) {\n        leftEl.style.flex = \"none\"\n        leftEl.style.width = `${newLeft}%`\n      }\n      if (rightEl) {\n        rightEl.style.flex = \"none\"\n        rightEl.style.width = `${newRight}%`\n      }\n      dividers[index]?.setAttribute(\n        \"aria-valuenow\",\n        String(Math.round(newLeft))\n      )\n    }\n\n    function onKeyDown(dividerIndex: number) {\n      return (e: KeyboardEvent) => {\n        const step = e.shiftKey ? 15 : 5\n        let delta = 0\n        if (e.key === \"ArrowLeft\") delta = -step\n        else if (e.key === \"ArrowRight\") delta = step\n        else return\n\n        e.preventDefault()\n\n        const leftEl = childRefs.current[dividerIndex]\n        const rightEl = childRefs.current[dividerIndex + 1]\n        if (!leftEl || !rightEl) return\n\n        const rect = container!.getBoundingClientRect()\n        const leftPct =\n          (leftEl.getBoundingClientRect().width / rect.width) * 100\n        const rightPct =\n          (rightEl.getBoundingClientRect().width / rect.width) * 100\n        const newLeft = Math.min(\n          Math.max(leftPct + delta, 15),\n          leftPct + rightPct - 15\n        )\n        const newRight = leftPct + rightPct - newLeft\n\n        leftEl.style.flex = \"none\"\n        leftEl.style.width = `${newLeft}%`\n        rightEl.style.flex = \"none\"\n        rightEl.style.width = `${newRight}%`\n        manualWidthsRef.current[dividerIndex] = `${newLeft}%`\n        manualWidthsRef.current[dividerIndex + 1] = `${newRight}%`\n        dividers[dividerIndex]?.setAttribute(\n          \"aria-valuenow\",\n          String(Math.round(newLeft))\n        )\n\n        if (storageKey) {\n          try {\n            const widths = childRefs.current.map((el) => el?.style.width ?? \"\")\n            sessionStorage.setItem(\n              `dashrow:${storageKey}`,\n              JSON.stringify(widths)\n            )\n          } catch {\n            // ignore quota errors\n          }\n        }\n      }\n    }\n\n    function onEnd() {\n      if (!isDraggingRef.current) return\n      isDraggingRef.current = false\n\n      if (activeDividerRef.current !== null) {\n        dividers[activeDividerRef.current.index]?.setAttribute(\n          \"data-dragging\",\n          \"false\"\n        )\n      }\n      activeDividerRef.current = null\n      document.body.style.cursor = \"\"\n      document.body.style.userSelect = \"\"\n\n      childRefs.current.forEach((el, i) => {\n        if (el?.style.width) manualWidthsRef.current[i] = el.style.width\n      })\n\n      if (storageKey) {\n        try {\n          const widths = childRefs.current.map((el) => el?.style.width ?? \"\")\n          sessionStorage.setItem(\n            `dashrow:${storageKey}`,\n            JSON.stringify(widths)\n          )\n        } catch {\n          // ignore quota errors\n        }\n      }\n    }\n\n    const listeners: Array<[HTMLElement, string, EventListener]> = []\n\n    dividers.forEach((div, i) => {\n      const mousedown = onStart(i) as EventListener\n      const touchstart = onStart(i) as EventListener\n      const keydown = onKeyDown(i) as EventListener\n      div.addEventListener(\"mousedown\", mousedown)\n      div.addEventListener(\"touchstart\", touchstart, { passive: false })\n      div.addEventListener(\"keydown\", keydown)\n      listeners.push(\n        [div, \"mousedown\", mousedown],\n        [div, \"touchstart\", touchstart],\n        [div, \"keydown\", keydown]\n      )\n    })\n\n    const mousemove = onMove as EventListener\n    const touchmove = onMove as EventListener\n    const mouseup = onEnd as EventListener\n    const touchend = onEnd as EventListener\n\n    window.addEventListener(\"mousemove\", mousemove)\n    window.addEventListener(\"touchmove\", touchmove, { passive: false })\n    window.addEventListener(\"mouseup\", mouseup)\n    window.addEventListener(\"touchend\", touchend)\n\n    return () => {\n      listeners.forEach(([el, type, fn]) => el.removeEventListener(type, fn))\n      window.removeEventListener(\"mousemove\", mousemove)\n      window.removeEventListener(\"touchmove\", touchmove)\n      window.removeEventListener(\"mouseup\", mouseup)\n      window.removeEventListener(\"touchend\", touchend)\n      document.body.style.cursor = \"\"\n      document.body.style.userSelect = \"\"\n    }\n  }, [resizable, count, storageKey])\n\n  useEffect(() => {\n    const mq = window.matchMedia(\"(min-width: 1024px)\")\n\n    function applyBreakpoint(isDesktop: boolean) {\n      if (!isDesktop) {\n        childRefs.current.forEach((el) => {\n          if (el) {\n            el.style.flex = \"\"\n            el.style.width = \"\"\n          }\n        })\n      } else {\n        childRefs.current.forEach((el, i) => {\n          const w = manualWidthsRef.current[i]\n          if (el && w) {\n            el.style.flex = \"none\"\n            el.style.width = w\n          }\n        })\n      }\n    }\n\n    const handler = (e: MediaQueryListEvent) => applyBreakpoint(e.matches)\n    mq.addEventListener(\"change\", handler)\n    return () => mq.removeEventListener(\"change\", handler)\n  }, [])\n\n  const containerClass = useMemo(\n    () => cn(dashrowContainerVariants({ alignment, gap, padding }), className),\n    [alignment, gap, padding, className]\n  )\n\n  function getPosition(index: number) {\n    if (index === 0) return \"first\"\n    if (index === childrenArray.length - 1) return \"last\"\n    return \"middle\"\n  }\n\n  return (\n    <div ref={containerRef} data-slot=\"dashrow\" className={containerClass}>\n      {childrenArray.map((child, index) => (\n        <Fragment key={index}>\n          <div\n            data-slot=\"dashrow-panel\"\n            ref={(el) => {\n              childRefs.current[index] = el\n            }}\n            className={dashrowChildVariants({\n              alignment,\n              position: getPosition(index),\n            })}\n          >\n            {child}\n          </div>\n\n          {resizable && index < childrenArray.length - 1 && (\n            <div\n              data-slot=\"dashrow-divider\"\n              data-dashrow-divider\n              data-dragging=\"false\"\n              role=\"separator\"\n              aria-orientation=\"vertical\"\n              aria-label={`${UI_I18N[locale].dashrow.resizePanels} ${index + 1} ${index + 2}`}\n              aria-valuenow={getInitialValueNow(alignment, index, count)}\n              aria-valuemin={15}\n              aria-valuemax={85}\n              tabIndex={0}\n              className={cn(\n                dashrowDividerVariants(),\n                \"group/divider -mx-2 min-h-11 min-w-8 px-2\"\n              )}\n            >\n              <div\n                data-slot=\"dashrow-divider-handle\"\n                className={dashrowDividerHandleVariants()}\n              />\n            </div>\n          )}\n        </Fragment>\n      ))}\n    </div>\n  )\n}\n\nDashrow.displayName = \"Dashrow\"\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}
