{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "segmented-control",
  "type": "registry:component",
  "title": "Segmented Control",
  "description": "iOS-style segmented control with a thumb that slides to the selected option.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "micro",
    "segmented",
    "tabs",
    "radio",
    "form",
    "slider"
  ],
  "docs": "https://hyperblur.dev/effects/segmented-control",
  "files": [
    {
      "path": "registry/hyperblur/segmented-control.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/segmented-control.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface SegmentedOption {\n  value: string;\n  label: string;\n  disabled?: boolean;\n}\n\nexport interface SegmentedControlProps\n  extends Omit<React.HTMLAttributes<HTMLDivElement>, \"onChange\" | \"defaultValue\" | \"children\"> {\n  options: SegmentedOption[];\n  value?: string;\n  defaultValue?: string;\n  onChange?: (value: string) => void;\n  size?: \"sm\" | \"md\";\n  color?: string;\n  fullWidth?: boolean;\n}\n\nconst css = `\n.hb-seg {\n  --hb-p: 3px;\n  position: relative;\n  display: inline-flex;\n  align-items: stretch;\n  padding: var(--hb-p);\n  border-radius: 12px;\n  background: rgba(255, 255, 255, 0.07);\n  color: #fff;\n  isolation: isolate;\n}\n.hb-seg[data-full=\"true\"] { display: flex; width: 100%; }\n.hb-seg[data-size=\"sm\"] { border-radius: 10px; }\n.hb-seg-t {\n  position: absolute;\n  top: var(--hb-p);\n  bottom: var(--hb-p);\n  left: 0;\n  z-index: 0;\n  width: var(--hb-tw, 0px);\n  border-radius: 9px;\n  background: var(--hb-c);\n  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);\n  transform: translateX(var(--hb-tx, 0px));\n  opacity: 0;\n  pointer-events: none;\n}\n.hb-seg[data-size=\"sm\"] .hb-seg-t { border-radius: 7px; }\n.hb-seg[data-ready=\"true\"] .hb-seg-t { opacity: 1; }\n.hb-seg[data-anim=\"true\"] .hb-seg-t {\n  transition: transform 0.4s cubic-bezier(0.34, 1.3, 0.64, 1), width 0.4s cubic-bezier(0.34, 1.3, 0.64, 1);\n}\n.hb-seg-i {\n  position: relative;\n  z-index: 1;\n  flex: 0 0 auto;\n  margin: 0;\n  padding: 0.55em 1.1em;\n  font: inherit;\n  font-size: 14px;\n  font-weight: 600;\n  line-height: 1.2;\n  white-space: nowrap;\n  color: rgba(255, 255, 255, 0.62);\n  background: transparent;\n  border: 0;\n  border-radius: 9px;\n  cursor: pointer;\n  -webkit-tap-highlight-color: transparent;\n  transition: color 0.2s ease;\n}\n.hb-seg[data-full=\"true\"] .hb-seg-i { flex: 1 1 0; }\n.hb-seg[data-size=\"sm\"] .hb-seg-i { padding: 0.35em 0.85em; font-size: 13px; border-radius: 7px; }\n.hb-seg-i:hover:not(:disabled) { color: rgba(255, 255, 255, 0.9); }\n.hb-seg-i[aria-checked=\"true\"] { color: #fff; }\n.hb-seg-i:focus-visible { outline: 2px solid #2f7bff; outline-offset: 1px; }\n.hb-seg-i:disabled { opacity: 0.4; cursor: not-allowed; }\n.hb-seg:not([data-ready=\"true\"]) .hb-seg-i[aria-checked=\"true\"] { background: var(--hb-c); }\n@media (prefers-reduced-motion: reduce) {\n  .hb-seg[data-anim=\"true\"] .hb-seg-t, .hb-seg-i { transition: none; }\n}\n`;\n\nexport function SegmentedControl({\n  options,\n  value,\n  defaultValue,\n  onChange,\n  size = \"md\",\n  color = \"rgba(255,255,255,0.18)\",\n  fullWidth = false,\n  className = \"\",\n  style,\n  ...rest\n}: SegmentedControlProps) {\n  const root = React.useRef<HTMLDivElement>(null);\n  const items = React.useRef(new Map<string, HTMLButtonElement>());\n  const [inner, setInner] = React.useState(\n    () => defaultValue ?? options.find((o) => !o.disabled)?.value ?? \"\",\n  );\n  const current = value !== undefined ? value : inner;\n  const hasCurrent = options.some((o) => o.value === current);\n  const tabValue = hasCurrent ? current : options.find((o) => !o.disabled)?.value;\n\n  const measure = React.useCallback(() => {\n    const box = root.current;\n    const el = items.current.get(current);\n    if (!box || !el) {\n      if (box) box.dataset.ready = \"false\";\n      return;\n    }\n    box.style.setProperty(\"--hb-tx\", `${el.offsetLeft}px`);\n    box.style.setProperty(\"--hb-tw\", `${el.offsetWidth}px`);\n    box.dataset.ready = \"true\";\n  }, [current]);\n\n  React.useLayoutEffect(() => {\n    measure();\n  }, [measure, options, size, fullWidth]);\n\n  React.useEffect(() => {\n    const box = root.current;\n    if (!box) return;\n    const frame = requestAnimationFrame(() => {\n      box.dataset.anim = \"true\";\n    });\n    let observer: ResizeObserver | undefined;\n    if (typeof ResizeObserver !== \"undefined\") {\n      observer = new ResizeObserver(measure);\n      observer.observe(box);\n      items.current.forEach((el) => observer?.observe(el));\n    }\n    window.addEventListener(\"resize\", measure);\n    return () => {\n      cancelAnimationFrame(frame);\n      observer?.disconnect();\n      window.removeEventListener(\"resize\", measure);\n    };\n  }, [measure, options]);\n\n  function select(next: string) {\n    if (value === undefined) setInner(next);\n    if (next !== current) onChange?.(next);\n  }\n\n  function handleKeyDown(e: React.KeyboardEvent<HTMLButtonElement>, from: string) {\n    const enabled = options.filter((o) => !o.disabled);\n    if (enabled.length === 0) return;\n    const index = enabled.findIndex((o) => o.value === from);\n    let target: SegmentedOption | undefined;\n    if (e.key === \"ArrowRight\" || e.key === \"ArrowDown\") {\n      target = enabled[(index + 1) % enabled.length];\n    } else if (e.key === \"ArrowLeft\" || e.key === \"ArrowUp\") {\n      target = enabled[(index - 1 + enabled.length) % enabled.length];\n    } else if (e.key === \"Home\") {\n      target = enabled[0];\n    } else if (e.key === \"End\") {\n      target = enabled[enabled.length - 1];\n    }\n    if (!target) return;\n    e.preventDefault();\n    items.current.get(target.value)?.focus();\n    select(target.value);\n  }\n\n  const vars = { \"--hb-c\": color, ...style } as React.CSSProperties;\n\n  return (\n    <div\n      ref={root}\n      role=\"radiogroup\"\n      className={`hb-seg ${className}`}\n      style={vars}\n      data-size={size}\n      data-full={fullWidth}\n      {...rest}\n    >\n      <style href=\"hb-segmented-control\" precedence=\"default\">{css}</style>\n      <span className=\"hb-seg-t\" aria-hidden=\"true\" />\n      {options.map((opt) => {\n        const active = opt.value === current;\n        return (\n          <button\n            key={opt.value}\n            ref={(el) => {\n              if (el) items.current.set(opt.value, el);\n              else items.current.delete(opt.value);\n            }}\n            type=\"button\"\n            role=\"radio\"\n            aria-checked={active}\n            className=\"hb-seg-i\"\n            disabled={opt.disabled}\n            tabIndex={opt.value === tabValue ? 0 : -1}\n            onClick={() => select(opt.value)}\n            onKeyDown={(e) => handleKeyDown(e, opt.value)}\n          >\n            {opt.label}\n          </button>\n        );\n      })}\n    </div>\n  );\n}\n"
    }
  ]
}