{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"stretch-tabs","type":"registry:ui","title":"Stretch Tabs","description":"Segmented control whose pill indicator stretches like rubber toward the new option, thins out as it pulls, then snaps shut.","dependencies":["motion"],"registryDependencies":[],"files":[{"path":"registry/ui/stretch-tabs.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useLayoutEffect, useRef, useState, type KeyboardEvent } from \"react\";\nimport { animate, motion, useMotionValue, useReducedMotion, useTransform } from \"motion/react\";\n\nexport interface StretchTabsProps {\n  /** Segment labels. */\n  options?: string[];\n  /** Controlled selected option. */\n  value?: string;\n  /** Initial option when uncontrolled. */\n  defaultValue?: string;\n  /** Called with the newly selected option. */\n  onChange?: (value: string) => void;\n  /** How stretchy the indicator is: 0 snaps rigidly, 1 is very gooey. */\n  stretch?: number;\n  /** Accessible name of the group. */\n  label?: string;\n  className?: string;\n}\n\nconst DEFAULT_OPTIONS = [\"Day\", \"Week\", \"Month\", \"Year\"];\n\nexport function StretchTabs({\n  options = DEFAULT_OPTIONS,\n  value,\n  defaultValue,\n  onChange,\n  stretch = 0.6,\n  label = \"View\",\n  className,\n}: StretchTabsProps) {\n  const reduce = useReducedMotion();\n  const [inner, setInner] = useState(defaultValue ?? options[0]);\n  const current = value ?? inner;\n  const index = Math.max(0, options.indexOf(current));\n  const btns = useRef<(HTMLButtonElement | null)[]>([]);\n  const first = useRef(true);\n  const indexRef = useRef(index);\n  useLayoutEffect(() => {\n    indexRef.current = index;\n  });\n\n  // The indicator is two independent edges. The leading edge races ahead on a stiff\n  // spring, the trailing edge lags on a soft one, so it stretches then snaps shut.\n  const left = useMotionValue(0);\n  const right = useMotionValue(0);\n  const width = useTransform(() => right.get() - left.get());\n  const rest = useRef(0);\n  // Keep the area roughly constant: longer = thinner.\n  const scaleY = useTransform(width, (w) => (rest.current ? Math.min(1, Math.max(0.72, rest.current / w) ** 0.5) : 1));\n\n  const place = (i: number, instant: boolean) => {\n    const el = btns.current[i];\n    if (!el) return;\n    const l = el.offsetLeft;\n    const r = l + el.offsetWidth;\n    rest.current = el.offsetWidth;\n    if (instant || reduce) {\n      left.jump(l);\n      right.jump(r);\n      return;\n    }\n    const goingRight = l > left.get();\n    const lead = { type: \"spring\", stiffness: 700, damping: 38 } as const;\n    const lag = { type: \"spring\", stiffness: 700 - 520 * stretch, damping: 38 - 20 * stretch } as const;\n    animate(left, l, goingRight ? lag : lead);\n    animate(right, r, goingRight ? lead : lag);\n  };\n  const placeRef = useRef(place);\n  useLayoutEffect(() => {\n    placeRef.current = place;\n  });\n\n  useLayoutEffect(() => {\n    placeRef.current(index, first.current);\n    first.current = false;\n  }, [index, options]);\n\n  useLayoutEffect(() => {\n    const parent = btns.current[0]?.parentElement;\n    if (!parent) return;\n    const ro = new ResizeObserver(() => placeRef.current(indexRef.current, true));\n    ro.observe(parent);\n    return () => ro.disconnect();\n  }, []);\n\n  const select = (i: number) => {\n    const next = options[(i + options.length) % options.length];\n    if (value === undefined) setInner(next);\n    if (next !== current) onChange?.(next);\n    btns.current[(i + options.length) % options.length]?.focus();\n  };\n\n  const onKey = (e: KeyboardEvent) => {\n    const step = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }[e.key];\n    if (step) select(index + step);\n    else if (e.key === \"Home\") select(0);\n    else if (e.key === \"End\") select(options.length - 1);\n    else return;\n    e.preventDefault();\n  };\n\n  return (\n    <div\n      role=\"radiogroup\"\n      aria-label={label}\n      onKeyDown={onKey}\n      className={`relative inline-flex rounded-full border border-border bg-muted p-1 ${className ?? \"\"}`}\n    >\n      <motion.span\n        aria-hidden\n        className=\"absolute inset-y-1 left-0 rounded-full bg-background shadow-[0_1px_2px_rgb(0_0_0/.2),0_4px_14px_-4px_rgb(0_0_0/.35)] ring-1 ring-border\"\n        style={{ x: left, width, scaleY }}\n      />\n      {options.map((o, i) => (\n        <button\n          key={o}\n          ref={(el) => {\n            btns.current[i] = el;\n          }}\n          type=\"button\"\n          role=\"radio\"\n          aria-checked={i === index}\n          tabIndex={i === index ? 0 : -1}\n          onClick={() => select(i)}\n          className={`relative z-10 h-9 select-none rounded-full px-5 text-sm font-medium outline-none transition-[color,scale] duration-200 focus-visible:ring-2 focus-visible:ring-ring active:scale-95 ${\n            i === index ? \"text-foreground\" : \"text-muted-foreground hover:text-foreground\"\n          }`}\n        >\n          {o}\n        </button>\n      ))}\n    </div>\n  );\n}\n"}]}