{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "segmented-control",
  "title": "Segmented control",
  "description": "iOS-style segmented control whose thumb springs to the chosen segment and squeezes while pressed, with sizes, icons, full-width mode, dividers that hide next to the thumb, and radio-group arrow keys.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json"
  ],
  "files": [
    {
      "path": "registry/components/navigation/segmented-control.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { motion, useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { GooduiTokens, spring as springs } from \"@/lib/goodui-motion\"\n\nexport type SegmentedControlOption = {\n  /** Unique key of the option; this is what value and onValueChange use. */\n  value: string\n  /** Visible text. Leave it out for an icon-only segment (then set ariaLabel). */\n  label?: React.ReactNode\n  /** Icon component (e.g. a lucide icon), sized by the control's size. */\n  icon?: React.ComponentType<{ className?: string }>\n  /** Accessible name, required when the option shows only an icon. */\n  ariaLabel?: string\n  disabled?: boolean\n}\n\nexport type SegmentedControlProps = Omit<React.ComponentProps<\"div\">, \"defaultValue\" | \"onChange\"> & {\n  options: SegmentedControlOption[]\n  /** Selected option value (controlled). */\n  value?: string\n  /** Initially selected option when uncontrolled; defaults to the first enabled option. */\n  defaultValue?: string\n  /** Called with the new value on click or arrow-key selection. */\n  onValueChange?: (value: string) => void\n  /** Height: sm 32px, md 36px, lg 44px. */\n  size?: \"sm\" | \"md\" | \"lg\"\n  /** Stretch to the container width with equal segments. */\n  fullWidth?: boolean\n  /** Disables the whole control. */\n  disabled?: boolean\n}\n\nfunction useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (v: T) => void) {\n  const [inner, setInner] = React.useState(defaultValue)\n  const controlled = value !== undefined\n  const current = controlled ? value : inner\n  const set = React.useCallback(\n    (next: T) => {\n      if (!controlled) setInner(next)\n      onChange?.(next)\n    },\n    [controlled, onChange],\n  )\n  return [current, set] as const\n}\n\nconst SIZE = {\n  sm: { root: \"h-8 rounded-lg p-0.5 text-xs\", item: \"px-2.5 gap-1.5 [&_svg]:size-3.5\", radius: 6 },\n  md: { root: \"h-9 rounded-[9px] p-0.5 text-sm\", item: \"px-3 gap-2 [&_svg]:size-4\", radius: 7 },\n  lg: { root: \"h-11 rounded-xl p-1 text-sm\", item: \"px-4 gap-2 [&_svg]:size-4\", radius: 8 },\n}\n\n/**\n * iOS-style segmented control: a springy thumb slides to the chosen segment and squeezes while pressed.\n * Behaves as a radio group: arrow keys, Home and End move the selection.\n */\nexport function SegmentedControl({\n  options,\n  value,\n  defaultValue,\n  onValueChange,\n  size = \"md\",\n  fullWidth = false,\n  disabled = false,\n  className,\n  ...props\n}: SegmentedControlProps) {\n  const uid = React.useId()\n  const reduce = useReducedMotion() ?? false\n  const [current, setCurrent] = useControllable(value, defaultValue ?? options.find((o) => !o.disabled)?.value ?? \"\", onValueChange)\n  const [pressed, setPressed] = React.useState<string | null>(null)\n  const refs = React.useRef<(HTMLButtonElement | null)[]>([])\n  const s = SIZE[size]\n  const selectedIndex = options.findIndex((o) => o.value === current)\n\n  const enabled = options.map((o, i) => ({ o, i })).filter((x) => !x.o.disabled && !disabled)\n  const move = (e: React.KeyboardEvent, i: number) => {\n    const pos = enabled.findIndex((x) => x.i === i)\n    let next: (typeof enabled)[number] | undefined\n    // In right-to-left layouts the visual order is mirrored, so Left moves forward.\n    const rtl = getComputedStyle(e.currentTarget).direction === \"rtl\"\n    const forward = e.key === \"ArrowDown\" || e.key === (rtl ? \"ArrowLeft\" : \"ArrowRight\")\n    const back = e.key === \"ArrowUp\" || e.key === (rtl ? \"ArrowRight\" : \"ArrowLeft\")\n    if (forward) next = enabled[(pos + 1) % enabled.length]\n    else if (back) next = enabled[(pos - 1 + enabled.length) % enabled.length]\n    else if (e.key === \"Home\") next = enabled[0]\n    else if (e.key === \"End\") next = enabled[enabled.length - 1]\n    if (!next) return\n    e.preventDefault()\n    setCurrent(next.o.value)\n    refs.current[next.i]?.focus()\n  }\n\n  const spring = reduce ? { duration: 0 } : springs.snap\n\n  return (\n    <div\n      role=\"radiogroup\"\n      aria-disabled={disabled || undefined}\n      data-slot=\"segmented-control\"\n      className={cn(\n        \"relative isolate inline-flex max-w-full items-stretch bg-muted shadow-[var(--pc-well)] select-none\",\n        s.root,\n        fullWidth && \"flex w-full\",\n        disabled && \"pointer-events-none opacity-50\",\n        className,\n      )}\n      onPointerUp={() => setPressed(null)}\n      onPointerLeave={() => setPressed(null)}\n      {...props}\n    >\n      <GooduiTokens />\n      {options.map((o, i) => {\n        const selected = i === selectedIndex\n        const Icon = o.icon\n        // Hairline dividers between unselected neighbours, hidden next to the thumb like iOS.\n        const showDivider = i > 0 && i !== selectedIndex && i - 1 !== selectedIndex\n        return (\n          <button\n            key={o.value}\n            ref={(el) => {\n              refs.current[i] = el\n            }}\n            type=\"button\"\n            role=\"radio\"\n            aria-checked={selected}\n            aria-label={o.ariaLabel}\n            tabIndex={selected || (selectedIndex === -1 && i === enabled[0]?.i) ? 0 : -1}\n            disabled={o.disabled || disabled}\n            onClick={() => setCurrent(o.value)}\n            onKeyDown={(e) => move(e, i)}\n            onPointerDown={() => setPressed(o.value)}\n            className={cn(\n              \"pc-press pc-press-soft relative inline-flex min-w-0 cursor-pointer items-center justify-center font-medium whitespace-nowrap text-foreground/65 transition-colors outline-none hover:text-foreground focus-visible:z-20 focus-visible:ring-3 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-40 aria-checked:text-foreground\",\n              s.item,\n              fullWidth && \"flex-1\",\n            )}\n            style={{ borderRadius: s.radius }}\n          >\n            <span\n              aria-hidden=\"true\"\n              className={cn(\"absolute top-1/4 bottom-1/4 start-0 w-px bg-foreground/15 transition-opacity duration-200\", showDivider ? \"opacity-100\" : \"opacity-0\")}\n            />\n            {selected && (\n              <motion.span\n                layoutId={`${uid}-thumb`}\n                aria-hidden=\"true\"\n                className=\"absolute inset-0 -z-10 bg-background shadow-[var(--pc-e1)] dark:bg-input\"\n                style={{ borderRadius: s.radius }}\n                initial={false}\n                animate={{ scale: pressed === o.value && !reduce ? 0.95 : 1 }}\n                transition={spring}\n              />\n            )}\n            <motion.span\n              className=\"relative inline-flex min-w-0 items-center gap-[inherit]\"\n              animate={{ scale: pressed === o.value && !selected && !reduce ? 0.96 : 1 }}\n              transition={spring}\n            >\n              {Icon && <Icon className=\"shrink-0\" />}\n              {o.label !== undefined && <span className=\"truncate\">{o.label}</span>}\n            </motion.span>\n          </button>\n        )\n      })}\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/segmented-control.tsx"
    }
  ],
  "categories": [
    "components",
    "navigation"
  ],
  "type": "registry:component"
}