{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"stretch-rail","type":"registry:ui","title":"Stretch Rail","description":"Sidebar navigation whose glowing line indicator stretches toward the new item and springs back to size.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/stretch-rail.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { animate, motion, useMotionValue, useTransform } from \"motion/react\";\nimport { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\nexport interface StretchRailItem {\n  label: string;\n  icon?: ReactNode;\n  /** Small count or tag shown on the right. */\n  badge?: string;\n}\n\nexport interface StretchRailProps {\n  items: StretchRailItem[];\n  /** Index selected at first render. */\n  defaultActive?: number;\n  /** Indicator color. */\n  color?: string;\n  /** Spring stiffness of the leading edge; the trailing edge follows at a third. */\n  stiffness?: number;\n  /** Show the faint rail behind the indicator. */\n  showTrack?: boolean;\n  onSelect?: (index: number) => void;\n  className?: string;\n}\n\n/** Top and bottom px of the indicator for an item, inset a little. */\nconst edges = (el: HTMLElement | null | undefined) => (el ? [el.offsetTop + 8, el.offsetTop + el.offsetHeight - 8] : [0, 0]);\n\nexport function StretchRail({\n  items,\n  defaultActive = 0,\n  color = \"#c6f24e\",\n  stiffness = 420,\n  showTrack = true,\n  onSelect,\n  className,\n}: StretchRailProps) {\n  const [active, setActive] = useState(defaultActive);\n  const listRef = useRef<HTMLUListElement>(null);\n  const itemRefs = useRef<(HTMLButtonElement | null)[]>([]);\n  const prev = useRef(defaultActive);\n  const top = useMotionValue(0);\n  const bottom = useMotionValue(0);\n  const height = useTransform(() => Math.max(0, bottom.get() - top.get()));\n\n  // Snap into place on mount and whenever the list resizes.\n  useLayoutEffect(() => {\n    const snap = () => {\n      const [t, b] = edges(itemRefs.current[prev.current]);\n      top.jump(t);\n      bottom.jump(b);\n    };\n    snap();\n    const ro = new ResizeObserver(snap);\n    ro.observe(listRef.current!);\n    return () => ro.disconnect();\n  }, [top, bottom]);\n\n  // The edge moving toward the target leads; the other trails, so the line stretches then catches up.\n  useEffect(() => {\n    if (active === prev.current) return;\n    const down = active > prev.current;\n    prev.current = active;\n    const [t, b] = edges(itemRefs.current[active]);\n    const lead = { type: \"spring\", stiffness, damping: 30 } as const;\n    const trail = { type: \"spring\", stiffness: stiffness / 3, damping: 24 } as const;\n    const a1 = animate(top, t, down ? trail : lead);\n    const a2 = animate(bottom, b, down ? lead : trail);\n    return () => {\n      a1.stop();\n      a2.stop();\n    };\n  }, [active, stiffness, top, bottom]);\n\n  const select = (i: number) => {\n    setActive(i);\n    onSelect?.(i);\n  };\n\n  const onKey = (e: KeyboardEvent<HTMLUListElement>) => {\n    const i = itemRefs.current.indexOf(document.activeElement as HTMLButtonElement);\n    if (i < 0) return;\n    const next = { ArrowDown: i + 1, ArrowUp: i - 1, Home: 0, End: items.length - 1 }[e.key];\n    if (next === undefined) return;\n    e.preventDefault();\n    itemRefs.current[(next + items.length) % items.length]?.focus();\n  };\n\n  return (\n    <nav className={cn(\"w-full\", className)}>\n      <ul ref={listRef} onKeyDown={onKey} className=\"relative flex flex-col gap-1 pl-4\">\n        {showTrack && <li aria-hidden className=\"absolute bottom-2 left-0 top-2 w-0.5 rounded-full bg-border\" />}\n        <motion.li\n          aria-hidden\n          className=\"absolute left-0 top-0 w-0.5 rounded-full\"\n          style={{ y: top, height, background: color, boxShadow: `0 0 12px ${color}, 0 0 2px ${color}` }}\n        />\n        {items.map((it, i) => {\n          const on = i === active;\n          return (\n            <li key={it.label}>\n              <button\n                ref={(el) => {\n                  itemRefs.current[i] = el;\n                }}\n                type=\"button\"\n                aria-current={on ? \"page\" : undefined}\n                onClick={() => select(i)}\n                className={cn(\n                  \"group flex w-full items-center gap-3 rounded-lg px-3 py-2.5 text-left text-sm font-medium outline-none transition-colors hover:bg-muted/60 focus-visible:ring-2 focus-visible:ring-ring\",\n                  on ? \"text-foreground\" : \"text-muted-foreground hover:text-foreground\",\n                )}\n              >\n                {it.icon && (\n                  <span\n                    className=\"grid size-4 shrink-0 place-items-center transition-colors duration-300 [&>svg]:size-full\"\n                    style={{ color: on ? color : undefined }}\n                  >\n                    {it.icon}\n                  </span>\n                )}\n                <span className=\"flex-1 truncate transition-transform duration-300 ease-out\" style={{ transform: on ? \"translateX(3px)\" : \"none\" }}>\n                  {it.label}\n                </span>\n                {it.badge && (\n                  <span className=\"rounded-md bg-muted px-1.5 py-0.5 font-mono text-[0.65rem] text-muted-foreground\">{it.badge}</span>\n                )}\n              </button>\n            </li>\n          );\n        })}\n      </ul>\n    </nav>\n  );\n}\n"}]}