{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "radio",
  "type": "registry:ui",
  "title": "<ui-radio-group>",
  "description": "Carbon-style radio button group. Holds `<ui-radio>` or `<ui-radio-tile>` options. 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/shared-form-control",
    "@kanok-ui/shared-localize",
    "@kanok-ui/shared-skeleton",
    "@kanok-ui/shared-slots",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/radio/radio-group.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/radio/radio-group.ts",
      "content": "import { css, nothing, html } from 'lit';\nimport { customElement, property, state, queryAssignedElements } from 'lit/decorators.js';\nimport { fieldTextStyles, uid, barWhenDisabled, customErrorMessage, renderLabelText, pickMessage, pickErrorText, messageId, renderMessage, type MessageKind } from '../shared/field.ts';\nimport { FormControlController } from '../shared/form-control.ts';\nimport { SlotController } from '../shared/slots.ts';\nimport { LocalizeController } from '../shared/localize.ts';\nimport { UiElement } from '../shared/element.ts';\nimport { rovingTarget } from '../../core/roving.ts';\nimport type { RadioLabelPosition } from './radio-shared.ts';\nimport type { UiRadio } from './radio.ts';\n\n/** Options a group manages: plain radios and radio tiles. */\nconst RADIO_SELECTOR = 'ui-radio, ui-radio-tile';\n\n/**\n * Carbon-style radio button group. Holds `<ui-radio>` or `<ui-radio-tile>` options.\n *\n * Keyboard (WAI-ARIA APG): Tab enters on the checked (or first) option, arrow keys\n * move and select, Space selects. Form-associated: submits the checked `value`.\n * `readonly` keeps the value (and submits it); arrows still move focus so options can\n * be read, but nothing changes. `warn` shows a non-blocking warning.\n * `label-position=\"left\"` (Carbon RadioButtonGroup `labelPosition`) puts every radio's text before its circle; a\n * vertical group then lines the circles up at its end, as wide as its widest option. A radio's own `label-position`\n * wins over the group's.\n *\n * State on the host for styling: `data-show-error` while the error shows, `data-show-warn` while the\n * warning shows.\n *\n * @cssstate show-error - While its error message shows (also `[data-show-error]`)\n * @cssstate show-warn - While its warning message shows (also `[data-show-warn]`)\n * @slot - `<ui-radio>` options\n * @slot label - Label content with markup; the `label` attribute is the plain-text fallback\n * @slot helper-text - Helper text with markup (links, bold); overrides `helper-text`\n * @slot warn-text - Warning text shown while `warn`; overrides `warn-text`\n * @slot invalid-text - Error text shown while invalid; overrides `invalid-text`\n * @fires change - When the user picks an option\n * @csspart base - The group box holding the radios\n * @csspart label - The group label\n * @csspart status-icon - The error / warning icon\n * @csspart helper-text - The helper line\n * @csspart warning-text - The warning line\n * @csspart warn-text - Same line as `warning-text` (named after the `warn-text` attribute / slot)\n * @csspart error-text - The error line\n * @csspart invalid-text - Same line as `error-text` (named after the `invalid-text` attribute / slot)\n */\n@customElement('ui-radio-group')\nexport class UiRadioGroup extends UiElement {\n    static override hostStates: readonly string[] = ['show-error', 'show-warn'];\n\n    static readonly formAssociated = true;\n\n    static override readonly styles = [\n        fieldTextStyles,\n        css`\n            :host {\n                display: block;\n                font-family: var(--ui-font-family);\n            }\n\n            .options {\n                display: flex;\n                flex-direction: column;\n                gap: var(--ui-space-xxs);\n            }\n\n            :host([orientation='horizontal']) .options {\n                flex-direction: row;\n                flex-wrap: wrap;\n                gap: var(--ui-space-md);\n            }\n\n            /* inherited by the radios (see ui-radio); their own label-position wins */\n            :host([label-position='left']) {\n                --_ui-radio-direction: row-reverse;\n                --_ui-radio-gap: 8px;\n                --_ui-radio-width: fit-content;\n            }\n\n            /* Carbon radio-button-group--vertical.--label-left: align-items flex-end, the group as wide as its content */\n            :host([label-position='left']:not([orientation='horizontal'])) .options {\n                inline-size: fit-content;\n                align-items: flex-end;\n            }\n        `,\n    ];\n\n    /** Group label (rendered like a fieldset legend). */\n    @property() label = '';\n\n    private readonly localize = new LocalizeController(this);\n\n    /** Keep the group label for screen readers but don't show it. */\n    @property({ type: Boolean, attribute: 'hide-label' }) hideLabel = false;\n\n    /** Reflected: form-associated elements submit under the `name` attribute. */\n    @property({ reflect: true }) name = '';\n\n    @property() value = '';\n\n    @property({ reflect: true }) orientation: 'vertical' | 'horizontal' = 'vertical';\n\n    /** Carbon `labelPosition`: `left` puts each radio's text before its circle (default `right`, after it). */\n    @property({ attribute: 'label-position', reflect: true }) labelPosition: RadioLabelPosition = 'right';\n\n    /** Disables the control; a disabled `<fieldset>` ancestor does the same. */\n    @property({ type: Boolean, reflect: true }) disabled = false;\n\n    /** Blocks form submission while the field is empty. */\n    @property({ type: Boolean, reflect: true }) required = false;\n\n    /** Shows and submits the value but blocks changes. */\n    @property({ type: Boolean, reflect: true }) readonly = false;\n\n    /** Helper text under the field, shown while there is no error or warning. */\n    @property({ attribute: 'helper-text' }) helperText = '';\n\n    /** Error text shown while `invalid` or while the field fails validation. */\n    @property({ attribute: 'invalid-text' }) invalidText = '';\n\n    /** Forces the error state, e.g. after server-side validation. */\n    @property({ type: Boolean, reflect: true }) invalid = false;\n\n    /** Non-blocking warning; shown when there is no error. */\n    @property({ type: Boolean, reflect: true }) warn = false;\n\n    /** Text of the warning shown while `warn`. */\n    @property({ attribute: 'warn-text' }) warnText = '';\n\n    /** Error text for an empty required group. */\n    @property({ attribute: 'required-text' }) requiredText = '';\n\n    @state() private touched = false;\n\n    /** Disabled, reset defaults, validation on submit and the name given from outside (shared by the form fields). */\n    private readonly formControl = new FormControlController(this, { reveal: () => (this.touched = true) });\n\n    @queryAssignedElements({ selector: RADIO_SELECTOR }) private readonly radios!: UiRadio[];\n\n\n    /** Message from `setCustomValidity()`; '' = none. */\n    private customValidityMessage = '';\n\n    /** Like `<input>.setCustomValidity()`: a non-empty message makes the field invalid (and is shown as its error once touched) until cleared with ''. */\n    setCustomValidity(message: string) {\n        this.customValidityMessage = message;\n        if (!this.hasUpdated) return;\n        this.syncValidity();\n        this.requestUpdate();\n    }\n    private readonly groupId = uid('ui-radio-group');\n    private defaultValue = '';\n\n    override connectedCallback() {\n        super.connectedCallback();\n        // first connection only: moving the element must not change what reset goes back to\n        this.formControl.captureDefaults(() => {\n            // `<ui-radio checked>` is the initial (and reset) choice unless the group's `value` is set\n            if (!this.value && !this.hasAttribute('value')) this.value = this.querySelector(`:is(${RADIO_SELECTOR})[checked]`)?.getAttribute('value') ?? '';\n            this.defaultValue = this.value;\n        });\n        this.addEventListener('click', this.onClick);\n        this.addEventListener('keydown', this.onKeydown);\n        this.addEventListener('focusout', this.onFocusout);\n    }\n\n    override disconnectedCallback() {\n        super.disconnectedCallback();\n        this.removeEventListener('click', this.onClick);\n        this.removeEventListener('keydown', this.onKeydown);\n        this.removeEventListener('focusout', this.onFocusout);\n    }\n\n    get form() {\n        return this.internals.form;\n    }\n\n    get validationMessage() {\n        return this.internals.validationMessage;\n    }\n\n    /** Whether the field takes part in constraint validation (not while disabled), as natively. */\n    get willValidate() {\n        return this.internals.willValidate;\n    }\n\n    /** The `<label>`s that point at this field. */\n    get labels() {\n        return this.internals.labels;\n    }\n\n    reportValidity() {\n        return this.formControl.reportValidity(this.internals);\n    }\n\n    get validity() {\n        return this.internals.validity;\n    }\n\n    checkValidity() {\n        return this.internals.checkValidity();\n    }\n\n    override focus(options?: FocusOptions) {\n        this.tabStop()?.focus(options);\n    }\n\n    formResetCallback() {\n        this.value = this.defaultValue;\n        this.touched = false;\n    }\n\n\n    /** Back/forward cache and autofill hand the saved value back here. */\n    formStateRestoreCallback(state: string | File | FormData | null) {\n        if (typeof state === 'string') this.value = state;\n    }\n    /** From an ancestor `<fieldset>`; kept apart so it never overwrites the author's `disabled`. */\n    formDisabledCallback(disabled: boolean) {\n        this.formControl.setFieldsetDisabled(disabled);\n    }\n\n    private get isDisabled() {\n        return this.formControl.disabled;\n    }\n\n    private get interactive() {\n        return !this.isDisabled && !this.readonly;\n    }\n\n    protected override updated() {\n        this.syncRadios();\n        this.syncValidity();\n        this.syncHostState();\n    }\n\n    /** Kind of the message the last render showed, for the host state attributes. */\n    private shownKind?: MessageKind;\n\n    private syncHostState() {\n        this.toggleAttribute('data-show-error', this.shownKind === 'error');\n        this.toggleAttribute('data-show-warn', this.shownKind === 'warn');\n    }\n\n    private syncValidity() {\n        if (barWhenDisabled(this.internals, this.isDisabled)) return;\n        this.internals.setFormValue(this.value || null);\n        const anchor = this.tabStop();\n        const customError = customErrorMessage(this.invalid, this.invalidText, this.customValidityMessage);\n        if (customError) {\n            this.internals.setValidity({ customError: true }, customError, anchor);\n        } else if (this.required && !this.value) {\n            this.internals.setValidity({ valueMissing: true }, this.invalidText || (this.requiredText || this.localize.t.field.requiredOne), anchor);\n        } else {\n            this.internals.setValidity({});\n        }\n    }\n\n    private get showError() {\n        return this.interactive && (this.invalid || (this.touched && (!!this.customValidityMessage || (this.required && !this.value))));\n    }\n\n    private enabledRadios() {\n        return this.radios.filter((r) => !r.disabled && !this.isDisabled);\n    }\n\n    /** The single radio that is reachable with Tab. */\n    private tabStop() {\n        const enabled = this.enabledRadios();\n        return enabled.find((r) => r.value === this.value) ?? enabled[0];\n    }\n\n    /** The visible error text: the attribute, else the slotted `invalid-text`, else the required message. */\n    private get errorText() {\n        return this.invalidText || this.querySelector('[slot=\"invalid-text\"]')?.textContent?.trim() || (this.requiredText || this.localize.t.field.requiredOne);\n    }\n\n    private syncRadios() {\n        const stop = this.tabStop();\n        const showError = this.showError;\n        for (const radio of this.radios) {\n            radio.checked = radio.value === this.value && this.value !== '';\n            radio.tabIndex = radio === stop ? 0 : -1;\n            radio.toggleAttribute('data-invalid', showError);\n            // The message lives in this shadow root, so the radios (light DOM hosts) cannot reference it by id:\n            // repeat it as a plain-text description (ARIA 1.3 aria-description) and flag each radio invalid.\n            if (showError) {\n                radio.setAttribute('aria-description', this.errorText);\n                radio.setAttribute('aria-invalid', 'true');\n            } else {\n                radio.removeAttribute('aria-description');\n                radio.removeAttribute('aria-invalid');\n            }\n            radio.toggleAttribute('data-group-disabled', this.isDisabled);\n            radio.toggleAttribute('data-group-readonly', this.readonly);\n        }\n    }\n\n    private select(radio: UiRadio) {\n        if (radio.disabled || this.isDisabled) return;\n        radio.focus();\n        if (this.readonly || radio.value === this.value) return;\n        this.value = radio.value;\n        this.dispatchEvent(new Event('input', { bubbles: true, composed: true }));\n        this.dispatchEvent(new Event('change', { bubbles: true }));\n    }\n\n    private readonly onClick = (event: MouseEvent) => {\n        const radio = (event.target as Element).closest<UiRadio>(RADIO_SELECTOR);\n        if (radio) this.select(radio);\n    };\n\n    private readonly onKeydown = (event: KeyboardEvent) => {\n        const radios = this.enabledRadios();\n        const current = (event.target as Element).closest<UiRadio>(RADIO_SELECTOR);\n        if (!current || radios.length === 0) return;\n        // APG radio group: any arrow moves and selects, wrapping; Space selects the focused radio\n        const moved = rovingTarget(radios.length, radios.indexOf(current), event.key, { orientation: 'both', homeEnd: false, rtl: getComputedStyle(this).direction === 'rtl' });\n        if (moved === undefined && event.key !== ' ') return;\n        event.preventDefault();\n        this.select(moved === undefined ? current : radios[moved]);\n    };\n\n    private readonly onFocusout = (event: FocusEvent) => {\n        if (!this.contains(event.relatedTarget as Node)) this.touched = true;\n    };\n\n    private readonly slots = new SlotController(this);\n\n    private renderLabel() {\n        if (!this.label && !this.slots.has('label')) return nothing;\n        return html`<div class=${this.hideLabel ? 'sr-only' : 'label'} part=\"label\" id=\"${this.groupId}-label\">${renderLabelText(this.label, this.formControl.fallbackName(this.label, this.internals))}</div>`;\n    }\n\n    override render() {\n        const showError = this.showError;\n        const message = pickMessage({\n            showError,\n            errorText: pickErrorText(this.invalid, this.customValidityMessage, this.invalidText || (this.requiredText || this.localize.t.field.requiredOne)),\n            showWarn: this.warn && this.interactive,\n            warnText: this.warnText,\n            helperText: this.helperText,\n            slots: this.slots,\n        });\n        this.shownKind = message?.kind;\n        return html`\n            ${this.renderLabel()}\n            <div\n                class=\"options\"\n                part=\"base\"\n                role=\"radiogroup\"\n                aria-labelledby=${this.label || this.slots.has('label') || this.formControl.fallbackName(this.label, this.internals) ? `${this.groupId}-label` : nothing}\n                aria-describedby=${messageId(this.groupId, message) ?? nothing}\n                aria-required=${this.required ? 'true' : 'false'}\n                aria-readonly=${this.readonly ? 'true' : nothing}\n                aria-disabled=${this.isDisabled ? 'true' : nothing}\n                aria-invalid=${showError ? 'true' : 'false'}\n            >\n                <slot @slotchange=${() => this.syncRadios()}></slot>\n            </div>\n            ${renderMessage(this.groupId, message, true)}\n        `;\n    }\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-radio-group': UiRadioGroup;\n    }\n}\n"
    },
    {
      "path": "src/primitives/radio/radio-shared.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/radio/radio-shared.ts",
      "content": "\n/** Carbon `labelPosition`: the text after the circle (`right`, the default) or before it (`left`). */\nexport type RadioLabelPosition = 'left' | 'right';\n"
    },
    {
      "path": "src/primitives/radio/radio-skeleton.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/radio/radio-skeleton.ts",
      "content": "import { html, css } from 'lit';\nimport { customElement } from 'lit/decorators.js';\nimport { skeletonStyles, UiSkeletonElement } from '../shared/skeleton.ts';\n\n/**\n * Carbon `RadioButtonSkeleton` (`cds-radio-button-skeleton`): a 100×18 label bar (Carbon draws no circle). Hidden\n * from assistive tech.\n *\n * @csspart label - The label bar\n */\n@customElement('ui-radio-skeleton')\nexport class UiRadioSkeleton extends UiSkeletonElement {\n    static override readonly styles = [\n        skeletonStyles,\n        css`\n            :host {\n                display: block;\n            }\n\n            .label {\n                inline-size: 100px;\n                block-size: 18px;\n            }\n        `,\n    ];\n\n    override render() {\n        return html`<div class=\"bone label\" part=\"label\"></div>`;\n    }\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-radio-skeleton': UiRadioSkeleton;\n    }\n}\n"
    },
    {
      "path": "src/primitives/radio/radio.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/radio/radio.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 } from 'lit/decorators.js';\nimport { UiElement } from '../shared/element.ts';\nimport type { RadioLabelPosition } from './radio-shared.ts';\nexport * from './radio-group.ts';\nexport type { RadioLabelPosition } from './radio-shared.ts';\n\n/**\n * One option inside `<ui-radio-group>`. Native radios can't group across shadow roots,\n * so this follows the WAI-ARIA radio-group pattern: role/aria-checked/roving tabindex\n * live on the host, and the group handles keyboard and form value.\n *\n * `label-position=\"left\"` (Carbon RadioButton `labelPosition`) puts the text before the circle, 8px apart; left\n * unset, the radio follows its group's `label-position`.\n *\n * @slot - Label text\n */\n/**\n * @csspart circle - The drawn radio circle\n * @csspart label - The text beside it\n * @cssprop --ui-radio-border - Ring colour (default `--ui-ink`; Kanok: `--ui-ink-subtle`)\n * @cssprop --ui-radio-checked - Ring and dot when chosen (default `--ui-ink`; Kanok: `--ui-primary`)\n */\n@customElement('ui-radio')\nexport class UiRadio extends UiElement {\n    static override readonly styles = css`\n        /*\n         * Carbon radio-button__label--left: row-reverse, the circle's inline margins 8px 0 (8px after the text, none at\n         * the end), and the label only as wide as its content. A group's label-position arrives through these.\n         */\n        :host([label-position='left']) {\n            --_ui-radio-direction: row-reverse;\n            --_ui-radio-gap: 8px;\n            --_ui-radio-width: fit-content;\n        }\n\n        :host([label-position='right']) {\n            --_ui-radio-direction: row;\n            --_ui-radio-gap: 10px;\n            --_ui-radio-width: auto;\n        }\n\n        :host {\n            display: flex;\n            flex-direction: var(--_ui-radio-direction, row);\n            align-items: flex-start;\n            gap: var(--_ui-radio-gap, 10px); /* Carbon radio: 10px between the circle and the text */\n            inline-size: var(--_ui-radio-width, auto);\n            min-height: 24px;\n            font: var(--ui-font-body-sm);\n            letter-spacing: var(--ui-tracking-body);\n            color: var(--ui-ink);\n            cursor: pointer;\n            outline: none;\n        }\n\n        /* 3px above and below (Carbon 24px row) — on the parts, not :host, so a page-wide CSS reset can't remove it */\n        .circle,\n        .text {\n            margin-block: 3px;\n        }\n\n        .circle {\n            flex-shrink: 0;\n            display: grid;\n            place-items: center;\n            width: 18px;\n            height: 18px;\n            box-sizing: border-box;\n            /* --ui-radio-border / -checked: Carbon draws in primary ink; a design can use its brand colour */\n            border: 1px solid var(--ui-radio-border, var(--ui-ink));\n            border-radius: 50%;\n            background-color: transparent; /* Carbon radio-button__appearance: the row or layer shows through */\n        }\n\n        :host([checked]) .circle {\n            border-color: var(--ui-radio-checked, var(--ui-ink));\n        }\n\n        :host([checked]) .circle::after {\n            content: '';\n            width: 8px;\n            height: 8px;\n            border-radius: 50%;\n            background-color: var(--ui-radio-checked, var(--ui-ink));\n        }\n\n        /* Carbon radio-button:focus (not :focus-visible), so a click shows the ring too; Carbon's 1.5px offset computes to 1px */\n        :host(:focus) .circle {\n            outline: var(--ui-focus-width, 2px) solid var(--ui-focus);\n            outline-offset: 1px;\n        }\n\n        /* a mouse or touch focus shows no ring in a design that sets --ui-pointer-focus (Kanok: keyboard only) */\n        :host(:focus:not(:focus-visible)) .circle {\n            outline-color: var(--ui-pointer-focus, var(--ui-focus));\n        }\n\n        :host([data-invalid]) .circle {\n            border: 2px solid var(--ui-error);\n        }\n\n        :host([disabled]),\n        :host([data-group-disabled]) {\n            color: var(--ui-ink-disabled);\n            cursor: not-allowed;\n        }\n\n        :host([disabled]) .circle,\n        :host([data-group-disabled]) .circle {\n            border-color: var(--ui-ink-disabled);\n        }\n\n        :host([data-group-readonly]) {\n            cursor: default;\n        }\n\n        :host([data-group-readonly]) .circle {\n            border-color: var(--ui-ink-subtle);\n        }\n\n        :host([data-group-readonly][checked]) .circle::after {\n            background-color: var(--ui-ink-muted);\n        }\n\n        /* Carbon radio-button:disabled:checked: the dot greys too, whether the radio or its whole group is disabled */\n        :host([disabled][checked]) .circle::after,\n        :host([data-group-disabled][checked]) .circle::after {\n            background-color: var(--ui-ink-disabled);\n        }\n\n        @media (forced-colors: active) {\n            .circle {\n                border-color: CanvasText;\n                forced-color-adjust: none;\n            }\n            :host([checked]) .circle::after {\n                background-color: Highlight;\n            }\n        }\n    `;\n\n    @property() value = '';\n\n    @property({ type: Boolean, reflect: true }) checked = false;\n\n    /** Disables the control; a disabled `<fieldset>` ancestor does the same. */\n    @property({ type: Boolean, reflect: true }) disabled = false;\n\n    /** Carbon `labelPosition`: `left` puts the text before the circle; unset, the group's `label-position` applies. */\n    @property({ attribute: 'label-position', reflect: true }) labelPosition?: RadioLabelPosition;\n\n    override connectedCallback() {\n        super.connectedCallback();\n        if (!this.hasAttribute('role')) this.setAttribute('role', 'radio');\n    }\n\n    protected override updated() {\n        this.setAttribute('aria-checked', String(this.checked));\n        this.setAttribute('aria-disabled', String(this.disabled));\n    }\n\n    override render() {\n        return html`<span class=\"circle\" part=\"circle\" aria-hidden=\"true\"></span><span class=\"text\" part=\"label\"><slot></slot></span>`;\n    }\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-radio': UiRadio;\n    }\n}\n"
    }
  ]
}
