{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"droplet-tabs","type":"registry:ui","title":"Droplet Tabs","description":"A segmented control whose highlight stretches like a drop of liquid as it glides to the new option, then snaps tight. Arrow keys move it.","dependencies":["motion"],"registryDependencies":[],"files":[{"path":"registry/ui/droplet-tabs.tsx","type":"registry:ui","content":"\"use client\";\n\nimport { useEffect, useRef, useState, type KeyboardEvent } from \"react\";\nimport { animate, motion, useMotionValue, useReducedMotion, useTransform } from \"motion/react\";\n\nexport interface DropletTabsProps {\n  /** Option labels (also used as values). */\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  onValueChange?: (value: string) => void;\n  /** Accessible name of the group. */\n  label?: string;\n  /** Highlight color; defaults to the theme primary. */\n  color?: string;\n  /** How much the highlight stretches while travelling, 0-1. */\n  stretch?: number;\n  className?: string;\n}\n\nexport function DropletTabs({\n  options = [\"Day\", \"Week\", \"Month\", \"Year\"],\n  value,\n  defaultValue,\n  onValueChange,\n  label = \"Time range\",\n  color,\n  stretch = 0.6,\n  className,\n}: DropletTabsProps) {\n  const [inner, setInner] = useState(defaultValue ?? options[0]);\n  const current = value ?? inner;\n  const idx = Math.max(0, options.indexOf(current));\n  const refs = useRef<(HTMLButtonElement | null)[]>([]);\n  const root = useRef<HTMLDivElement>(null);\n  const prev = useRef(-1);\n  const reduce = useReducedMotion();\n\n  const left = useMotionValue(0);\n  const right = useMotionValue(0);\n  const target = useRef(0);\n  const width = useTransform(() => right.get() - left.get());\n  // Thin out while stretched, like a drop of liquid pulled between two points.\n  const scaleY = useTransform(() => Math.max(0.72, 1 - Math.max(0, width.get() - target.current) / 260));\n\n  const place = (i: number, glide: boolean) => {\n    const el = refs.current[i];\n    if (!el) return;\n    const l = el.offsetLeft;\n    const r = l + el.offsetWidth;\n    target.current = el.offsetWidth;\n    if (!glide) {\n      // Next frame: values set before Motion has mounted the element never reach the DOM.\n      requestAnimationFrame(() => {\n        left.set(l);\n        right.set(r);\n      });\n      return;\n    }\n    // The leading edge snaps ahead on a stiff spring, the trailing edge catches up on a loose one.\n    const fast = { type: \"spring\", stiffness: 520, damping: 34 } as const;\n    const slow = { type: \"spring\", stiffness: 520 - 360 * stretch, damping: 34 - 14 * stretch } as const;\n    const forward = i > prev.current;\n    animate(left, l, forward ? slow : fast);\n    animate(right, r, forward ? fast : slow);\n  };\n\n  useEffect(() => {\n    place(idx, prev.current >= 0 && prev.current !== idx && !reduce);\n    prev.current = idx;\n    // place only touches refs and motion values\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [idx, reduce, options.length]);\n\n  useEffect(() => {\n    const el = root.current;\n    if (!el) return;\n    const ro = new ResizeObserver(() => place(prev.current, false));\n    ro.observe(el);\n    return () => ro.disconnect();\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, []);\n\n  const select = (i: number, focus = false) => {\n    const next = options[(i + options.length) % options.length];\n    if (focus) refs.current[options.indexOf(next)]?.focus();\n    if (next === current) return;\n    setInner(next);\n    onValueChange?.(next);\n  };\n\n  const onKey = (e: KeyboardEvent) => {\n    const map: Record<string, number> = {\n      ArrowRight: idx + 1,\n      ArrowDown: idx + 1,\n      ArrowLeft: idx - 1,\n      ArrowUp: idx - 1,\n      Home: 0,\n      End: options.length - 1,\n    };\n    if (!(e.key in map)) return;\n    e.preventDefault();\n    select(map[e.key], true);\n  };\n\n  return (\n    <div\n      ref={root}\n      role=\"radiogroup\"\n      aria-label={label}\n      onKeyDown={onKey}\n      className={`relative inline-flex rounded-full border border-border bg-muted/60 p-1 shadow-[inset_0_1px_3px_rgb(0_0_0/.25)] ${className ?? \"\"}`}\n    >\n      <motion.span\n        aria-hidden\n        className=\"absolute inset-y-1 left-0 rounded-full shadow-[inset_0_1px_0_rgb(255_255_255/.35),0_6px_18px_-8px_rgb(0_0_0/.6)]\"\n        style={{ x: left, width, scaleY, background: color ?? \"var(--primary)\" }}\n      />\n      {options.map((o, i) => {\n        const on = i === idx;\n        return (\n          <motion.button\n            key={o}\n            ref={(el) => {\n              refs.current[i] = el;\n            }}\n            type=\"button\"\n            role=\"radio\"\n            aria-checked={on}\n            tabIndex={on ? 0 : -1}\n            onClick={() => select(i)}\n            whileTap={{ scale: 0.92 }}\n            transition={{ type: \"spring\", stiffness: 600, damping: 20 }}\n            className={`relative z-10 rounded-full px-4 py-1.5 text-sm font-medium outline-none transition-colors duration-200 focus-visible:ring-[3px] focus-visible:ring-ring/60 ${\n              on ? (color ? \"text-white\" : \"text-primary-foreground\") : \"text-muted-foreground hover:text-foreground\"\n            }`}\n          >\n            {o}\n          </motion.button>\n        );\n      })}\n    </div>\n  );\n}\n"}]}