{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "core-theme",
  "type": "registry:lib",
  "title": "core/theme",
  "description": "Framework-free behaviour shared by the ui-* components (theme); no DOM, no lit. 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": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "src/core/theme.ts",
      "type": "registry:lib",
      "target": "components/ui/core/theme.ts",
      "content": "/**\n * A person's theme choice — light, dark or the system's — turned into a Carbon theme on `<html\n * data-theme>` and remembered in `localStorage`. No Carbon counterpart: Carbon React has\n * `GlobalTheme` / `Theme` / `usePrefersDarkScheme` but no control to pick one. Framework-free; the\n * Lit `<ui-theme-switcher>` and the React `ThemeSwitcher` / `useThemeMode` share it.\n *\n * Two designs share the components (`<html data-design>`): Carbon, the default, and Kanok (kanok.css).\n * A mode stays light / dark / system in both; inside Kanok its Carbon theme shows as Kanok's own\n * (white, g10 → paper; g90, g100 → lacquer), so a switcher works unchanged.\n */\n\nexport type ThemeMode = 'light' | 'dark' | 'system';\nexport type CarbonTheme = 'white' | 'g10' | 'g90' | 'g100';\n/** The themes of the Kanok design (kanok.css): paper is light, lacquer dark. */\nexport type KanokTheme = 'paper' | 'lacquer';\n/** A theme of either design. */\nexport type Theme = CarbonTheme | KanokTheme;\n/** The designs: Carbon (tokens.css, the default) and Kanok (kanok.css, `<html data-design=\"kanok\">`). */\nexport type Design = 'carbon' | 'kanok';\n\n/** The Carbon themes a mode turns into. */\nexport interface ThemePair {\n    light: CarbonTheme;\n    dark: CarbonTheme;\n}\n\nexport const THEME_MODES: readonly ThemeMode[] = ['light', 'dark', 'system'];\nexport const CARBON_THEMES: readonly CarbonTheme[] = ['white', 'g10', 'g90', 'g100'];\n/** The `localStorage` key unless one is given. */\nexport const THEME_STORAGE_KEY = 'ui-theme';\n/** Like `data-theme=\"auto\"`: white, or g100 when dark. */\nexport const DEFAULT_THEMES: ThemePair = { light: 'white', dark: 'g100' };\nexport const DARK_QUERY = '(prefers-color-scheme: dark)';\n/** Fired (bubbling, composed) by a switcher when the person picks a mode; every switcher on the page follows it. */\nexport const THEME_CHANGE_EVENT = 'ui-theme-change';\n\nexport interface ThemeChangeDetail {\n    mode: ThemeMode;\n    theme: Theme;\n}\n\n/** The browser APIs used here, typed here: the core compiles without the DOM lib (tsconfig.core.json). Each is missing on a server. */\ninterface ThemeHost {\n    document?: { documentElement: { dataset: Record<string, string | undefined> } };\n    localStorage?: { getItem(key: string): string | null; setItem(key: string, value: string): void };\n    matchMedia?: (query: string) => { matches: boolean; addEventListener(type: 'change', listener: () => void): void; removeEventListener(type: 'change', listener: () => void): void };\n}\n\nconst host = globalThis as unknown as ThemeHost;\n\nexport const isThemeMode = (value: unknown): value is ThemeMode => THEME_MODES.includes(value as ThemeMode);\nexport const isCarbonTheme = (value: unknown): value is CarbonTheme => CARBON_THEMES.includes(value as CarbonTheme);\n\n/** The pair with anything that is not a Carbon theme put back to the default. */\nexport function themePair(light?: string | null, dark?: string | null): ThemePair {\n    return { light: isCarbonTheme(light) ? light : DEFAULT_THEMES.light, dark: isCarbonTheme(dark) ? dark : DEFAULT_THEMES.dark };\n}\n\n/** The Carbon theme a mode shows. */\nexport function themeOf(mode: ThemeMode, prefersDark: boolean, themes: ThemePair = DEFAULT_THEMES): CarbonTheme {\n    const dark = mode === 'dark' || (mode === 'system' && prefersDark);\n    return dark ? themes.dark : themes.light;\n}\n\n/** The mode a page's own `data-theme` stands for (`auto` = system); undefined for anything else. */\nexport function modeOf(theme: string | null | undefined): ThemeMode | undefined {\n    if (theme === 'auto') return 'system';\n    if (theme === 'white' || theme === 'g10' || theme === 'paper') return 'light';\n    if (theme === 'g90' || theme === 'g100' || theme === 'lacquer') return 'dark';\n    return undefined;\n}\n\n/** Whether the system asks for dark (false outside a browser). */\nexport function prefersDark(): boolean {\n    return !!host.matchMedia?.(DARK_QUERY).matches;\n}\n\n/** The remembered mode; undefined when none, `key` is empty, or storage is blocked. */\nexport function readThemeMode(key: string): ThemeMode | undefined {\n    if (!key) return undefined;\n    try {\n        const value = host.localStorage?.getItem(key);\n        return isThemeMode(value) ? value : undefined;\n    } catch {\n        return undefined;\n    }\n}\n\n/** Remembers the mode under `key` (nothing when `key` is empty or storage is blocked). */\nexport function saveThemeMode(key: string, mode: ThemeMode) {\n    if (!key) return;\n    try {\n        host.localStorage?.setItem(key, mode);\n    } catch {\n        // private mode / blocked storage: the choice lasts until the page closes\n    }\n}\n\n/** The mode a switcher starts on: the remembered one, else the given one, else what `<html data-theme>` shows, else light. */\nexport function initialThemeMode(key: string, given?: string | null): ThemeMode {\n    return readThemeMode(key) ?? (isThemeMode(given) ? given : undefined) ?? modeOf(host.document?.documentElement.dataset.theme) ?? 'light';\n}\n\n/** Shows the mode's theme on `<html data-theme>` (as the page's design shows it) and returns it. */\nexport function applyThemeMode(mode: ThemeMode, themes: ThemePair = DEFAULT_THEMES): Theme {\n    const theme = themeIn(designOf(), themeOf(mode, prefersDark(), themes));\n    const root = host.document?.documentElement;\n    if (root && root.dataset.theme !== theme) root.dataset.theme = theme;\n    return theme;\n}\n\n/** Calls `onChange` when the system switches between light and dark; returns the stop function. */\nexport function watchSystemTheme(onChange: () => void): () => void {\n    const query = host.matchMedia?.(DARK_QUERY);\n    if (!query) return () => {};\n    query.addEventListener('change', onChange);\n    return () => query.removeEventListener('change', onChange);\n}\n\n/**\n * A script for `<head>`, before the styles load, that shows the remembered theme on the first paint\n * (no flash of the light theme). It does nothing when nothing is remembered, so `<html data-theme>`\n * stays the page's own default.\n */\nexport function themeScript(options: { storageKey?: string; lightTheme?: string; darkTheme?: string } = {}): string {\n    const { light, dark } = themePair(options.lightTheme, options.darkTheme);\n    // `<` written as an escape so the key cannot close the <script>\n    const key = JSON.stringify(options.storageKey ?? THEME_STORAGE_KEY).replaceAll('<', String.raw`\\u003c`);\n    // inside Kanok (<html data-design=\"kanok\">, set first by designScript) the theme is Kanok's own\n    return `(function(){try{var m=localStorage.getItem(${key});if(m!==\"light\"&&m!==\"dark\"&&m!==\"system\")return;var d=m===\"dark\"||(m===\"system\"&&matchMedia(${JSON.stringify(DARK_QUERY)}).matches),r=document.documentElement,t=d?\"${dark}\":\"${light}\";if(r.getAttribute(\"data-design\")===\"kanok\")t=d?\"lacquer\":\"paper\";r.setAttribute(\"data-theme\",t)}catch(e){}})();`;\n}\n\n// ---- the design (Carbon or Kanok) ----\n\nexport const DESIGNS: readonly Design[] = ['carbon', 'kanok'];\n/** The `localStorage` key of a page's design choice unless one is given. */\nexport const DESIGN_STORAGE_KEY = 'ui-design';\n/** Fired on `document` by `applyDesign` with `{ design }`. */\nexport const DESIGN_CHANGE_EVENT = 'ui-design-change';\n\nexport interface DesignChangeDetail {\n    design: Design;\n}\n\nexport const isDesign = (value: unknown): value is Design => DESIGNS.includes(value as Design);\n\ninterface DesignHost {\n    document?: {\n        documentElement: { dataset: Record<string, string | undefined>; removeAttribute(name: string): void };\n        dispatchEvent(event: unknown): boolean;\n    };\n    CustomEvent?: new (type: string, init: { detail: DesignChangeDetail }) => unknown;\n}\n\nconst designHost = globalThis as unknown as DesignHost;\n\n/** The design `<html data-design>` names; Carbon when none (or outside a browser). */\nexport function designOf(): Design {\n    const value = designHost.document?.documentElement.dataset.design;\n    return isDesign(value) ? value : 'carbon';\n}\n\n/**\n * A theme as `design` shows it: Kanok has one light (paper) and one dark (lacquer) theme, Carbon four.\n * Anything else (`auto`, a theme of the same design) comes back unchanged.\n */\nexport function themeIn(design: Design, theme: Theme): Theme;\nexport function themeIn(design: Design, theme: string): string;\nexport function themeIn(design: Design, theme: string): string {\n    if (design === 'kanok') {\n        if (theme === 'white' || theme === 'g10') return 'paper';\n        if (theme === 'g90' || theme === 'g100') return 'lacquer';\n    } else {\n        if (theme === 'paper') return 'white';\n        if (theme === 'lacquer') return 'g100';\n    }\n    return theme;\n}\n\n/** The remembered design; undefined when none, `key` is empty, or storage is blocked. */\nexport function readDesign(key: string = DESIGN_STORAGE_KEY): Design | undefined {\n    if (!key) return undefined;\n    try {\n        const value = host.localStorage?.getItem(key);\n        return isDesign(value) ? value : undefined;\n    } catch {\n        return undefined;\n    }\n}\n\n/** Remembers the design under `key` (nothing when `key` is empty or storage is blocked). */\nexport function saveDesign(design: Design, key: string = DESIGN_STORAGE_KEY) {\n    if (!key) return;\n    try {\n        host.localStorage?.setItem(key, design);\n    } catch {\n        // private mode / blocked storage: the choice lasts until the page closes\n    }\n}\n\n/**\n * Shows `design` on `<html>`: `data-design=\"kanok\"`, or no attribute for Carbon. The page's theme moves\n * to the same mode in the new design (white ↔ paper, g100 ↔ lacquer), and `ui-design-change` fires.\n */\nexport function applyDesign(design: Design) {\n    const doc = designHost.document;\n    if (!doc) return;\n    const root = doc.documentElement;\n    if (design === 'kanok') root.dataset.design = 'kanok';\n    else root.removeAttribute('data-design');\n    if (root.dataset.theme) root.dataset.theme = themeIn(design, root.dataset.theme);\n    if (designHost.CustomEvent) doc.dispatchEvent(new designHost.CustomEvent(DESIGN_CHANGE_EVENT, { detail: { design } }));\n}\n\n/**\n * A script for `<head>`, before themeScript and the styles: shows the design asked for in the URL\n * (`?design=kanok`, remembered) or the remembered one on the first paint. Nothing when neither is.\n */\nexport function designScript(options: { storageKey?: string; param?: string } = {}): string {\n    const key = JSON.stringify(options.storageKey ?? DESIGN_STORAGE_KEY).replaceAll('<', String.raw`\\u003c`);\n    const param = JSON.stringify(options.param ?? 'design').replaceAll('<', String.raw`\\u003c`);\n    return `(function(){try{var q=new URLSearchParams(location.search).get(${param}),d=q;if(q===\"kanok\"||q===\"carbon\")localStorage.setItem(${key},q);else d=localStorage.getItem(${key});if(d===\"kanok\")document.documentElement.setAttribute(\"data-design\",\"kanok\")}catch(e){}})();`;\n}\n"
    }
  ]
}
