{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "toggle",
  "type": "registry:ui",
  "title": "<ui-toggle>",
  "description": "Carbon-style toggle (`Toggle`): an on / off switch with a label above and a state text beside it. 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/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/toggle/toggle-skeleton.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/toggle/toggle-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 `ToggleSkeleton` (`cds-toggle-skeleton`): an 18px circle and a 24×8 bar beside it. Hidden from assistive\n * tech.\n *\n * @csspart circle - The circle\n * @csspart bar - The bar\n */\n@customElement('ui-toggle-skeleton')\nexport class UiToggleSkeleton extends UiSkeletonElement {\n    static override readonly styles = [\n        skeletonStyles,\n        css`\n            :host {\n                display: block;\n            }\n\n            .row {\n                display: flex;\n                align-items: center;\n                gap: var(--ui-space-xs);\n                block-size: 18px;\n            }\n\n            .circle {\n                inline-size: 18px;\n                block-size: 18px;\n                border-radius: 50%;\n            }\n\n            .bar {\n                inline-size: 24px;\n                block-size: 8px;\n            }\n        `,\n    ];\n\n    override render() {\n        return html`<div class=\"row\"><span class=\"bone circle\" part=\"circle\"></span><span class=\"bone bar\" part=\"bar\"></span></div>`;\n    }\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-toggle-skeleton': UiToggleSkeleton;\n    }\n}\n"
    },
    {
      "path": "src/primitives/toggle/toggle.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/toggle/toggle.ts",
      "content": "import { LitElement, html, css, nothing } from 'lit';\nimport { customElement, property } from 'lit/decorators.js';\nimport { srOnlyStyles, uid, renderLabelText, isLabelClick } 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';\n\nexport type ToggleSize = 'sm' | 'md';\n\n/**\n * Carbon-style toggle (`Toggle`): an on / off switch with a label above and a state text beside it.\n *\n * Structure follows Carbon: a `role=\"switch\"` button (visually hidden) drives a 48×24 track with an\n * 18px handle (`size=\"sm\"`: 32×16, 10px handle with a check mark when on). Off = `--ui-toggle-off`,\n * on = `--ui-success`. `label-a` / `label-b` (\"Off\" / \"On\") are the side text; with `hide-label` the\n * label itself moves beside the switch and the state text is dropped, as in Carbon.\n *\n * Form-associated: submits `value` (default \"on\") under `name` while checked, resets with the form.\n *\n * @slot label - Label content with markup; the `label` attribute is the plain-text fallback\n * @fires change - After the user toggles it (`checked` already updated)\n * @csspart base - The row holding the switch and the side text\n * @csspart label - The label above the switch\n * @csspart switch - The track\n * @csspart handle - The knob\n * @csspart text - The On / Off text beside the switch\n * @cssprop --ui-toggle-on - Track colour when on (default `--ui-success`; Kanok: `--ui-primary`)\n */\n@customElement('ui-toggle')\nexport class UiToggle extends UiElement {\n    static readonly formAssociated = true;\n\n    static override readonly shadowRootOptions: ShadowRootInit = {\n        ...LitElement.shadowRootOptions,\n        delegatesFocus: true,\n    };\n\n    static override readonly styles = [\n        srOnlyStyles,\n        css`\n            :host {\n                display: block;\n                font-family: var(--ui-font-family);\n            }\n\n            .label {\n                display: block;\n                margin-bottom: var(--ui-space-md); /* Carbon: 16px (sm: 8px) between label and switch */\n                font: var(--ui-label-font, var(--ui-font-caption)); /* the field label settings (shared/field.ts) */\n                letter-spacing: var(--ui-tracking-caption);\n                color: var(--ui-label-color, var(--ui-ink-muted));\n            }\n\n            :host([size='sm']) .label {\n                margin-bottom: var(--ui-space-xs);\n            }\n\n            .row {\n                display: flex;\n                align-items: center;\n                cursor: pointer;\n            }\n\n            /* the real control: keyboard-reachable, visually hidden (Carbon toggle__button) */\n            .button {\n                position: absolute;\n                width: 1px;\n                height: 1px;\n                margin: -1px;\n                padding: 0;\n                border: 0;\n                overflow: hidden;\n                clip-path: inset(50%);\n                white-space: nowrap;\n            }\n\n            .switch {\n                position: relative;\n                box-sizing: border-box;\n                width: 48px;\n                height: 24px;\n                border-radius: 12px;\n                background-color: var(--ui-toggle-off);\n                transition: background-color var(--ui-duration-fast) var(--ui-easing-exit); /* Carbon toggle__switch: fast-01, exit easing */\n            }\n\n            .handle {\n                position: absolute;\n                top: 3px;\n                inset-inline-start: 3px;\n                display: grid;\n                place-items: center;\n                width: 18px;\n                height: 18px;\n                border-radius: 50%;\n                background-color: var(--ui-on-primary);\n                transition: translate var(--ui-duration-fast) var(--ui-easing-exit); /* Carbon toggle__switch::before: fast-01, exit easing */\n            }\n\n            /* on: Carbon support-success; --ui-toggle-on lets a design use its brand colour */\n            .switch.checked {\n                background-color: var(--ui-toggle-on, var(--ui-success));\n            }\n\n            .switch.checked .handle {\n                translate: 24px 0;\n            }\n\n            :host(:dir(rtl)) .switch.checked .handle {\n                translate: -24px 0;\n            }\n\n            :host([size='sm']) .switch {\n                width: 32px;\n                height: 16px;\n            }\n\n            :host([size='sm']) .handle {\n                width: 10px;\n                height: 10px;\n            }\n\n            :host([size='sm']) .switch.checked .handle {\n                translate: 16px 0;\n            }\n\n            :host([size='sm']:dir(rtl)) .switch.checked .handle {\n                translate: -16px 0;\n            }\n\n            /* sm shows a 6×5 check mark in the knob while on (Carbon toggle__check) */\n            .check {\n                display: none;\n                width: 6px;\n                height: 5px;\n                fill: var(--ui-toggle-on, var(--ui-success));\n            }\n\n            :host([size='sm']) .switch.checked .check {\n                display: block;\n            }\n\n            .text {\n                margin-inline-start: var(--ui-space-xs);\n                font: var(--ui-font-body-sm);\n                line-height: 20px; /* body-01 */\n                letter-spacing: var(--ui-tracking-body);\n                color: var(--ui-ink);\n            }\n\n            /* Carbon toggle__button:focus (not :focus-visible), so a click keeps the ring after release */\n            .button:focus ~ .row .switch,\n            .row:active .switch {\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            .button:focus:not(:focus-visible) ~ .row .switch,\n            .row:active .switch {\n                outline-color: var(--ui-pointer-focus, var(--ui-focus));\n            }\n\n            /* disabled */\n            :host([aria-disabled='true']) .row,\n            :host([aria-disabled='true']) .label {\n                cursor: not-allowed;\n            }\n\n            :host([aria-disabled='true']) .label,\n            :host([aria-disabled='true']) .text {\n                color: var(--ui-ink-disabled);\n            }\n\n            :host([aria-disabled='true']) .switch,\n            :host([aria-disabled='true']) .switch.checked {\n                background-color: var(--ui-button-disabled);\n            }\n\n            :host([aria-disabled='true']) .handle {\n                background-color: var(--ui-on-color-disabled); /* icon-on-color-disabled, the same values */\n            }\n\n            :host([aria-disabled='true']) .check {\n                fill: var(--ui-button-disabled);\n            }\n\n            /* read-only: hollow track, dark knob */\n            :host([readonly]) .row {\n                cursor: default;\n            }\n\n            :host([readonly]) .switch,\n            :host([readonly]) .switch.checked {\n                border: 1px solid var(--ui-ink-disabled);\n                background-color: transparent;\n            }\n\n            :host([readonly]) .handle {\n                top: 2px;\n                inset-inline-start: 2px;\n                background-color: var(--ui-ink);\n            }\n\n            :host([readonly]) .check {\n                fill: var(--ui-canvas);\n            }\n\n            @media (forced-colors: active) {\n                .switch {\n                    border: 1px solid ButtonText;\n                }\n                .switch.checked {\n                    background-color: Highlight;\n                }\n                .handle {\n                    background-color: ButtonText;\n                    forced-color-adjust: none;\n                }\n            }\n        `,\n    ];\n\n    /** Label above the switch; the `label` slot replaces it when markup is needed. */\n    @property() label = '';\n\n    private readonly localize = new LocalizeController(this);\n    /** Hide the label above; it then replaces the On / Off text beside the switch (Carbon `hideLabel`). */\n    @property({ type: Boolean, attribute: 'hide-label' }) hideLabel = false;\n    /** Side text while off (Carbon `labelA`). */\n    @property({ attribute: 'label-a' }) labelA = '';\n    /** Side text while on (Carbon `labelB`). */\n    @property({ attribute: 'label-b' }) labelB = '';\n    @property({ type: Boolean, reflect: true }) checked = false;\n    /** Submitted under `name` while checked. */\n    @property() value = 'on';\n    /** Reflected: form-associated elements submit under the `name` attribute. */\n    @property({ reflect: true }) name = '';\n    /** md: 48×24 track (default) · sm: 32×16 with a check mark in the knob. */\n    @property({ reflect: true }) size: ToggleSize = 'md';\n    /** Disables the control; a disabled `<fieldset>` ancestor does the same. */\n    @property({ type: Boolean, reflect: true }) disabled = false;\n    /** Shows and submits the state but blocks toggling. */\n    @property({ type: Boolean, reflect: true }) readonly = 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);\n\n    private readonly ids = uid('ui-toggle');\n    private readonly slots = new SlotController(this);\n    private defaultChecked = false;\n\n    constructor() {\n        super();\n        this.addEventListener('click', this.onLabelClick);\n    }\n\n    /** `<label for>` / a wrapping label: flip like a click on the switch. */\n    private readonly onLabelClick = (event: MouseEvent) => {\n        if (isLabelClick(this, event)) this.shadowRoot?.querySelector<HTMLButtonElement>('button[role=\"switch\"]')?.click();\n    };\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            this.defaultChecked = this.checked;\n        });\n    }\n\n    get form() {\n        return this.internals.form;\n    }\n\n    get validity() {\n        return this.internals.validity;\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    checkValidity() {\n        return this.internals.checkValidity();\n    }\n\n    reportValidity() {\n        return this.formControl.reportValidity(this.internals);\n    }\n\n    protected override updated() {\n        this.internals.setFormValue(this.checked && !this.isDisabled ? this.value : null, String(this.checked));\n        // the host carries the disabled state for AT and styling, like a native disabled control\n        if (this.isDisabled) this.setAttribute('aria-disabled', 'true');\n        else this.removeAttribute('aria-disabled');\n    }\n\n    formResetCallback() {\n        this.checked = this.defaultChecked;\n    }\n\n    formStateRestoreCallback(state: string | File | FormData | null) {\n        if (typeof state === 'string') this.checked = state === 'true';\n    }\n\n    formDisabledCallback(disabled: boolean) {\n        this.formControl.setFieldsetDisabled(disabled);\n    }\n\n    private get isDisabled() {\n        return this.formControl.disabled;\n    }\n\n    private toggle() {\n        if (this.isDisabled || this.readonly) return;\n        this.checked = !this.checked;\n        this.dispatchEvent(new Event('input', { bubbles: true, composed: true }));\n        this.dispatchEvent(new Event('change', { bubbles: true }));\n    }\n\n    /** A click on the switch, or on the track and side text (its label, as Carbon): toggle and keep focus on the switch. */\n    private onClick(event: MouseEvent) {\n        this.toggle();\n        (event.currentTarget as HTMLElement).focus();\n    }\n\n    private renderLabel() {\n        // no label of its own: an unseen one with the name given from outside (aria-label, a <label for>), if any\n        const unlabelled = !this.label && !this.slots.has('label');\n        const outside = unlabelled ? this.formControl.fallbackName(this.label, this.internals) : '';\n        if (this.hideLabel || (unlabelled && !outside)) return nothing;\n        return html`<span class=${unlabelled ? 'sr-only' : 'label'} part=\"label\" id=\"${this.ids}-label\">${renderLabelText(this.label, outside)}</span>`;\n    }\n\n    private get sideText() {\n        if (this.hideLabel) return this.label;\n        return this.checked ? (this.labelB || this.localize.t.field.on) : (this.labelA || this.localize.t.field.off);\n    }\n\n    override render() {\n        const hasLabel = this.label || this.slots.has('label') || this.formControl.fallbackName(this.label, this.internals);\n        // with hide-label the only label is the side text, so the switch is named by it\n        const labelSuffix = this.hideLabel ? 'text' : 'label';\n        const labelledBy = hasLabel ? `${this.ids}-${labelSuffix}` : nothing;\n        return html`\n            ${this.renderLabel()}\n            <button\n                class=\"button\"\n                type=\"button\"\n                role=\"switch\"\n                id=${this.ids}\n                aria-checked=${this.checked ? 'true' : 'false'}\n                aria-labelledby=${labelledBy}\n                aria-readonly=${this.readonly ? 'true' : nothing}\n                ?disabled=${this.isDisabled}\n                @click=${this.onClick}\n            ></button>\n            <label class=\"row\" part=\"base\" for=${this.ids}>\n                <span class=\"switch ${this.checked ? 'checked' : ''}\" part=\"switch\" aria-hidden=\"true\">\n                    <span class=\"handle\" part=\"handle\">\n                        <svg class=\"check\" viewBox=\"0 0 6 5\" aria-hidden=\"true\"><path d=\"M2.2 2.7 4.7 0l.9.9L2.2 4.6 0 2.4l.9-.9z\" /></svg>\n                    </span>\n                </span>\n                <span class=\"text\" part=\"text\" id=\"${this.ids}-text\" aria-hidden=${this.hideLabel ? nothing : 'true'}>${this.sideText}</span>\n            </label>\n        `;\n    }\n}\n\ndeclare global {\n    interface HTMLElementTagNameMap {\n        'ui-toggle': UiToggle;\n    }\n}\n"
    }
  ]
}
