{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "components-segmented-control",
  "type": "registry:component",
  "title": "Segmented control",
  "description": "One choice among a few, as a Mac view switcher",
  "author": "sudo-kw <https://github.com/sudo-kw>",
  "dependencies": [
    "@gpuix/native@0.11.0",
    "@gpuix/react@0.11.0",
    "effect@4.1.2",
    "react@^19.2.4"
  ],
  "registryDependencies": [
    "https://gpuixui.sudokw.com/gpuix-icons.json",
    "https://gpuixui.sudokw.com/gpuix-motion.json",
    "https://gpuixui.sudokw.com/gpuix-primitives.json",
    "https://gpuixui.sudokw.com/gpuix-tokens.json"
  ],
  "files": [
    {
      "path": "packages/components/src/internal/measure.ts",
      "target": "@ui/gpuixui/components/internal/measure.ts",
      "type": "registry:lib",
      "content": "import type { EventPayload } from \"@gpuix/react\";\nimport * as Option from \"effect/Option\";\n\nexport interface MeasuredBox {\n  readonly x: number;\n  readonly y: number;\n  readonly width: number;\n  readonly height: number;\n}\n\nconst measurable = (value: number | undefined): Option.Option<number> =>\n  Option.filter(Option.fromNullishOr(value), Number.isFinite);\n\nexport const measuredBox = (event: EventPayload): Option.Option<MeasuredBox> =>\n  Option.all({\n    x: measurable(event.x),\n    y: measurable(event.y),\n    width: measurable(event.width),\n    height: measurable(event.height),\n  });\n\nexport const measuredHeight = (event: EventPayload): Option.Option<number> =>\n  measurable(event.height);\n\nexport interface MeasuredPoint {\n  readonly x: number;\n  readonly y: number;\n}\n\nexport const measuredPoint = (\n  event: EventPayload,\n): Option.Option<MeasuredPoint> =>\n  Option.all({\n    x: measurable(event.x),\n    y: measurable(event.y),\n  });\n"
    },
    {
      "path": "packages/components/src/internal/presence.tsx",
      "target": "@ui/gpuixui/components/internal/presence.tsx",
      "type": "registry:component",
      "content": "/** @jsxImportSource @gpuix/react */\nimport type { MotionProps } from \"@gpuix/react\";\nimport {\n  delayedTiming,\n  exitWindowMs,\n  fadeFrom,\n  fadeFromAnchor,\n  fadeFromSide,\n  fadeOnly,\n  presenceMotion as motionOfPresence,\n  presenceStyle,\n  transitionWindowMs,\n  type PresenceFrames,\n  type PresenceHitRule,\n  type PresenceSide,\n  type PresenceTiming,\n  type PresenceTransition,\n} from \"../../motion/presence.js\";\nimport {\n  MotionProvider,\n  useMotionPreference,\n  useSurfacePresence,\n  useSurfaceTiming,\n  type MotionProviderProps,\n  type SurfacePresence,\n} from \"../../motion/react/presence.js\";\nimport {\n  motionTiming,\n  type MotionLevel,\n  type MotionPreference,\n} from \"../../motion/timing.js\";\n\nexport { MotionProvider, useMotionPreference, type MotionProviderProps };\n\nexport type SurfaceFrames = PresenceFrames;\n\nexport type SurfaceTiming = PresenceTiming;\n\nexport type PanelSide = PresenceSide;\n\nexport {\n  delayedTiming,\n  exitWindowMs,\n  fadeFrom,\n  fadeFromAnchor,\n  fadeFromSide,\n  fadeOnly,\n  presenceStyle,\n  transitionWindowMs,\n};\nexport type { PresenceHitRule };\n\nexport { useSurfacePresence, useSurfaceTiming, type SurfacePresence };\n\nexport const moveTiming = (\n  level: MotionLevel,\n  preference: MotionPreference,\n): PresenceTransition => motionTiming(level, { phase: \"standard\", preference });\n\nexport const useMoveTiming = (level: MotionLevel): PresenceTransition =>\n  moveTiming(level, useMotionPreference());\n\nexport const presenceMotion = (\n  frames: SurfaceFrames,\n  presence: SurfacePresence,\n  timing: SurfaceTiming = presence.timing,\n): MotionProps => motionOfPresence(frames, presence.present, timing);\n\nexport const PresenceShield = ({ present }: { readonly present: boolean }) =>\n  present ? null : (\n    <div\n      testId=\"presence-shield\"\n      style={{ position: \"absolute\", top: 0, right: 0, bottom: 0, left: 0 }}\n    />\n  );\n\nexport const presenceTabStop = (\n  present: boolean,\n): { readonly tabIndex?: number } => (present ? {} : { tabIndex: -1 });\n"
    },
    {
      "path": "packages/components/src/internal/theme.ts",
      "target": "@ui/gpuixui/components/internal/theme.ts",
      "type": "registry:lib",
      "content": "import type { StyleDesc } from \"@gpuix/react\";\nimport * as Schema from \"effect/Schema\";\nimport {\n  alpha,\n  avatarPixels,\n  avatarSize,\n  brandTextOn,\n  controlFocusRing,\n  controlHeight,\n  controlPaddingX,\n  currentPlatform,\n  darkTheme,\n  disabledAction,\n  disabledOpacity,\n  elevationShadow,\n  filledHover,\n  filledPressed,\n  findHighlight,\n  focusRing,\n  keyboardFocusRing,\n  lightTheme,\n  nativeTextTheme,\n  pointerWash,\n  selectionOn,\n  statusTone,\n  syntaxColors,\n  targetSize,\n  textStyle,\n  type BoxShadow,\n  type ElevationLevel,\n  type FindHighlightSource,\n  type NativeTextFill,\n  type PlatformName,\n  type PointerWashSurface,\n  type StatusTone,\n  type SyntaxColors,\n  type SyntaxRole,\n  type TextRole,\n  type Theme,\n  type ThemeColors,\n} from \"../../tokens/index.js\";\n\nexport {\n  alpha,\n  avatarPixels,\n  avatarSize,\n  controlFocusRing,\n  controlHeight,\n  controlPaddingX,\n  currentPlatform,\n  darkTheme,\n  disabledAction,\n  disabledOpacity,\n  elevationShadow,\n  filledHover,\n  filledPressed,\n  findHighlight,\n  focusRing,\n  keyboardFocusRing,\n  lightTheme,\n  nativeTextTheme,\n  statusTone,\n  syntaxColors,\n  targetSize,\n  textStyle,\n};\nexport type {\n  BoxShadow,\n  ElevationLevel,\n  FindHighlightSource,\n  NativeTextFill,\n  PlatformName,\n  StatusTone,\n  SyntaxColors,\n  SyntaxRole,\n  TextRole,\n  Theme,\n};\nexport { ThemeProvider, useTheme } from \"../../tokens/react.js\";\n\nexport const keyboardRing = (theme: Theme, invalid = false): StyleDesc =>\n  controlFocusRing(theme, { invalid }) ?? {};\n\nexport interface ControlRingState {\n  readonly disabled?: boolean;\n\n  readonly invalid?: boolean;\n}\n\nexport const controlRing = (\n  theme: Theme,\n  state: ControlRingState = {},\n): StyleDesc => controlFocusRing(theme, state) ?? {};\n\nexport const pressWash = (theme: Theme, replaced: PointerWashSurface): string =>\n  pointerWash(theme, \"active\", replaced) ?? theme.colors.active;\n\nexport const selectionWash = (theme: Theme): StyleDesc => ({\n  selectionColor: theme.colors.selection,\n});\n\nexport const keepContent: StyleDesc = { keyboardFocusDim: false };\n\nexport const disabledFade = (\n  disabled: boolean,\n): { readonly opacity: number } | undefined =>\n  disabled ? { opacity: disabledOpacity } : undefined;\n\nconst Tones = Schema.Literals([\"info\", \"success\", \"warning\", \"danger\"]);\n\nexport type Tone = typeof Tones.Type;\n\nexport const toneOf = (value: unknown): Tone =>\n  Schema.is(Tones)(value) ? value : \"info\";\n\nexport interface ToneStyle {\n  readonly fg: string;\n  readonly bg: string;\n  readonly border: string;\n}\n\nconst TOKEN_TONE: Readonly<Record<Tone, StatusTone>> = {\n  info: \"info\",\n  success: \"success\",\n  warning: \"warning\",\n  danger: \"destructive\",\n};\n\nexport const tone = (theme: Theme, value: Tone): ToneStyle => {\n  const { background, border, foreground } = statusTone(\n    theme,\n    TOKEN_TONE[value],\n  );\n  return { bg: background, border, fg: foreground };\n};\n\nconst softRoles: Readonly<Record<Tone, keyof ThemeColors>> = {\n  info: \"infoSoft\",\n  success: \"successSoft\",\n  warning: \"warningSoft\",\n  danger: \"destructiveSoft\",\n};\n\nexport const messageLink = (theme: Theme, value: Tone): string =>\n  brandTextOn(theme, softRoles[value]);\n\nexport const messageSelection = (theme: Theme, value: Tone): string =>\n  selectionOn(theme, softRoles[value]);\n\nexport const toneSurface = (theme: Theme, value: Tone): StyleDesc => {\n  const { bg, border, fg } = tone(theme, value);\n  return {\n    backgroundColor: bg,\n    color: fg,\n    borderWidth: theme.borderWidth.thin,\n    borderColor: border,\n  };\n};\n"
    },
    {
      "path": "packages/components/src/segmented-control.tsx",
      "target": "@ui/gpuixui/components/segmented-control.tsx",
      "type": "registry:component",
      "content": "/** @jsxImportSource @gpuix/react */\nimport {\n  useGpuix,\n  type KeyEvent,\n  type Props,\n  type PublicInstance,\n} from \"@gpuix/react\";\nimport { Icon, type IconName } from \"../icons/index.js\";\nimport { Button as Action } from \"../primitives/button.js\";\nimport * as Arr from \"effect/Array\";\nimport * as Option from \"effect/Option\";\nimport { useEffect, useRef, useState } from \"react\";\nimport { useMoveTiming } from \"./internal/presence.js\";\nimport { elevationShadow, useTheme } from \"./internal/theme.js\";\nimport { indicatorBox, measuredBox, type TabBox } from \"./tabs/indicator.js\";\n\nexport interface SegmentedControlItem {\n  readonly value: string;\n  readonly label: string;\n  readonly icon?: IconName;\n  readonly disabled?: boolean;\n}\n\nexport interface SegmentedControlProps extends Omit<Props, \"children\"> {\n  readonly label: string;\n  readonly items: ReadonlyArray<SegmentedControlItem>;\n  readonly value: string;\n  readonly onValueChange: (value: string) => void;\n\n  readonly size?: \"sm\" | \"md\";\n}\n\nexport const stepSegment = (\n  items: ReadonlyArray<SegmentedControlItem>,\n  value: string,\n  direction: 1 | -1,\n): Option.Option<string> => {\n  const at = Option.getOrElse(\n    Arr.findFirstIndex(items, (item) => item.value === value),\n    () => 0,\n  );\n  const ahead =\n    direction === 1\n      ? Arr.drop(items, at + 1)\n      : Arr.reverse(Arr.take(items, at));\n  return Option.map(\n    Arr.findFirst(ahead, (item) => item.disabled !== true),\n    (item) => item.value,\n  );\n};\n\nexport const SegmentedControl = ({\n  label,\n  items,\n  value,\n  onValueChange,\n  size = \"sm\",\n  style,\n  ...rest\n}: SegmentedControlProps) => {\n  const theme = useTheme();\n  const renderer = useGpuix().renderer;\n  const segments = useRef(new Map<string, PublicInstance>());\n\n  const follow = useRef(false);\n  const move = useMoveTiming(\"normal\");\n  const [origin, setOrigin] = useState<Option.Option<TabBox>>(Option.none());\n  const [boxes, setBoxes] = useState<ReadonlyMap<string, TabBox>>(new Map());\n  const chipFill =\n    theme.mode === \"dark\" ? theme.colors.selected : theme.colors.background;\n  const chosenBox = Option.fromNullishOr(boxes.get(value));\n  const measured = Option.isSome(origin) && Option.isSome(chosenBox);\n\n  useEffect(() => {\n    if (!follow.current) return;\n    follow.current = false;\n    const target = segments.current.get(value);\n    if (target !== undefined) renderer?.focusElement?.(target.id);\n  }, [value, renderer]);\n\n  const choose = (next: string): void => {\n    follow.current = true;\n    onValueChange(next);\n  };\n\n  const onKeyDown = (event: KeyEvent): void => {\n    const key = event.key ?? \"\";\n    if (key !== \"left\" && key !== \"right\") return;\n    Option.map(stepSegment(items, value, key === \"right\" ? 1 : -1), choose);\n  };\n\n  return (\n    <div\n      role=\"radiogroup\"\n      aria-label={label}\n      {...rest}\n      onKeyDown={onKeyDown}\n      onLayout={(event) => {\n        setOrigin(measuredBox(event));\n        rest.onLayout?.(event);\n      }}\n      style={{\n        position: \"relative\",\n        display: \"flex\",\n        flexDirection: \"row\",\n        gap: 2,\n        padding: 2,\n        borderRadius: theme.radius.md,\n        backgroundColor: theme.colors.muted,\n        ...style,\n      }}\n    >\n      {Option.isSome(origin) && Option.isSome(chosenBox) ? (\n        <div\n          testId=\"segment-indicator\"\n          motion={{\n            initial: false,\n            animate: indicatorBox(origin.value, chosenBox.value),\n            transition: move,\n          }}\n          style={{\n            position: \"absolute\",\n            backgroundColor: chipFill,\n            borderRadius: theme.radius.sm,\n            boxShadow: elevationShadow(theme, \"sm\"),\n            pointerEvents: \"none\",\n          }}\n        />\n      ) : null}\n      {Arr.map(items, (item) => {\n        const chosen = item.value === value;\n        return (\n          <Action\n            key={item.value}\n            variant=\"ghost\"\n            size={size}\n            label={item.label}\n            disabled={item.disabled === true}\n            tabIndex={chosen ? 0 : -1}\n            testId={`segment-${item.value}`}\n            aria-checked={chosen}\n            ref={(instance: PublicInstance | null) => {\n              if (instance === null) segments.current.delete(item.value);\n              else segments.current.set(item.value, instance);\n            }}\n            onActivate={() => choose(item.value)}\n            onLayout={(event) =>\n              Option.map(measuredBox(event), (box) =>\n                setBoxes((current) => {\n                  const known = current.get(item.value);\n                  return known?.x === box.x &&\n                    known.y === box.y &&\n                    known.width === box.width &&\n                    known.height === box.height\n                    ? current\n                    : new Map(current).set(item.value, box);\n                }),\n              )\n            }\n            leading={\n              item.icon === undefined ? undefined : (\n                <Icon\n                  name={item.icon}\n                  size={theme.iconSize.sm}\n                  color={theme.colors.foreground}\n                />\n              )\n            }\n            style={{\n              flexGrow: 1,\n              flexBasis: 0,\n              borderRadius: theme.radius.sm,\n              ...(chosen && !measured\n                ? {\n                    backgroundColor: chipFill,\n                    boxShadow: elevationShadow(theme, \"sm\"),\n                  }\n                : { backgroundColor: \"transparent\" }),\n            }}\n          >\n            {item.label}\n          </Action>\n        );\n      })}\n    </div>\n  );\n};\n\nexport const segmentedControlMeta = {\n  name: \"Segmented control\",\n  tier: 2,\n  status: \"done\",\n  gpuixSupport: \"yes\",\n  limitations: [\n    \"No `aria-checked` channel is read by assistive technology in 0.11.0; the chosen segment is the group's one tab stop\",\n  ],\n  docs: \"docs/segmented-control.mdx\",\n  example: \"examples/segmented-control.example.tsx\",\n  testFile: \"src/behaviour/segmented-control.test.tsx\",\n} as const;\n"
    },
    {
      "path": "packages/components/src/tabs/indicator.ts",
      "target": "@ui/gpuixui/components/tabs/indicator.ts",
      "type": "registry:lib",
      "content": "import type { MeasuredBox } from \"../internal/measure.js\";\n\nexport type TabBox = MeasuredBox;\n\nexport interface IndicatorBox {\n  readonly left: number;\n  readonly top: number;\n  readonly width: number;\n  readonly height: number;\n}\n\nexport { measuredBox } from \"../internal/measure.js\";\n\nexport const indicatorBox = (\n  origin: TabBox,\n  selected: TabBox,\n): IndicatorBox => ({\n  left: selected.x - origin.x,\n  top: selected.y - origin.y,\n  width: selected.width,\n  height: selected.height,\n});\n\nexport const lineIndicatorBox = (\n  origin: TabBox,\n  selected: TabBox,\n  thickness: number,\n): IndicatorBox => ({\n  left: selected.x - origin.x,\n  top: selected.y - origin.y + selected.height - thickness,\n  width: selected.width,\n  height: thickness,\n});\n"
    }
  ],
  "docs": "[Segmented control](https://gpuixui.sudokw.com/components/segmented-control) — These are GPUix native components: the files render through GPUix's reconciler against `@gpuix/react`, not the DOM, and need the per-platform `@gpuix/native` binding. The CLI installs the `@gpuixui/*` runtime the components import as registry items, so the copied tree resolves on its own; wrap it in `ThemeProvider` before it renders.",
  "meta": {
    "url": "/components/segmented-control",
    "kind": "component",
    "runtime": "gpuix",
    "registry": "gpuixui"
  }
}