{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"liquid-segmented","type":"registry:component","title":"Liquid Segmented Control","description":"A selection surface with a little momentum.","dependencies":[],"categories":["input","segmented","radiogroup","keyboard","density"],"files":[{"path":"components/lab/liquid-segmented/liquid-segmented.tsx","target":"components/lab/liquid-segmented/liquid-segmented.tsx","type":"registry:component","content":"\"use client\";\n\nimport {\n  useCallback,\n  useEffect,\n  useLayoutEffect,\n  useRef,\n  useState,\n  type KeyboardEvent,\n} from \"react\";\nimport \"./liquid-segmented.css\";\n\nexport type SegmentedOption = {\n  value: string;\n  label: string;\n  disabled?: boolean;\n};\n\nexport type LiquidSegmentedProps = {\n  options: SegmentedOption[];\n  value: string;\n  onChange: (value: string) => void;\n  /** Accessible group name. */\n  label: string;\n  size?: \"sm\" | \"md\";\n  /** Let the surface stretch briefly in the direction of travel. */\n  stretch?: boolean;\n  className?: string;\n};\n\n/**\n * LIQUID SEGMENTED CONTROL\n *\n * A radiogroup with one physical selection surface. The surface is\n * measured from the chosen option and glides between slots; during a\n * change it gains a few pixels toward the direction of travel and\n * settles back. Arrow keys move selection and focus together, disabled\n * options are skipped, and the roving tab stop always points at the\n * current choice.\n */\nexport function LiquidSegmented({\n  options,\n  value,\n  onChange,\n  label,\n  size = \"md\",\n  stretch = true,\n  className,\n}: LiquidSegmentedProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const buttonRefs = useRef(new Map<string, HTMLButtonElement>());\n  const [indicator, setIndicator] = useState({ x: 0, width: 0, ready: false });\n  const [give, setGive] = useState({ amount: 0, direction: 1 });\n  const giveTimer = useRef<number | null>(null);\n  const usable = options.filter((option) => !option.disabled);\n  const activeValue = options.some(\n    (option) => option.value === value && !option.disabled,\n  )\n    ? value\n    : (usable[0]?.value ?? \"\");\n  const selected = options.find((option) => option.value === activeValue);\n\n  const measure = useCallback(() => {\n    const root = rootRef.current;\n    const button = buttonRefs.current.get(activeValue);\n    if (!root || !button) return;\n    setIndicator({\n      x: button.offsetLeft,\n      width: button.offsetWidth,\n      ready: true,\n    });\n  }, [activeValue]);\n\n  useLayoutEffect(() => {\n    measure();\n    const root = rootRef.current;\n    if (!root || typeof ResizeObserver !== \"function\") return;\n    const observer = new ResizeObserver(measure);\n    observer.observe(root);\n    for (const button of buttonRefs.current.values()) observer.observe(button);\n    return () => observer.disconnect();\n  }, [measure]);\n\n  useEffect(() => {\n    const onResize = () => measure();\n    window.addEventListener(\"resize\", onResize);\n    return () => window.removeEventListener(\"resize\", onResize);\n  }, [measure]);\n\n  const choose = (next: string, current: string) => {\n    const from = options.findIndex((option) => option.value === current);\n    const to = options.findIndex((option) => option.value === next);\n    if (stretch && from !== -1 && to !== -1 && from !== to) {\n      const amount = Math.min(18, Math.abs(to - from) * 5);\n      setGive({ amount, direction: to > from ? 1 : -1 });\n      if (giveTimer.current !== null) window.clearTimeout(giveTimer.current);\n      giveTimer.current = window.setTimeout(() => {\n        setGive({ amount: 0, direction: 1 });\n        giveTimer.current = null;\n      }, 190);\n    }\n    onChange(next);\n  };\n\n  useEffect(\n    () => () => {\n      if (giveTimer.current !== null) window.clearTimeout(giveTimer.current);\n    },\n    [],\n  );\n\n  const move = (\n    event: KeyboardEvent<HTMLButtonElement>,\n    direction: 1 | -1,\n  ) => {\n    event.preventDefault();\n    const usable = options.filter((option) => !option.disabled);\n    if (!usable.length) return;\n    const current = usable.findIndex(\n      (option) => option.value === selected?.value,\n    );\n    const next = usable[(current + direction + usable.length) % usable.length];\n    choose(next.value, selected?.value ?? value);\n    buttonRefs.current.get(next.value)?.focus();\n  };\n\n  const onKeyDown = (event: KeyboardEvent<HTMLButtonElement>) => {\n    if (event.key === \"ArrowRight\" || event.key === \"ArrowDown\")\n      move(event, 1);\n    else if (event.key === \"ArrowLeft\" || event.key === \"ArrowUp\")\n      move(event, -1);\n    else if (event.key === \"Home\") {\n      event.preventDefault();\n      const first = options.find((option) => !option.disabled);\n      if (first) {\n        choose(first.value, selected?.value ?? value);\n        buttonRefs.current.get(first.value)?.focus();\n      }\n    } else if (event.key === \"End\") {\n      event.preventDefault();\n      const last = [...options].reverse().find((option) => !option.disabled);\n      if (last) {\n        choose(last.value, selected?.value ?? value);\n        buttonRefs.current.get(last.value)?.focus();\n      }\n    }\n  };\n\n  const width = indicator.width + give.amount;\n  const x =\n    indicator.x -\n    (give.amount && give.direction < 0 ? give.amount : 0);\n\n  return (\n    <div\n      ref={rootRef}\n      className={`ls-root${className ? ` ${className}` : \"\"}`}\n      role=\"radiogroup\"\n      aria-label={label}\n      data-size={size}\n    >\n      <span\n        className=\"ls-indicator\"\n        aria-hidden=\"true\"\n        data-ready={indicator.ready ? \"true\" : \"false\"}\n        style={{ width, transform: `translate3d(${x}px, 0, 0)` }}\n      />\n      {options.map((option) => {\n        const checked = option.value === activeValue;\n        return (\n          <button\n            key={option.value}\n            ref={(node) => {\n              if (node) buttonRefs.current.set(option.value, node);\n              else buttonRefs.current.delete(option.value);\n            }}\n            type=\"button\"\n            role=\"radio\"\n            aria-checked={checked}\n            disabled={option.disabled}\n            tabIndex={checked ? 0 : -1}\n            className=\"ls-option\"\n            onClick={() => choose(option.value, activeValue)}\n            onKeyDown={onKeyDown}\n          >\n            {option.label}\n          </button>\n        );\n      })}\n    </div>\n  );\n}\n"},{"path":"components/lab/liquid-segmented/liquid-segmented.css","target":"components/lab/liquid-segmented/liquid-segmented.css","type":"registry:file","content":".ls-root {\n  --ls-ink: #24211c;\n  --ls-muted: #625d55;\n  --ls-line: rgba(36, 33, 28, 0.13);\n  --ls-surface: #f6f3ea;\n  --ls-pill: #e6d5ae;\n  --ls-pill-ink: #24211c;\n  --ls-focus: #b04a2e;\n  --ls-ease: cubic-bezier(0.16, 1, 0.3, 1);\n  position: relative;\n  display: inline-flex;\n  align-items: stretch;\n  gap: 2px;\n  min-width: 0;\n  max-width: 100%;\n  padding: 3px;\n  border: 1px solid var(--ls-line);\n  border-radius: 999px;\n  background: var(--ls-surface);\n  color: var(--ls-ink);\n  font: inherit;\n  overflow-x: auto;\n  scrollbar-width: none;\n  isolation: isolate;\n}\n\n.ls-root::-webkit-scrollbar {\n  display: none;\n}\n\n.ls-indicator {\n  position: absolute;\n  top: 3px;\n  bottom: 3px;\n  left: 0;\n  z-index: -1;\n  border-radius: 999px;\n  background: var(--ls-pill);\n  box-shadow: inset 0 0 0 1px rgba(36, 33, 28, 0.08);\n  opacity: 0;\n  pointer-events: none;\n  transition:\n    transform 380ms var(--ls-ease),\n    width 380ms var(--ls-ease),\n    opacity 120ms linear;\n  will-change: transform, width;\n}\n\n.ls-indicator[data-ready=\"true\"] {\n  opacity: 1;\n}\n\n.ls-option {\n  position: relative;\n  z-index: 1;\n  flex: 0 0 auto;\n  min-height: 34px;\n  padding: 6px 16px;\n  border: 0;\n  border-radius: 999px;\n  background: transparent;\n  color: var(--ls-muted);\n  font: inherit;\n  font-size: 0.8125rem;\n  font-weight: 550;\n  letter-spacing: -0.004em;\n  white-space: nowrap;\n  cursor: pointer;\n  transition: color 190ms var(--ls-ease);\n}\n\n.ls-root[data-size=\"sm\"] .ls-option {\n  min-height: 28px;\n  padding: 3px 12px;\n  font-size: 0.75rem;\n}\n\n.ls-option:hover:not(:disabled) {\n  color: var(--ls-ink);\n}\n\n.ls-option[aria-checked=\"true\"] {\n  color: var(--ls-pill-ink);\n}\n\n.ls-option:disabled {\n  opacity: 0.38;\n  cursor: not-allowed;\n}\n\n.ls-option:focus-visible {\n  outline: 2px solid var(--ls-focus);\n  outline-offset: 2px;\n}\n\n@media (max-width: 400px) {\n  .ls-option {\n    padding-inline: 12px;\n    font-size: 0.75rem;\n  }\n}\n\n@media (prefers-reduced-motion: reduce) {\n  .ls-indicator,\n  .ls-option {\n    transition-duration: 0.01ms;\n  }\n}\n"}]}