{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "segmented-control",
  "type": "registry:ui",
  "title": "SegmentedControl",
  "description": "SegmentedControl lets users pick one option from a compact horizontal group.",
  "dependencies": [
    "@radix-ui/react-compose-refs@^1.1.3",
    "@radix-ui/react-slot@^1.2.4",
    "@radix-ui/react-toggle-group@^1.1.11",
    "@remixicon/react@^4.9.0",
    "class-variance-authority@^0.7.1"
  ],
  "registryDependencies": [
    "https://wideset.co/r/motion.json",
    "https://wideset.co/r/styles.json",
    "https://wideset.co/r/utils.json"
  ],
  "files": [
    {
      "path": "src/components/segmented-control/SegmentedControl.context.ts",
      "type": "registry:ui",
      "target": "components/ui/wideset/segmented-control/SegmentedControl.context.ts",
      "content": "'use client';\n\nimport { createContext, useContext } from 'react';\n\nimport type { SegmentedControlContextValue } from './SegmentedControl.types';\n\nexport const SegmentedControlContext = createContext<SegmentedControlContextValue | null>(null);\n\nexport const useSegmentedControlContext = () => {\n  const context = useContext(SegmentedControlContext);\n\n  if (!context) {\n    throw new Error('useSegmentedControlContext must be used within a <SegmentedControl>');\n  }\n\n  return context;\n};\n"
    },
    {
      "path": "src/components/segmented-control/SegmentedControl.hooks.tsx",
      "type": "registry:ui",
      "target": "components/ui/wideset/segmented-control/SegmentedControl.hooks.tsx",
      "content": "'use client';\n\nimport { useLayoutEffect, useState } from 'react';\n\nexport type SelectionPill = {\n  x: number;\n  y: number;\n  width: number;\n  height: number;\n  visible: boolean;\n  animate: boolean;\n};\n\nconst isSamePill = (a: SelectionPill, b: SelectionPill) =>\n  (Object.keys(a) as (keyof SelectionPill)[]).every((key) => a[key] === b[key]);\n\nexport const useSelectionPill = (enabled: boolean) => {\n  const [node, setNode] = useState<HTMLDivElement | null>(null);\n  const [pill, setPill] = useState<SelectionPill | null>(null);\n\n  useLayoutEffect(() => {\n    if (!enabled || !node) return;\n\n    const controller = new AbortController();\n    const { signal } = controller;\n    let lastSelected: HTMLElement | null = null;\n    let fromKeyboard = false;\n\n    const resizeObserver = new ResizeObserver(() => measure());\n\n    const measure = () => {\n      node\n        .querySelectorAll(':scope > [role=\"radio\"]')\n        .forEach((item) => resizeObserver.observe(item));\n\n      const selected = node.querySelector<HTMLElement>(':scope > [data-state=\"on\"]');\n      if (!selected) {\n        setPill((previous) => previous && { ...previous, visible: false });\n        return;\n      }\n\n      const moved = lastSelected !== null && lastSelected !== selected;\n      lastSelected = selected;\n\n      const next: SelectionPill = {\n        x: selected.offsetLeft,\n        y: selected.offsetTop,\n        width: selected.offsetWidth,\n        height: selected.offsetHeight,\n        visible: !selected.hasAttribute('data-disabled'),\n        animate: moved && !fromKeyboard,\n      };\n      setPill((previous) => (previous && isSamePill(previous, next) ? previous : next));\n    };\n\n    const mutationObserver = new MutationObserver(measure);\n\n    measure();\n    resizeObserver.observe(node);\n    mutationObserver.observe(node, {\n      subtree: true,\n      childList: true,\n      attributes: true,\n      attributeFilter: ['data-state', 'data-disabled'],\n    });\n    node.addEventListener('keydown', () => (fromKeyboard = true), { signal });\n    node.addEventListener('pointerdown', () => (fromKeyboard = false), { signal });\n    document.fonts?.ready.then(() => !signal.aborted && measure());\n\n    return () => {\n      controller.abort();\n      resizeObserver.disconnect();\n      mutationObserver.disconnect();\n    };\n  }, [enabled, node]);\n\n  return { ref: setNode, pill };\n};\n"
    },
    {
      "path": "src/components/segmented-control/SegmentedControl.tsx",
      "type": "registry:ui",
      "target": "components/ui/wideset/segmented-control/SegmentedControl.tsx",
      "content": "'use client';\n\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport * as ToggleGroup from '@radix-ui/react-toggle-group';\nimport React, { useState } from 'react';\nimport { cn } from '@/lib/wideset/utils';\n\nimport { useDefaultMotion } from '@/lib/wideset/motion/MotionProvider';\nimport { SegmentedControlContext } from './SegmentedControl.context';\nimport { useSelectionPill } from './SegmentedControl.hooks';\nimport type {\n  SegmentedControlProps,\n  SegmentedControlWithSubComponents,\n} from './SegmentedControl.types';\nimport {\n  segmentedControlGlideVariants,\n  segmentedControlPillVariants,\n  segmentedControlVariants,\n} from './SegmentedControl.variants';\nimport { SegmentedControlIcon } from './SegmentedControlIcon';\nimport { SegmentedControlItem } from './SegmentedControlItem';\n\nconst SegmentedControl = React.forwardRef<HTMLDivElement, SegmentedControlProps>(\n  (\n    {\n      children,\n      size = 'md',\n      variant = 'regular',\n      className,\n      value,\n      defaultValue,\n      onValueChange,\n      disabled,\n      defaultMotion,\n      ...props\n    },\n    ref,\n  ) => {\n    const [internalValue, setInternalValue] = useState(defaultValue);\n    const motion = useDefaultMotion(defaultMotion);\n    const selection = useSelectionPill(motion);\n    const composedRef = useComposedRefs(ref, selection.ref);\n\n    const isControlled = value !== undefined;\n    const currentValue = isControlled ? value : internalValue;\n\n    const handleValueChange = (newValue: string) => {\n      if (!newValue) return;\n\n      if (!isControlled) {\n        setInternalValue(newValue);\n      }\n      onValueChange?.(newValue);\n    };\n\n    return (\n      <SegmentedControlContext.Provider value={{ size, variant, motion }}>\n        <ToggleGroup.Root\n          ref={composedRef}\n          className={cn(\n            segmentedControlVariants({ size, variant }),\n            segmentedControlGlideVariants({ enabled: motion }),\n            className,\n          )}\n          type=\"single\"\n          value={currentValue}\n          onValueChange={handleValueChange}\n          disabled={disabled}\n          data-slot=\"segmented-control\"\n          {...props}\n        >\n          {children}\n          {motion && selection.pill && (\n            <span\n              aria-hidden\n              className={segmentedControlPillVariants({ size, animate: selection.pill.animate })}\n              style={{\n                width: selection.pill.width,\n                height: selection.pill.height,\n                transform: `translate(${selection.pill.x}px, ${selection.pill.y}px)`,\n                opacity: selection.pill.visible ? 1 : 0,\n              }}\n            />\n          )}\n        </ToggleGroup.Root>\n      </SegmentedControlContext.Provider>\n    );\n  },\n) as SegmentedControlWithSubComponents;\n\nSegmentedControl.displayName = 'SegmentedControl';\nSegmentedControl.Item = SegmentedControlItem;\nSegmentedControl.Icon = SegmentedControlIcon;\n\nexport { SegmentedControl };\n"
    },
    {
      "path": "src/components/segmented-control/SegmentedControl.types.ts",
      "type": "registry:ui",
      "target": "components/ui/wideset/segmented-control/SegmentedControl.types.ts",
      "content": "import type * as ToggleGroup from '@radix-ui/react-toggle-group';\nimport type { RemixiconComponentType } from '@remixicon/react';\nimport type { VariantProps } from 'class-variance-authority';\nimport type {\n  ComponentPropsWithoutRef,\n  ForwardRefExoticComponent,\n  HTMLAttributes,\n  PropsWithoutRef,\n  ReactNode,\n  RefAttributes,\n} from 'react';\n\nimport type { MotionProps } from '@/lib/wideset/motion/Motion.types';\nimport type {\n  segmentedControlItemVariants,\n  segmentedControlVariants,\n} from './SegmentedControl.variants';\nimport type { SegmentedControlIcon } from './SegmentedControlIcon';\nimport type { SegmentedControlItem } from './SegmentedControlItem';\n\nexport type SegmentedControlSize = VariantProps<typeof segmentedControlVariants>['size'];\nexport type SegmentedControlVariant = VariantProps<typeof segmentedControlVariants>['variant'];\n\nexport type SegmentedControlContextValue = {\n  size: SegmentedControlSize;\n  variant: SegmentedControlVariant;\n  motion: boolean;\n};\n\n/** A group of mutually exclusive toggle options. */\nexport type SegmentedControlProps = {\n  /** Controls the height and padding of the control and its items. */\n  size?: SegmentedControlSize;\n  /** Layout variant — `regular` auto-sizes to content, `fullWidth` expands to fill the container. */\n  variant?: SegmentedControlVariant;\n  /** Controlled selected value. Pair with `onValueChange` to handle updates. */\n  value?: string;\n  /** Initial selected value for uncontrolled usage. */\n  defaultValue?: string;\n  /** Fired when the selected value changes. Receives the new value string. */\n  onValueChange?: (value: string) => void;\n  /** Accessible name of the group, e.g. `\"View mode\"`. Give the group a name with this or `aria-labelledby`. */\n  'aria-label'?: string;\n  /** ID of a visible element that names the group. Use instead of `aria-label` when a heading or label is on screen. */\n  'aria-labelledby'?: string;\n} & MotionProps &\n  Omit<\n    ComponentPropsWithoutRef<typeof ToggleGroup.Root>,\n    'type' | 'value' | 'defaultValue' | 'onValueChange'\n  >;\n\n/** An icon rendered within `SegmentedControl`. */\nexport type SegmentedControlIconProps = {\n  /** Icon component from `@remixicon/react` to render inside the item. */\n  icon: RemixiconComponentType;\n} & HTMLAttributes<HTMLDivElement>;\n\n/**\n * Icon-only items (icon set, no children) must provide an accessible name. An item whose only\n * child is `SegmentedControl.Icon` is icon-only too and needs an `aria-label` the same way; the\n * type cannot tell it apart from a label, so the item checks it at runtime and warns.\n *\n * For the square icon-only padding, `SegmentedControl.Icon` must be a direct child of the item\n * (a Fragment around it is fine). An icon rendered by your own wrapper component keeps the label\n * padding; it still warns when the item has no accessible name.\n */\ntype SegmentedControlItemIconProps =\n  | {\n      /** Optional icon rendered before children. Use `SegmentedControl.Icon` as a child for icon + label combinations. */\n      icon?: undefined;\n    }\n  | {\n      /** Icon component from `@remixicon/react`. */\n      icon: RemixiconComponentType;\n      /** Content rendered inside the component. */\n      children: ReactNode;\n    }\n  | {\n      /** Icon component from `@remixicon/react`. */\n      icon: RemixiconComponentType;\n      children?: never;\n      /** Accessible name for an icon-only item. */\n      'aria-label': string;\n    }\n  | {\n      /** Icon component from `@remixicon/react`. */\n      icon: RemixiconComponentType;\n      children?: never;\n      /** ID of the element providing the accessible name for an icon-only item. */\n      'aria-labelledby': string;\n    };\n\n/** An individual item within `SegmentedControl`. */\nexport type SegmentedControlItemProps = ComponentPropsWithoutRef<typeof ToggleGroup.Item> &\n  Omit<VariantProps<typeof segmentedControlItemVariants>, 'iconOnly'> &\n  SegmentedControlItemIconProps;\n\nexport type SegmentedControlWithSubComponents = ForwardRefExoticComponent<\n  PropsWithoutRef<SegmentedControlProps> & RefAttributes<HTMLDivElement>\n> & {\n  Item: typeof SegmentedControlItem;\n  Icon: typeof SegmentedControlIcon;\n};\n"
    },
    {
      "path": "src/components/segmented-control/SegmentedControl.variants.ts",
      "type": "registry:ui",
      "target": "components/ui/wideset/segmented-control/SegmentedControl.variants.ts",
      "content": "import { cva } from 'class-variance-authority';\nimport { cn } from '@/lib/wideset/utils';\n\nimport { hoverTintMotion } from '@/lib/wideset/motion/recipes';\n\nexport const segmentedControlVariants = cva('bg-ws-static-alpha-base p-ws-2', {\n  variants: {\n    variant: {\n      regular: 'inline-flex w-fit',\n      fullWidth: 'flex w-full',\n    },\n    size: {\n      sm: 'rounded-ws-control-lg h-ws-forms-sm gap-ws-2',\n      md: 'rounded-ws-control-lg h-ws-forms-md gap-ws-2',\n      lg: 'rounded-ws-control-xl h-ws-forms-lg gap-ws-3',\n    },\n  },\n  defaultVariants: {\n    variant: 'regular',\n    size: 'md',\n  },\n});\n\nexport const segmentedControlItemVariants = cva(\n  cn(\n    'text-ws-dynamic-secondary-default flex cursor-pointer items-center justify-center',\n    'focus-visible:wds-focus-visible outline-ws-dynamic-primary-strong-default',\n    'hover:bg-ws-dynamic-secondary-alpha-hover active:bg-ws-dynamic-secondary-alpha-pressed',\n    'data-[state=on]:bg-ws-static-highlight-strong data-[state=on]:shadow-ws-low',\n    'data-[state=on]:hover:bg-ws-static-highlight-strong data-[state=on]:active:bg-ws-static-highlight-strong',\n    'disabled:text-ws-dynamic-secondary-disabled disabled:pointer-events-none',\n    'disabled:bg-transparent disabled:shadow-none',\n    'disabled:data-[state=on]:bg-transparent disabled:data-[state=on]:shadow-none',\n    'data-disabled:bg-transparent data-disabled:shadow-none',\n    'data-disabled:data-[state=on]:bg-transparent data-disabled:data-[state=on]:shadow-none',\n    'disabled:hover:bg-transparent disabled:active:bg-transparent',\n    'disabled:data-[state=on]:hover:bg-transparent disabled:data-[state=on]:active:bg-transparent',\n  ),\n  {\n    variants: {\n      size: {\n        sm: 'typography-label-strong-sm rounded-ws-control-md min-w-ws-13 gap-ws-5 px-ws-7 py-ws-3',\n        md: 'typography-label-strong-sm rounded-ws-control-md min-w-ws-15 gap-ws-5 px-ws-9 py-ws-4',\n        lg: 'typography-label-strong-md rounded-ws-control-lg min-w-ws-17 gap-ws-5 px-ws-11 py-ws-7',\n      },\n      variant: {\n        regular: '',\n        fullWidth: 'flex-1',\n      },\n      iconOnly: {\n        true: '',\n        false: '',\n      },\n    },\n    compoundVariants: [\n      { size: 'sm', iconOnly: true, class: 'p-ws-4' },\n      { size: 'md', iconOnly: true, class: 'p-ws-6' },\n      { size: 'lg', iconOnly: true, class: 'p-ws-8' },\n    ],\n    defaultVariants: {\n      size: 'md',\n      variant: 'regular',\n      iconOnly: false,\n    },\n  },\n);\n\nexport const segmentedControlItemMotionVariants = cva('', {\n  variants: {\n    enabled: { true: hoverTintMotion, false: '' },\n  },\n  defaultVariants: { enabled: true },\n});\n\nexport const segmentedControlGlideVariants = cva('', {\n  variants: {\n    enabled: {\n      true: 'relative isolate *:data-[state=on]:bg-transparent! *:data-[state=on]:shadow-none!',\n      false: '',\n    },\n  },\n  defaultVariants: { enabled: true },\n});\n\nexport const segmentedControlPillVariants = cva(\n  'bg-ws-static-highlight-base shadow-ws-low pointer-events-none absolute top-0 left-0 -z-[var(--ws-z-index-base)]',\n  {\n    variants: {\n      size: {\n        sm: 'rounded-ws-control-md',\n        md: 'rounded-ws-control-md',\n        lg: 'rounded-ws-control-lg',\n      },\n      animate: {\n        true: 'transition-[transform,width,height,opacity] duration-(--ws-motion-duration-fast) ease-(--ws-motion-ease-smooth-out)',\n        false: '',\n      },\n    },\n    defaultVariants: { size: 'md', animate: false },\n  },\n);\n"
    },
    {
      "path": "src/components/segmented-control/SegmentedControlIcon.tsx",
      "type": "registry:ui",
      "target": "components/ui/wideset/segmented-control/SegmentedControlIcon.tsx",
      "content": "'use client';\n\nimport { Slot } from '@radix-ui/react-slot';\nimport { cn } from '@/lib/wideset/utils';\n\nimport type { SegmentedControlIconProps } from './SegmentedControl.types';\n\nconst SegmentedControlIcon = ({ icon: Icon, className, ...props }: SegmentedControlIconProps) => {\n  return (\n    <Slot\n      data-slot=\"segmented-control-icon\"\n      aria-hidden=\"true\"\n      className={cn('size-ws-9 shrink-0', className)}\n      {...props}\n    >\n      <Icon focusable=\"false\" />\n    </Slot>\n  );\n};\n\nSegmentedControlIcon.displayName = 'SegmentedControl.Icon';\n\nexport { SegmentedControlIcon };\n"
    },
    {
      "path": "src/components/segmented-control/SegmentedControlItem.tsx",
      "type": "registry:ui",
      "target": "components/ui/wideset/segmented-control/SegmentedControlItem.tsx",
      "content": "'use client';\n\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport * as ToggleGroup from '@radix-ui/react-toggle-group';\nimport React, { Children, Fragment, isValidElement, useEffect, useRef } from 'react';\nimport { cn } from '@/lib/wideset/utils';\n\nimport { useSegmentedControlContext } from './SegmentedControl.context';\nimport type { SegmentedControlItemProps } from './SegmentedControl.types';\nimport {\n  segmentedControlItemMotionVariants,\n  segmentedControlItemVariants,\n} from './SegmentedControl.variants';\nimport { SegmentedControlIcon } from './SegmentedControlIcon';\n\nconst isIconChild = (child: React.ReactNode) =>\n  isValidElement(child) && child.type === SegmentedControlIcon;\n\nconst flattenFragments = (children: React.ReactNode): React.ReactNode[] =>\n  Children.toArray(children).flatMap((child) =>\n    isValidElement<{ children?: React.ReactNode }>(child) && child.type === Fragment\n      ? flattenFragments(child.props.children)\n      : [child],\n  );\n\n// Elements that can name the item without text, e.g. `<img alt=\"Map\" />`.\nconst NAMED_CONTENT_SELECTOR = 'img[alt], [aria-label]';\n\nconst hasOwnName = (element: Element) =>\n  Boolean(\n    element.getAttribute('aria-label')?.trim() ||\n    (element.matches('img') && element.getAttribute('alt')?.trim()),\n  );\n\n// Attributes only: reading computed styles here would force a style recalculation on every render,\n// so content hidden by CSS alone still counts.\n// Stops at the item: an aria-hidden ancestor outside it (e.g. behind an open modal) says nothing\n// about whether the item's content names it.\nconst isHiddenWithin = (element: Element | null, item: Element) => {\n  for (let node = element; node && node !== item; node = node.parentElement) {\n    if (node.getAttribute('aria-hidden') === 'true' || node.hasAttribute('hidden')) return true;\n  }\n  return false;\n};\n\nconst hasVisibleName = (item: Element) => {\n  const walker = item.ownerDocument.createTreeWalker(item, NodeFilter.SHOW_TEXT);\n  for (let node = walker.nextNode(); node; node = walker.nextNode()) {\n    if (node.textContent?.trim() && !isHiddenWithin(node.parentElement, item)) return true;\n  }\n  return Array.from(item.querySelectorAll(NAMED_CONTENT_SELECTOR)).some(\n    (element) => hasOwnName(element) && !isHiddenWithin(element, item),\n  );\n};\n\n// A directly referenced element supplies its name even when it is aria-hidden itself, so only\n// what is hidden inside it is skipped.\nconst hasReferencedName = (item: HTMLElement, id: string) => {\n  const label = item.ownerDocument.getElementById(id);\n  return Boolean(label && (hasOwnName(label) || hasVisibleName(label)));\n};\n\nconst SegmentedControlItem = React.forwardRef<HTMLButtonElement, SegmentedControlItemProps>(\n  ({ className, children, icon: Icon, ...props }, ref) => {\n    const { size, variant, motion } = useSegmentedControlContext();\n    // Children.toArray drops null/undefined/booleans (e.g. `{showLabel && 'Map'}` when false),\n    // but keeps empty/whitespace-only strings — those don't count as a label either.\n    // A `SegmentedControl.Icon` child is an icon, not a label.\n    const childArray = flattenFragments(children);\n    const hasIconChild = childArray.some(isIconChild);\n    const hasLabel = childArray.some((child) =>\n      typeof child === 'string' ? child.trim() !== '' : !isIconChild(child),\n    );\n    const iconOnly = !hasLabel && (Boolean(Icon) || hasIconChild);\n    const ariaLabel = props['aria-label'];\n    const ariaLabelledby = props['aria-labelledby'];\n    const itemRef = useRef<HTMLButtonElement>(null);\n    const composedRef = useComposedRefs(ref, itemRef);\n    const warned = useRef(false);\n\n    // Reads the rendered item, so a wrapper component around the icon is caught too. Runs after\n    // every render because the content can change; `warned` keeps it to one warning, StrictMode's\n    // repeated effects included.\n    useEffect(() => {\n      const item = itemRef.current;\n      if (!item) return;\n\n      const unnamed =\n        !ariaLabel?.trim() &&\n        !ariaLabelledby?.split(/\\s+/).some((id) => hasReferencedName(item, id)) &&\n        !hasVisibleName(item);\n\n      if (unnamed && !warned.current) {\n        // eslint-disable-next-line no-console\n        console.warn(\n          'SegmentedControl.Item: icon-only items require a non-empty `aria-label` or `aria-labelledby` to have an accessible name.',\n        );\n      }\n      warned.current = unnamed;\n    });\n\n    return (\n      <ToggleGroup.Item\n        ref={composedRef}\n        className={cn(\n          segmentedControlItemVariants({ size, variant, iconOnly }),\n          segmentedControlItemMotionVariants({ enabled: motion }),\n          className,\n        )}\n        data-slot=\"segmented-control-item\"\n        {...props}\n      >\n        {Icon && (\n          <Icon\n            data-slot=\"segmented-control-icon\"\n            aria-hidden=\"true\"\n            focusable=\"false\"\n            className=\"size-ws-9 shrink-0\"\n          />\n        )}\n        {children}\n      </ToggleGroup.Item>\n    );\n  },\n);\n\nSegmentedControlItem.displayName = 'SegmentedControl.Item';\n\nexport { SegmentedControlItem };\n"
    }
  ],
  "meta": {
    "version": "1.0.0",
    "commit": "8bb3627"
  }
}
