{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tw-content-switcher",
  "type": "registry:ui",
  "title": "react-tw/content-switcher.tsx",
  "description": "Carbon `ContentSwitcher` / `Switch` / `IconSwitch` as React + Tailwind components, the look and Copied from @purpurabog/ui 2.3.0; the package is the maintained source, this copy is yours to change. Icons from the Carbon Design System (Apache-2.0, © IBM Corp.; see THIRD-PARTY-NOTICES.md in the package).",
  "dependencies": [
    "@purpurabog/ui@^2.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-position",
    "@kanok-ui/tw-theme",
    "@kanok-ui/tw-tooltip",
    "@kanok-ui/tw-utils"
  ],
  "files": [
    {
      "path": "src/react-tw/content-switcher.tsx",
      "type": "registry:ui",
      "target": "components/ui/react-tw/content-switcher.tsx",
      "content": "import { Children, createContext, isValidElement, useContext, useMemo, useState, type CSSProperties, type ReactElement, type ReactNode, type ComponentProps } from 'react';\nimport type { Placement } from '../core/position.ts';\nimport { ContentSwitcher as ContentSwitcherPrimitive } from '@purpurabog/ui/headless';\nimport { Tooltip } from './tooltip.tsx';\nimport { cn } from './lib/utils.ts';\n\n/**\n * Carbon `ContentSwitcher` / `Switch` / `IconSwitch` as React + Tailwind components, the look and\n * rules of `<ui-content-switcher>` (core/roving): a `tablist` where exactly one item is selected.\n * Click, or Left / Right (wrapping, skipping disabled items) select at once; `selectionMode=\"manual\"`\n * moves focus only and Enter / Space selects; Home / End jump to the ends. Sizes sm 32 / md 40 /\n * lg 48px, full width with items shared evenly; when every item is `iconOnly` the track shrinks to\n * fit and each item shows its `label` as a tooltip. `lowContrast` is the grey variant.\n */\nexport type ContentSwitcherSize = 'sm' | 'md' | 'lg';\n\nexport type ContentSwitcherChange = ContentSwitcherPrimitive.Change;\n\n/** What the styled items need to look right; the state (selection, Tab stop, keys) is the primitive's. */\ninterface SwitcherLook {\n    size: ContentSwitcherSize;\n    lowContrast: boolean;\n    /** The hovered and the focused item: the divider after each hides. */\n    hovered: number | null;\n    focused: number | null;\n    setHovered: (index: number | null) => void;\n    setFocused: (index: number | null) => void;\n}\n\nconst SwitcherContext = createContext<SwitcherLook | null>(null);\n\nexport interface ContentSwitcherProps extends Omit<ComponentProps<'div'>, 'onChange' | 'children'> {\n    selectedIndex?: number;\n    defaultSelectedIndex?: number;\n    onChange?: (change: ContentSwitcherChange) => void;\n    size?: ContentSwitcherSize;\n    selectionMode?: 'automatic' | 'manual';\n    lowContrast?: boolean;\n    /** Names the group. */\n    'aria-label'?: string;\n    'aria-labelledby'?: string;\n    className?: string;\n    /** `<ContentSwitcherItem>`s. */\n    children: ReactNode;\n}\n\nconst LOW_CONTRAST = {\n    '--_border': 'var(--ui-hairline-strong)',\n    '--_divider': 'var(--ui-hairline-strong)',\n    '--_hover': 'var(--ui-content-switcher-background-hover)',\n    '--_selected': 'var(--ui-content-switcher-selected)',\n    '--_selected-ink': 'var(--ui-ink)',\n    '--_selected-icon': 'var(--ui-icon-primary)',\n} as CSSProperties;\n\nexport function ContentSwitcher({ selectedIndex, defaultSelectedIndex = 0, onChange, size = 'md', selectionMode = 'automatic', lowContrast = false, className, children, ...rest }: Readonly<ContentSwitcherProps>) {\n    const [hovered, setHovered] = useState<number | null>(null);\n    const [focused, setFocused] = useState<number | null>(null);\n    const { anyDisabled, iconOnly } = useMemo(() => {\n        const items = Children.toArray(children).filter(isValidElement) as ReactElement<ContentSwitcherItemProps>[];\n        return { anyDisabled: items.some((item) => !!item.props.disabled), iconOnly: items.length > 0 && items.every((item) => item.props.iconOnly) };\n    }, [children]);\n    const look = useMemo<SwitcherLook>(() => ({ size, lowContrast, hovered, focused, setHovered, setFocused }), [size, lowContrast, hovered, focused]);\n\n    return (\n        <SwitcherContext.Provider value={look}>\n            <ContentSwitcherPrimitive.Root\n                {...rest}\n                selectedIndex={selectedIndex}\n                defaultSelectedIndex={defaultSelectedIndex}\n                onChange={onChange}\n                selectionMode={selectionMode}\n                style={lowContrast ? { ...rest.style, ...LOW_CONTRAST } : rest.style}\n                className={cn(\n                    // --ui-content-switcher-track / -radius / -padding / -outline: a design can make it the contained tabs' segmented track\n                    'box-border flex w-full justify-evenly p-[var(--ui-content-switcher-padding,0px)] rounded-[var(--ui-content-switcher-radius,4px)] bg-[var(--ui-content-switcher-track,transparent)] font-sans outline-1 outline-solid -outline-offset-1 outline-[var(--ui-content-switcher-outline,var(--ui-border-inverse))]',\n                    { sm: 'h-field-sm', md: 'h-field-md', lg: 'h-field-lg' }[size],\n                    // Carbon: the track greys while any item is disabled\n                    anyDisabled && 'outline-border-disabled',\n                    iconOnly && 'w-fit justify-start',\n                    lowContrast && 'bg-content-switcher-background outline-hairline-strong',\n                    className,\n                )}\n            >\n                {children}\n            </ContentSwitcherPrimitive.Root>\n        </SwitcherContext.Provider>\n    );\n}\n\nexport interface ContentSwitcherItemProps extends Omit<ComponentProps<'div'>, 'name' | 'label' | 'disabled' | 'iconOnly' | 'icon' | 'tooltipPlacement' | 'children' | 'className'> {\n    /** Identifies the item in `onChange` (Carbon `name`). */\n    name?: string;\n    /** Accessible name — required with `iconOnly`, and its tooltip. */\n    label?: string;\n    disabled?: boolean;\n    iconOnly?: boolean;\n    /** A 16px icon (20px in `lg`) before the text, or alone with `iconOnly`. */\n    icon?: ReactNode;\n    /** Where the `iconOnly` tooltip shows (default top). */\n    tooltipPlacement?: Placement;\n    children?: ReactNode;\n    className?: string;\n}\n\ninterface ItemLook {\n    selected: boolean;\n    disabled: boolean;\n    first: boolean;\n    last: boolean;\n    size: ContentSwitcherSize;\n    lowContrast: boolean;\n    iconOnly: boolean;\n    /** The divider before this item hides (see ContentSwitcherItem). */\n    hideDivider: boolean;\n}\n\nconst ICON_ONLY_WIDTH = { sm: 'w-field-sm', md: 'w-field-md', lg: 'w-field-lg' } as const;\n\n/** The tab: text colours (the selected and the disabled ones outrank hover, as later rules in <ui-content-switcher-item>), rounded ends. */\nfunction itemClasses({ selected, disabled, first, last, size, lowContrast, iconOnly }: ItemLook, className?: string) {\n    return cn(\n        // --ui-content-switcher-item-size / -grow: a design can size items to their labels and let them grow\n        'group/item relative box-border inline-flex w-[var(--ui-content-switcher-item-size,100%)] grow-[var(--ui-content-switcher-item-grow,0)] cursor-pointer select-none overflow-hidden whitespace-nowrap text-start text-ink-muted type-body-sm leading-[18px]! outline-none',\n        first && 'rounded-s-[var(--ui-content-switcher-item-radius,4px)]',\n        last && 'rounded-e-[var(--ui-content-switcher-item-radius,4px)]',\n        'hover:z-3 active:z-3',\n        !selected && !disabled && 'hover:text-ink active:text-ink',\n        selected && 'z-3 text-(--_selected-ink,var(--ui-content-switcher-selected-ink,var(--ui-inverse-ink)))',\n        disabled && 'cursor-not-allowed text-ink-disabled',\n        lowContrast && selected && 'font-semibold!', // over the type-body-sm font shorthand\n        lowContrast && selected && disabled && 'rounded-[4px]',\n        iconOnly && ICON_ONLY_WIDTH[size],\n        'forced-colors:focus-visible:outline-[length:var(--ui-focus-width,2px)] forced-colors:focus-visible:outline-solid forced-colors:focus-visible:-outline-offset-2 forced-colors:focus-visible:outline-[CanvasText]',\n        className,\n    );\n}\n\n/** The selection fill (Carbon ::after): grows in when selected; keyboard focus rings it. */\nfunction fillClasses({ selected, lowContrast }: ItemLook) {\n    return cn(\n        \"after:absolute after:start-0 after:top-0 after:block after:size-full after:origin-bottom after:rounded-[var(--ui-content-switcher-item-radius,4px)] after:bg-(--_selected,var(--ui-content-switcher-selected-fill,var(--ui-layer-selected-inverse))) after:[transform:scaleY(0)] after:content-['']\",\n        selected && 'after:shadow-[var(--ui-content-switcher-selected-shadow,none)] after:[transform:scaleY(1)] forced-colors:after:bg-[Highlight]',\n        'group-focus-visible/item:after:shadow-[inset_0_0_0_var(--ui-focus-width,2px)_var(--ui-focus),inset_0_0_0_calc(var(--ui-focus-width,2px)+var(--ui-focus-inset-width,1px))_var(--ui-focus-inset)]',\n        !selected &&\n            'group-focus-visible/item:after:bg-focus-inset group-focus-visible/item:after:[transform:scaleY(1)] group-hover/item:group-focus-visible/item:after:rounded-none group-hover/item:group-focus-visible/item:after:bg-(--_hover,var(--ui-ghost-hover)) group-hover/item:group-focus-visible/item:after:shadow-none',\n        lowContrast && selected && 'after:shadow-[inset_0_0_0_1px_var(--ui-border-inverse)] group-focus-visible/item:after:shadow-[inset_0_0_0_var(--ui-focus-width,2px)_var(--ui-focus)]',\n        lowContrast && !selected && 'group-focus-visible/item:after:bg-content-switcher-background',\n    );\n}\n\n/** The 1px divider between items (Carbon ::before); keyboard focus turns it into the focus ring. */\nfunction dividerClasses({ selected, disabled, hideDivider }: ItemLook) {\n    let hiding = 'group-hover/item:before:bg-transparent group-focus-visible/item:before:bg-transparent';\n    if (hideDivider) hiding = 'before:bg-transparent';\n    return cn(\n        \"before:absolute before:start-0 before:z-2 before:block before:h-4 before:w-px before:bg-(--_divider,var(--ui-content-switcher-divider,var(--ui-hairline))) before:content-['']\",\n        !disabled && hiding,\n        !selected && 'group-focus-visible/item:before:size-full group-focus-visible/item:before:rounded-[var(--ui-content-switcher-item-radius,4px)] group-focus-visible/item:before:shadow-[inset_0_0_0_var(--ui-focus-width,2px)_var(--ui-focus)]',\n    );\n}\n\n/** Disabled: grey borders, no fill; its divider stays whatever its neighbours do (the disabled rule comes last). */\nfunction disabledClasses({ selected, disabled, first, last, lowContrast }: ItemLook) {\n    if (!disabled) return '';\n    return cn(\n        'border-y-border-disabled bg-transparent after:hidden before:bg-border-disabled',\n        (first || last) && 'shadow-none',\n        selected && 'bg-button-disabled',\n        selected && lowContrast && 'border border-solid border-border-disabled bg-content-switcher-selected',\n    );\n}\n\n/** The item's box inside the tab: the track's top and bottom borders, its end shadows, the hover fill, then the layers above. */\nfunction baseClasses(look: ItemLook) {\n    const { selected, disabled, first, last, size, iconOnly } = look;\n    return cn(\n        'relative box-border flex min-w-0 flex-1 items-center rounded-[inherit] border-0 border-y border-solid border-y-(--_border,var(--ui-content-switcher-border,var(--ui-border-inverse))) bg-transparent px-4 py-2',\n        size === 'lg' && 'px-[14px]',\n        first && 'shadow-[inset_1px_0_0_0_var(--_border,var(--ui-content-switcher-border,var(--ui-border-inverse)))]',\n        last && 'shadow-[inset_-1px_0_0_0_var(--_border,var(--ui-content-switcher-border,var(--ui-border-inverse)))]',\n        !selected && !disabled && 'group-hover/item:bg-(--_hover,var(--ui-ghost-hover)) group-active/item:bg-(--_hover,var(--ui-ghost-hover))',\n        selected && 'border-0',\n        iconOnly && 'justify-center p-0',\n        fillClasses(look),\n        dividerClasses(look),\n        disabledClasses(look),\n    );\n}\n\n/** The label and icon; the icon follows the item's text colour. */\nfunction labelClasses({ selected, disabled, size, iconOnly }: ItemLook) {\n    return cn(\n        'relative z-1 inline-flex max-w-full items-center gap-2 overflow-hidden text-ellipsis whitespace-nowrap',\n        '[&>svg]:shrink-0',\n        size === 'lg' ? '[&>svg]:size-5' : '[&>svg]:size-4',\n        iconOnly ? '[&>svg]:fill-icon-primary' : '[&>svg]:fill-icon-secondary group-hover/item:[&>svg]:fill-icon-primary group-focus-visible/item:[&>svg]:fill-icon-primary',\n        selected && '[&>svg]:fill-(--_selected-icon,var(--ui-icon-inverse)) group-hover/item:[&>svg]:fill-(--_selected-icon,var(--ui-icon-inverse))',\n        disabled && '[&>svg]:fill-icon-disabled group-hover/item:[&>svg]:fill-icon-disabled',\n    );\n}\n\nexport function ContentSwitcherItem({ label, disabled = false, iconOnly = false, icon, tooltipPlacement = 'top', children, className, ...rest }: Readonly<ContentSwitcherItemProps>) {\n    const switcher = useContext(SwitcherContext);\n    const state = ContentSwitcherPrimitive.useContentSwitcherContext('<ContentSwitcherItem>');\n    const index = ContentSwitcherPrimitive.useItemIndex();\n    if (!switcher) throw new Error('<ContentSwitcherItem> goes inside <ContentSwitcher>');\n    const selected = state.selected === index;\n    const first = index === 0;\n    const last = index === state.count - 1;\n    const { size, lowContrast } = switcher;\n    // the divider (::before) hides on the first item, next to the item before it being hovered / focused / selected, and on this one when it is\n    const before = index - 1;\n    const hideDivider = first || selected || before === state.selected || before === switcher.hovered || before === switcher.focused;\n    const look: ItemLook = { selected, disabled, first, last, size, lowContrast, iconOnly, hideDivider };\n\n    const item = (\n        <ContentSwitcherPrimitive.Item {...rest}\n            disabled={disabled}\n            aria-label={iconOnly && label ? label : undefined}\n            onPointerOver={(event) => {\n                rest.onPointerOver?.(event);\n                switcher.setHovered(index);\n            }}\n            onPointerOut={(event) => {\n                rest.onPointerOut?.(event);\n                switcher.setHovered(null);\n            }}\n            onFocus={(event) => {\n                rest.onFocus?.(event);\n                switcher.setFocused(index);\n            }}\n            onBlur={(event) => {\n                rest.onBlur?.(event);\n                switcher.setFocused(null);\n            }}\n            className={itemClasses(look, className)}\n        >\n            <span\n                className={baseClasses(look)}\n            >\n                <span\n                    className={labelClasses(look)}\n                >\n                    {icon}\n                    {children}\n                </span>\n            </span>\n        </ContentSwitcherPrimitive.Item>\n    );\n    if (!iconOnly || !label) return item;\n    return (\n        <Tooltip text={label} asLabel placement={tooltipPlacement} className=\"inline-flex\">\n            {item}\n        </Tooltip>\n    );\n}\n"
    }
  ]
}
