{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "content-switcher",
  "type": "registry:ui",
  "title": "<ui-content-switcher>",
  "description": "Carbon `ContentSwitcher`: a `tablist` of `<ui-content-switcher-item>`s where exactly one is selected. Click, or Left/Right (wrapping, skipping disabled items) select immediately; `selection-mode=\"manual\"` moves focus only and Enter / Space selects. Home / End jump to the first / last item (APG). Sizes sm 32 / md 40 / lg 48px, full width with items shared evenly; `icon-only` (every item is `icon-only`) shrinks to fit. `low-contrast` renders the grey variant. Set `aria-label` on the host to name the group. 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": [
    "lit@^3.3.0"
  ],
  "registryDependencies": [
    "@kanok-ui/core-roving",
    "@kanok-ui/shared-element",
    "@kanok-ui/shared-field",
    "@kanok-ui/tooltip",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/content-switcher/content-switcher-item.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/content-switcher/content-switcher-item.ts",
      "content": "import { css, html } from 'lit';\nimport { customElement, property } from 'lit/decorators.js';\nimport { setManagedAttribute } from '../shared/field.ts';\nimport { UiElement } from '../shared/element.ts';\nimport type { TooltipPlacement } from '../tooltip/tooltip.ts';\nimport '../tooltip/tooltip.ts';\n\n/**\n * One option of a `<ui-content-switcher>` (Carbon `Switch` / `IconSwitch`). The host is the\n * `tab`: role, `aria-selected`, roving `tabindex` and the selection fill all live on it, so the\n * switcher's `tablist` has real tabs as children.\n *\n * With `icon-only`, `label` is also shown as an icon tooltip (Carbon `IconSwitch`): on hover after\n * 100ms and on keyboard focus (arrow keys included); a click, Enter / Space or Esc closes it.\n *\n * @slot - Label text\n * @slot icon - A 16px icon (20px in `size=\"lg\"`) before the text, or alone with `icon-only` + `label`\n * @csspart label - The label wrapper\n * @csspart tooltip - The `<ui-tooltip>` of an `icon-only` item\n * @cssprop --ui-content-switcher-item-radius / --ui-content-switcher-item-size / --ui-content-switcher-item-grow - Corner radius, `inline-size` and `flex-grow` (default 4px, 100%, 0)\n * @cssprop --ui-content-switcher-divider / --ui-content-switcher-selected-shadow - The rule between items and the selected one's shadow (default hairline, none)\n */\n@customElement('ui-content-switcher-item')\nexport class UiContentSwitcherItem extends UiElement {\n    static override readonly styles = css`\n        :host {\n            position: relative;\n            display: inline-flex;\n            box-sizing: border-box;\n            flex-grow: var(--ui-content-switcher-item-grow, 0);\n            inline-size: var(--ui-content-switcher-item-size, 100%); /* a design can size items to their labels (auto) and let them grow */\n            overflow: hidden;\n            color: var(--ui-ink-muted);\n            font: var(--ui-font-body-sm);\n            line-height: 18px; /* body-compact-01 */\n            letter-spacing: var(--ui-tracking-body);\n            white-space: nowrap;\n            text-align: start;\n            cursor: pointer;\n            outline: none;\n            user-select: none;\n        }\n\n        /* the colours (low contrast sets its own below): --ui-content-switcher-* let a design restyle the default kind */\n        :host {\n            --_border: var(--ui-content-switcher-border, var(--ui-border-inverse));\n            --_selected: var(--ui-content-switcher-selected-fill, var(--ui-layer-selected-inverse));\n            --_selected-ink: var(--ui-content-switcher-selected-ink, var(--ui-inverse-ink));\n            --_divider: var(--ui-content-switcher-divider, var(--ui-hairline));\n            --_radius: var(--ui-content-switcher-item-radius, 4px);\n        }\n\n        /*\n         * The item's box — padding, borders, the divider (::before) and the selection fill (::after) — is\n         * drawn by this part filling the host, not by :host, so a page-wide CSS reset can't remove it.\n         */\n        .base {\n            position: relative;\n            display: flex;\n            flex: 1;\n            align-items: center;\n            box-sizing: border-box;\n            min-inline-size: 0;\n            padding: var(--ui-space-xs) var(--ui-space-md);\n            border-block: 1px solid var(--_border, var(--ui-border-inverse));\n            border-radius: inherit;\n            background-color: transparent;\n        }\n\n        :host([data-size='lg']) .base {\n            padding-inline: 14px;\n        }\n\n        :host(:first-of-type) {\n            border-start-start-radius: var(--_radius);\n            border-end-start-radius: var(--_radius);\n        }\n\n        :host(:first-of-type) .base {\n            box-shadow: inset 1px 0 0 0 var(--_border, var(--ui-border-inverse));\n        }\n\n        :host(:last-of-type) {\n            border-start-end-radius: var(--_radius);\n            border-end-end-radius: var(--_radius);\n        }\n\n        :host(:last-of-type) .base {\n            box-shadow: inset -1px 0 0 0 var(--_border, var(--ui-border-inverse));\n        }\n\n        /* selection fill (Carbon ::after) */\n        .base::after {\n            content: '';\n            position: absolute;\n            inset-block-start: 0;\n            inset-inline-start: 0;\n            display: block;\n            inline-size: 100%;\n            block-size: 100%;\n            border-radius: var(--_radius);\n            background-color: var(--_selected, var(--ui-layer-selected-inverse));\n            transform: scaleY(0);\n            transform-origin: bottom;\n        }\n\n        /* divider between items (Carbon ::before): hidden on the first item, on hovered/focused/selected items and on the item after them */\n        .base::before {\n            content: '';\n            position: absolute;\n            z-index: 2;\n            inset-inline-start: 0;\n            display: block;\n            inline-size: 1px;\n            block-size: 16px;\n            background-color: var(--_divider, var(--ui-hairline));\n        }\n\n        :host(:first-of-type) .base::before,\n        :host(:hover) .base::before,\n        :host(:focus-visible) .base::before,\n        :host([selected]) .base::before,\n        :host([data-no-divider]) .base::before {\n            background-color: transparent;\n        }\n\n        :host(:hover),\n        :host(:active) {\n            z-index: 3;\n            color: var(--ui-ink);\n        }\n\n        :host(:hover) .base,\n        :host(:active) .base {\n            background-color: var(--_hover, var(--ui-ghost-hover));\n        }\n\n        :host([selected]) {\n            z-index: 3;\n            color: var(--_selected-ink, var(--ui-inverse-ink));\n        }\n\n        :host([selected]) .base {\n            border: 0;\n        }\n\n        :host([selected]:hover) .base {\n            background-color: transparent;\n        }\n\n        :host([selected]) .base::after {\n            box-shadow: var(--ui-content-switcher-selected-shadow, none); /* a design can raise the chosen item as a chip */\n            transform: scaleY(1);\n        }\n\n        /* the ring and Carbon's 1px inner line (--ui-focus-inset-width: a design can drop the line, Kanok does) */\n        :host(:focus-visible) .base::after {\n            box-shadow:\n                inset 0 0 0 var(--ui-focus-width, 2px) var(--ui-focus),\n                inset 0 0 0 calc(var(--ui-focus-width, 2px) + var(--ui-focus-inset-width, 1px)) var(--ui-focus-inset);\n        }\n\n        :host(:focus-visible:not([selected])) .base::before {\n            inline-size: 100%;\n            block-size: 100%;\n            border-radius: var(--_radius);\n            box-shadow: inset 0 0 0 var(--ui-focus-width, 2px) var(--ui-focus);\n        }\n\n        :host(:focus-visible:not([selected])) .base::after {\n            background-color: var(--ui-focus-inset);\n            transform: scaleY(1);\n        }\n\n        :host(:focus-visible:not([selected]):hover) .base::after {\n            border-radius: 0;\n            background-color: var(--_hover, var(--ui-ghost-hover));\n            box-shadow: none;\n        }\n\n        :host([disabled]) {\n            color: var(--ui-ink-disabled);\n            cursor: not-allowed;\n        }\n\n        :host([disabled]) .base {\n            border-color: var(--ui-border-disabled);\n            background-color: transparent;\n        }\n\n        :host([disabled]) .base::after {\n            display: none;\n        }\n\n        :host([disabled]) .base::before {\n            background-color: var(--ui-border-disabled);\n        }\n\n        :host(:first-of-type[disabled]) .base,\n        :host(:last-of-type[disabled]) .base {\n            box-shadow: none;\n        }\n\n        :host([selected][disabled]) .base {\n            background-color: var(--ui-button-disabled);\n        }\n\n        /* low contrast (Carbon --low-contrast): grey track, white selected pill with a hairline, bold selected text */\n        :host([data-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        }\n\n        :host([data-low-contrast][selected]) {\n            font-weight: 600; /* heading-compact-01 */\n        }\n\n        :host([data-low-contrast][selected]) .base::after {\n            box-shadow: inset 0 0 0 1px var(--ui-border-inverse);\n        }\n\n        :host([data-low-contrast][selected]:focus-visible) .base::after {\n            box-shadow: inset 0 0 0 var(--ui-focus-width, 2px) var(--ui-focus);\n        }\n\n        :host([data-low-contrast]:focus-visible:not([selected])) .base::after {\n            background-color: var(--ui-content-switcher-background);\n        }\n\n        :host([data-low-contrast][selected][disabled]) {\n            border-radius: 4px;\n        }\n\n        :host([data-low-contrast][selected][disabled]) .base {\n            border: 1px solid var(--ui-border-disabled);\n            background-color: var(--ui-content-switcher-selected);\n        }\n\n        /* icon only: a square button */\n        :host([icon-only]) {\n            inline-size: var(--_size, var(--ui-field-height-md));\n        }\n\n        :host([icon-only]) .base {\n            padding: 0;\n            justify-content: center;\n        }\n\n        :host([icon-only][data-size='sm']) {\n            --_size: var(--ui-field-height-sm);\n        }\n\n        :host([icon-only][data-size='lg']) {\n            --_size: var(--ui-field-height-lg);\n        }\n\n        .label {\n            position: relative;\n            z-index: 1;\n            display: inline-flex;\n            align-items: center;\n            gap: var(--ui-space-xs);\n            max-inline-size: 100%;\n            overflow: hidden;\n            text-overflow: ellipsis;\n            white-space: nowrap;\n        }\n\n        ::slotted([slot='icon']) {\n            flex-shrink: 0;\n            width: 16px;\n            height: 16px;\n            fill: var(--ui-icon-secondary);\n        }\n\n        :host([icon-only]) ::slotted([slot='icon']) {\n            fill: var(--ui-icon-primary);\n        }\n\n        :host([data-size='lg']) ::slotted([slot='icon']) {\n            width: 20px;\n            height: 20px;\n        }\n\n        :host(:hover) ::slotted([slot='icon']),\n        :host(:focus-visible) ::slotted([slot='icon']) {\n            fill: var(--ui-icon-primary);\n        }\n\n        :host([selected]) ::slotted([slot='icon']) {\n            fill: var(--_selected-icon, var(--ui-icon-inverse));\n        }\n\n        :host([data-low-contrast]) {\n            --_selected-icon: var(--ui-icon-primary);\n        }\n\n        :host([disabled]) ::slotted([slot='icon']) {\n            fill: var(--ui-icon-disabled);\n        }\n\n        /* the tooltip anchors to the host and shows its bubble in the top layer; it takes no room here */\n        ui-tooltip {\n            position: absolute;\n            cursor: auto;\n            white-space: normal;\n            user-select: auto;\n        }\n\n        @media (forced-colors: active) {\n            :host([selected]) .base::after {\n                background-color: Highlight;\n            }\n\n            :host(:focus-visible) {\n                outline: 2px solid CanvasText;\n                outline-offset: -2px;\n            }\n        }\n    `;\n\n    /** Identifies the item in `ui-change` (Carbon `name`). */\n    @property() name = '';\n\n    /** Accessible name — required with `icon-only`. */\n    @property() label = '';\n\n    /**\n     * Set by the switcher from its `selected-index` (it overwrites any value you set): read it or style on\n     * `[selected]`; select an item with the switcher's `selected-index`.\n     */\n    @property({ type: Boolean, reflect: true }) selected = false;\n\n    @property({ type: Boolean, reflect: true }) disabled = false;\n\n    @property({ type: Boolean, reflect: true, attribute: 'icon-only' }) iconOnly = false;\n\n    /** Where the `icon-only` tooltip shows (Carbon `align`, default top). */\n    @property({ attribute: 'tooltip-placement' }) tooltipPlacement: TooltipPlacement = 'top';\n\n    /** Set by the switcher: which item is the single tab stop (the selected one, or the first enabled one). */\n    @property({ type: Boolean, attribute: false }) focusable = false;\n\n    override connectedCallback() {\n        super.connectedCallback();\n        if (!this.hasAttribute('role')) this.setAttribute('role', 'tab');\n    }\n\n    protected override updated() {\n        this.tabIndex = this.focusable && !this.disabled ? 0 : -1;\n        this.setAttribute('aria-selected', this.selected ? 'true' : 'false');\n        if (this.disabled) this.setAttribute('aria-disabled', 'true');\n        else this.removeAttribute('aria-disabled');\n        setManagedAttribute(this, 'aria-label', this.iconOnly && this.label ? this.label : null);\n    }\n\n    override render() {\n        const label = html`<span class=\"base\"><span class=\"label\" part=\"label\"><slot name=\"icon\"></slot><slot></slot></span></span>`;\n        if (!this.iconOnly || !this.label) return label;\n        // a click on the bubble is no click on the item\n        return html`${label}<ui-tooltip\n                as-label\n                part=\"tooltip\"\n                text=${this.label}\n                placement=${this.tooltipPlacement}\n                .anchor=${this}\n                @click=${(e: Event) => e.stopPropagation()}\n            ></ui-tooltip>`;\n    }\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-content-switcher-item': UiContentSwitcherItem;\n    }\n}\n"
    },
    {
      "path": "src/primitives/content-switcher/content-switcher.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/content-switcher/content-switcher.ts",
      "content": "// Its child elements are one per file beside it; this module re-exports them (one entry point).\nimport { css, html } from 'lit';\nimport { customElement, property, queryAssignedElements } from 'lit/decorators.js';\nimport { UiElement } from '../shared/element.ts';\nimport { rovingTarget } from '../../core/roving.ts';\nimport type { UiContentSwitcherItem } from './content-switcher-item.ts';\nimport '../tooltip/tooltip.ts';\nexport * from './content-switcher-item.ts';\n\nexport type ContentSwitcherSize = 'sm' | 'md' | 'lg';\n\nexport type ContentSwitcherSelectionMode = 'automatic' | 'manual';\n\n/**\n * Carbon `ContentSwitcher`: a `tablist` of `<ui-content-switcher-item>`s where exactly one is\n * selected. Click, or Left/Right (wrapping, skipping disabled items) select immediately;\n * `selection-mode=\"manual\"` moves focus only and Enter / Space selects. Home / End jump\n * to the first / last item (APG). Sizes sm 32 / md 40 / lg 48px, full width with items shared\n * evenly; `icon-only` (every item is `icon-only`) shrinks to fit. `low-contrast` renders the\n * grey variant. Set `aria-label` on the host to name the group.\n *\n * Deviation from Carbon React: arrow keys skip disabled items (Carbon's `getNextIndex` would\n * try to focus a disabled button, which does nothing).\n *\n * @slot - `<ui-content-switcher-item>` elements\n * @fires ui-change - detail: { index, name } after the user selects a different item\n * @cssprop --ui-content-switcher-border / --ui-content-switcher-selected-fill / --ui-content-switcher-selected-ink - Border, selected fill and selected text of the default kind (default: Carbon's inverse; Kanok: no border, a segment in the page colour with ink text)\n * @cssprop --ui-content-switcher-track / --ui-content-switcher-padding / --ui-content-switcher-radius / --ui-content-switcher-outline - The track around the items (default transparent, 0, 4px, border-inverse: Carbon's outlined group; Kanok: the contained tabs' segmented track)\n * @cssprop --ui-content-switcher-item-radius / --ui-content-switcher-item-size / --ui-content-switcher-item-grow / --ui-content-switcher-divider / --ui-content-switcher-selected-shadow - Its items (on `<ui-content-switcher-item>`)\n */\n@customElement('ui-content-switcher')\nexport class UiContentSwitcher extends UiElement {\n    static override readonly styles = css`\n        :host {\n            display: flex;\n            justify-content: space-evenly;\n            box-sizing: border-box;\n            inline-size: 100%;\n            block-size: var(--ui-field-height-md);\n            /* --ui-content-switcher-track / -radius / -padding / -outline: Carbon's outlined strip; a design can make it the\n               contained tabs' segmented track (a fill, inset items, no outline) */\n            padding: var(--ui-content-switcher-padding, 0px);\n            border-radius: var(--ui-content-switcher-radius, 4px);\n            background-color: var(--ui-content-switcher-track, transparent);\n            outline: 1px solid var(--ui-content-switcher-outline, var(--ui-border-inverse));\n            outline-offset: -1px;\n            font-family: var(--ui-font-family);\n        }\n\n        :host([size='sm']) {\n            block-size: var(--ui-field-height-sm);\n        }\n\n        :host([size='lg']) {\n            block-size: var(--ui-field-height-lg);\n        }\n\n        /* Carbon .cds--content-switcher:has(.cds--content-switcher-btn:disabled); an attribute, as Chromium matches no :has() inside :host() */\n        :host([data-has-disabled]) {\n            outline-color: var(--ui-border-disabled);\n        }\n\n        :host([icon-only]) {\n            inline-size: fit-content;\n            justify-content: flex-start;\n        }\n\n        :host([low-contrast]) {\n            background-color: var(--ui-content-switcher-background);\n            outline-color: var(--ui-hairline-strong);\n        }\n    `;\n\n    @property({ type: Number, reflect: true, attribute: 'selected-index' }) selectedIndex = 0;\n\n    @property({ reflect: true }) size: ContentSwitcherSize = 'md';\n\n    @property({ attribute: 'selection-mode' }) selectionMode: ContentSwitcherSelectionMode = 'automatic';\n\n    @property({ type: Boolean, reflect: true, attribute: 'low-contrast' }) lowContrast = false;\n\n    /** Reflected by the switcher itself when every item is `icon-only`. */\n    @property({ type: Boolean, reflect: true, attribute: 'icon-only' }) iconOnly = false;\n\n    @queryAssignedElements({ selector: 'ui-content-switcher-item' }) private readonly items!: UiContentSwitcherItem[];\n\n    private hovered: UiContentSwitcherItem | null = null;\n    private focused: UiContentSwitcherItem | null = null;\n\n    override connectedCallback() {\n        super.connectedCallback();\n        if (!this.hasAttribute('role')) this.setAttribute('role', 'tablist');\n    }\n\n    protected override updated() {\n        this.sync();\n    }\n\n    private enabledItems() {\n        return this.items.filter((i) => !i.disabled);\n    }\n\n    private sync() {\n        const items = this.items;\n        const selected = items[this.selectedIndex];\n        const focusable = selected && !selected.disabled ? selected : this.enabledItems()[0];\n        items.forEach((item, i) => {\n            item.selected = i === this.selectedIndex;\n            item.focusable = item === focusable;\n            item.dataset.size = this.size;\n            item.toggleAttribute('data-low-contrast', this.lowContrast);\n            const prev = items[i - 1];\n            item.toggleAttribute('data-no-divider', !!prev && (prev.selected || prev === this.hovered || prev === this.focused));\n        });\n        this.iconOnly = items.length > 0 && items.every((i) => i.iconOnly);\n        this.toggleAttribute('data-has-disabled', items.some((i) => i.disabled));\n    }\n\n    private itemOf(target: EventTarget | null) {\n        return (target as Element | null)?.closest?.('ui-content-switcher-item') ?? null;\n    }\n\n    private select(item: UiContentSwitcherItem) {\n        const index = this.items.indexOf(item);\n        if (index < 0 || item.disabled || index === this.selectedIndex) return;\n        this.selectedIndex = index;\n        this.sync();\n        this.dispatchEvent(new CustomEvent('ui-change', { detail: { index, name: item.name }, bubbles: true, composed: true }));\n    }\n\n    private onClick(event: MouseEvent) {\n        const item = this.itemOf(event.target);\n        if (!item || item.disabled) return;\n        item.focus();\n        this.select(item);\n    }\n\n    private nextItem(current: UiContentSwitcherItem, key: string) {\n        const items = this.enabledItems();\n        const i = items.indexOf(current);\n        const next = rovingTarget(items.length, i, key, { rtl: getComputedStyle(this).direction === 'rtl' });\n        return next === undefined ? undefined : items[next];\n    }\n\n    private onKeydown(event: KeyboardEvent) {\n        const current = this.itemOf(event.target);\n        if (!current || current.disabled) return;\n        if (event.key === 'Enter' || event.key === ' ') {\n            event.preventDefault();\n            this.select(current);\n            return;\n        }\n        const next = this.nextItem(current, event.key);\n        if (!next) return;\n        event.preventDefault();\n        next.focus();\n        if (this.selectionMode === 'automatic') this.select(next);\n    }\n\n    private onPointer(event: PointerEvent) {\n        this.hovered = event.type === 'pointerover' ? this.itemOf(event.target) : null;\n        this.sync();\n    }\n\n    private onFocus(event: FocusEvent) {\n        this.focused = event.type === 'focusin' ? this.itemOf(event.target) : null;\n        this.sync();\n    }\n\n    /** Focus the selected item (the tab stop). */\n    override focus(options?: FocusOptions) {\n        (this.items.find((i) => i.focusable) ?? this.enabledItems()[0])?.focus(options);\n    }\n\n    override render() {\n        return html`<slot\n            @slotchange=${() => this.sync()}\n            @click=${this.onClick}\n            @keydown=${this.onKeydown}\n            @pointerover=${this.onPointer}\n            @pointerout=${this.onPointer}\n            @focusin=${this.onFocus}\n            @focusout=${this.onFocus}\n        ></slot>`;\n    }\n}\n\nexport interface ContentSwitcherChangeDetail {\n    index: number;\n    name: string;\n}\n\nexport interface UiContentSwitcherEventMap extends HTMLElementEventMap {\n    'ui-change': CustomEvent<ContentSwitcherChangeDetail>;\n}\n\n/** Typed listeners for UiContentSwitcher's events (declaration merging with the class). */\nexport interface UiContentSwitcher {\n    addEventListener<K extends keyof UiContentSwitcherEventMap>(type: K, listener: (this: UiContentSwitcher, event: UiContentSwitcherEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions): void;\n    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;\n    removeEventListener<K extends keyof UiContentSwitcherEventMap>(type: K, listener: (this: UiContentSwitcher, event: UiContentSwitcherEventMap[K]) => unknown, options?: boolean | EventListenerOptions): void;\n    removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-content-switcher': UiContentSwitcher;\n    }\n}\n"
    }
  ]
}
