{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "theme-switcher",
  "type": "registry:ui",
  "title": "<ui-theme-switcher>",
  "description": "Lets a person pick the light, dark or system theme (no Carbon counterpart; the user chose an icon content switcher, as the theme setting of IBM Cloud). The choice shows on `<html data-theme>` as `light-theme` / `dark-theme` (white / g100 by default; system follows the OS while it changes) and is remembered in `localStorage` under `storage-key` (`\"\"` = not remembered). It starts on the remembered mode, else `value`, else what `<html data-theme>` shows. Picking one fires `ui-theme-change` { mode, theme }; every switcher on the page, and in other tabs, follows. Setting `value` applies and remembers it too (e.g. from a profile saved on the server). Put `themeScript()` from the core in `<head>` so a reload paints the remembered theme at once. 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/content-switcher",
    "@kanok-ui/core-theme",
    "@kanok-ui/shared-element",
    "@kanok-ui/shared-localize",
    "@kanok-ui/tooltip",
    "@kanok-ui/tokens"
  ],
  "files": [
    {
      "path": "src/primitives/theme-switcher/theme-switcher.ts",
      "type": "registry:ui",
      "target": "components/ui/primitives/theme-switcher/theme-switcher.ts",
      "content": "import { html, css, svg, type PropertyValues } from 'lit';\nimport { customElement, property } from 'lit/decorators.js';\nimport { UiElement } from '../shared/element.ts';\nimport { LocalizeController } from '../shared/localize.ts';\nimport type { ContentSwitcherChangeDetail, ContentSwitcherSize } from '../content-switcher/content-switcher.ts';\nimport type { TooltipPlacement } from '../tooltip/tooltip.ts';\nimport '../content-switcher/content-switcher.ts';\nimport {\n    THEME_CHANGE_EVENT,\n    THEME_MODES,\n    THEME_STORAGE_KEY,\n    applyThemeMode,\n    initialThemeMode,\n    isThemeMode,\n    readThemeMode,\n    saveThemeMode,\n    themePair,\n    watchSystemTheme,\n    type CarbonTheme,\n    type ThemeChangeDetail,\n    type ThemeMode,\n} from '../../core/theme.ts';\n\nexport type { CarbonTheme, ThemeChangeDetail, ThemeMode } from '../../core/theme.ts';\n\n/** Carbon icons Light, Asleep (16) and Screen (32, drawn at 16). */\nconst ICONS: Record<ThemeMode, ReturnType<typeof svg>> = {\n    light: svg`<svg slot=\"icon\" viewBox=\"0 0 16 16\" aria-hidden=\"true\"><path d=\"M7.5 1h1v2.5h-1z\"/><path d=\"M10.8 3.4h2.5v1h-2.5z\" transform=\"rotate(-45 12.041 3.923)\"/><path d=\"M12.5 7.5H15v1h-2.5z\"/><path d=\"M11.6 10.8h1v2.5h-1z\" transform=\"rotate(-45 12.075 12.04)\"/><path d=\"M7.5 12.5h1V15h-1z\"/><path d=\"M2.7 11.6h2.5v1H2.7z\" transform=\"rotate(-45 3.96 12.078)\"/><path d=\"M1 7.5h2.5v1H1z\"/><path d=\"M3.4 2.7h1v2.5h-1z\" transform=\"rotate(-45 3.925 3.961)\"/><path d=\"M8 6c1.1 0 2 .9 2 2s-.9 2-2 2-2-.9-2-2 .9-2 2-2m0-1C6.3 5 5 6.3 5 8s1.3 3 3 3 3-1.3 3-3-1.3-3-3-3z\"/></svg>`,\n    dark: svg`<svg slot=\"icon\" viewBox=\"0 0 16 16\" aria-hidden=\"true\"><path d=\"M7.2 2.3c-1 4.4 1.7 8.7 6.1 9.8h.2c-1.1 1.2-2.7 1.8-4.3 1.8h-.2C5.6 13.8 3 11 3.2 7.7c0-2.4 1.6-4.6 4-5.4M8 1C4.1 1.6 1.5 5.3 2.1 9.1c.6 3.3 3.4 5.8 6.8 5.9h.3c2.3 0 4.4-1.1 5.8-3 .2-.2.1-.6-.1-.7-.1-.1-.2-.1-.3-.1-3.9-.3-6.7-3.8-6.4-7.6.1-.6.2-1.2.4-1.8.1-.3 0-.6-.3-.7C8.1 1 8.1 1 8 1z\"/></svg>`,\n    system: svg`<svg slot=\"icon\" viewBox=\"0 0 32 32\" aria-hidden=\"true\"><path d=\"M28 4H4a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h8v4H8v2h16v-2h-4v-4h8a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2ZM18 28h-4v-4h4Zm10-6H4V6h24Z\"/></svg>`,\n};\n\n/**\n * Lets a person pick the light, dark or system theme (no Carbon counterpart; the user chose an icon\n * content switcher, as the theme setting of IBM Cloud). The choice shows on `<html data-theme>` as\n * `light-theme` / `dark-theme` (white / g100 by default; system follows the OS while it changes) and\n * is remembered in `localStorage` under `storage-key` (`\"\"` = not remembered). It starts on the\n * remembered mode, else `value`, else what `<html data-theme>` shows. Picking one fires\n * `ui-theme-change` { mode, theme }; every switcher on the page, and in other tabs, follows. Setting\n * `value` applies and remembers it too (e.g. from a profile saved on the server). Put\n * `themeScript()` from the core in `<head>` so a reload paints the remembered theme at once.\n *\n * @csspart switcher - The `<ui-content-switcher>`\n * @csspart item - Each `<ui-content-switcher-item>`\n * @fires ui-theme-change - The person picked a mode: { mode, theme }\n */\n@customElement('ui-theme-switcher')\nexport class UiThemeSwitcher extends UiElement {\n    static override readonly styles = css`\n        :host {\n            display: inline-block;\n            vertical-align: top;\n        }\n    `;\n\n    /** The mode shown: light, dark or system. */\n    @property({ reflect: true }) value?: ThemeMode;\n\n    /** The Carbon theme of \"light\" (white or g10). */\n    @property({ attribute: 'light-theme' }) lightTheme: CarbonTheme = 'white';\n\n    /** The Carbon theme of \"dark\" (g90 or g100). */\n    @property({ attribute: 'dark-theme' }) darkTheme: CarbonTheme = 'g100';\n\n    /** The `localStorage` key; empty = don't remember. */\n    @property({ attribute: 'storage-key' }) storageKey = THEME_STORAGE_KEY;\n\n    @property({ reflect: true }) size: ContentSwitcherSize = 'md';\n\n    @property({ type: Boolean, attribute: 'low-contrast' }) lowContrast = false;\n\n    /** Where the icons' tooltips show (Carbon `IconSwitch` `align`). */\n    @property({ attribute: 'tooltip-placement' }) tooltipPlacement: TooltipPlacement = 'top';\n\n    /** Names the group; defaults to the translation of \"Theme\". */\n    @property() label = '';\n\n    private readonly localize = new LocalizeController(this);\n    private stopWatching?: () => void;\n    /** Set while the value comes from the page itself (start, another switcher, another tab): shown, not saved again. */\n    private quiet = true;\n\n    override connectedCallback() {\n        super.connectedCallback();\n        const start = initialThemeMode(this.storageKey, this.value);\n        if (start !== this.value) {\n            this.quiet = true;\n            this.value = start;\n        }\n        this.stopWatching = watchSystemTheme(() => {\n            if (this.value === 'system') this.apply();\n        });\n        document.addEventListener(THEME_CHANGE_EVENT, this.onPageChange);\n        globalThis.addEventListener('storage', this.onStorage);\n    }\n\n    override disconnectedCallback() {\n        super.disconnectedCallback();\n        this.stopWatching?.();\n        document.removeEventListener(THEME_CHANGE_EVENT, this.onPageChange);\n        globalThis.removeEventListener('storage', this.onStorage);\n    }\n\n    protected override updated(changed: PropertyValues<this>) {\n        super.updated(changed);\n        if (!changed.has('value') && !changed.has('lightTheme') && !changed.has('darkTheme')) return;\n        this.apply();\n        if (!this.quiet && changed.has('value') && this.value) saveThemeMode(this.storageKey, this.value);\n        this.quiet = false;\n    }\n\n    private apply() {\n        return this.value ? applyThemeMode(this.value, themePair(this.lightTheme, this.darkTheme)) : undefined;\n    }\n\n    /** Follow another switcher on this page. */\n    private readonly onPageChange = (event: Event) => {\n        const mode = (event as CustomEvent<ThemeChangeDetail>).detail?.mode;\n        if (event.target === this || !isThemeMode(mode) || mode === this.value) return;\n        this.quiet = true;\n        this.value = mode;\n    };\n\n    /** Follow a pick made in another tab. */\n    private readonly onStorage = (event: StorageEvent) => {\n        if (!this.storageKey || event.key !== this.storageKey) return;\n        const mode = readThemeMode(this.storageKey);\n        if (!mode || mode === this.value) return;\n        this.quiet = true;\n        this.value = mode;\n    };\n\n    private onChange(event: CustomEvent<ContentSwitcherChangeDetail>) {\n        event.stopPropagation();\n        const mode = event.detail.name;\n        if (!isThemeMode(mode)) return;\n        // saved before the event, so a listener reading the storage (a React switcher) sees the new mode\n        this.quiet = true;\n        this.value = mode;\n        saveThemeMode(this.storageKey, mode);\n        const theme = applyThemeMode(mode, themePair(this.lightTheme, this.darkTheme));\n        /** @internal the manifest takes this event from the class's `@fires ui-theme-change`, not from the constant's name */\n        this.dispatchEvent(new CustomEvent<ThemeChangeDetail>(THEME_CHANGE_EVENT, { detail: { mode, theme }, bubbles: true, composed: true }));\n    }\n\n    override render() {\n        const t = this.localize.t.common;\n        const names: Record<ThemeMode, string> = { light: t.themeLight, dark: t.themeDark, system: t.themeSystem };\n        return html`<ui-content-switcher\n            part=\"switcher\"\n            aria-label=${this.label || t.theme}\n            size=${this.size}\n            ?low-contrast=${this.lowContrast}\n            selected-index=${Math.max(0, THEME_MODES.indexOf(this.value ?? 'light'))}\n            @ui-change=${this.onChange}\n        >\n            ${THEME_MODES.map(\n                (mode) => html`<ui-content-switcher-item part=\"item\" icon-only name=${mode} label=${names[mode]} tooltip-placement=${this.tooltipPlacement}>${ICONS[mode]}</ui-content-switcher-item>`,\n            )}\n        </ui-content-switcher>`;\n    }\n}\n\nexport interface UiThemeSwitcherEventMap extends HTMLElementEventMap {\n    'ui-theme-change': CustomEvent<ThemeChangeDetail>;\n}\n\n/** Typed listeners for UiThemeSwitcher's events (declaration merging with the class). */\nexport interface UiThemeSwitcher {\n    addEventListener<K extends keyof UiThemeSwitcherEventMap>(type: K, listener: (this: UiThemeSwitcher, event: UiThemeSwitcherEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions): void;\n    addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;\n    removeEventListener<K extends keyof UiThemeSwitcherEventMap>(type: K, listener: (this: UiThemeSwitcher, event: UiThemeSwitcherEventMap[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-theme-switcher': UiThemeSwitcher;\n    }\n}\n"
    }
  ]
}
