{"version":3,"file":"apply-theme.cjs","names":[],"sources":["../../src/theme/apply-theme.ts"],"sourcesContent":["/**\n * Runtime installer for a {@link GeneratedTheme}.\n *\n * A generated theme is just CSS text, so it needs a `<style>` element to take\n * effect. This owns that element by id, which keeps re-applying idempotent — a\n * theme picker can call `applyTheme` on every click without stacking dead\n * stylesheets in `<head>`.\n */\nimport type { GeneratedTheme } from \"./create-theme\";\n\n/** Options for {@link applyTheme}. */\nexport interface ApplyThemeOptions {\n    /**\n     * `id` of the managed `<style>` element. Reusing the same id replaces the\n     * previous theme; a distinct id lets two themes coexist (e.g. a scoped\n     * preview under its own selector). Default `\"tempest-theme\"`.\n     */\n    id?: string;\n    /**\n     * Node the `<style>` is appended to. Default `document.head`. Pass a shadow\n     * root to theme a single web component.\n     */\n    target?: Document | ShadowRoot | HTMLElement;\n}\n\n/** The default `<style>` element id owned by {@link applyTheme}. */\nexport const THEME_STYLE_ID = \"tempest-theme\";\n\nfunction resolveContainer(target: ApplyThemeOptions[\"target\"]): ParentNode | null {\n    if (!target) {\n        return typeof document === \"undefined\" ? null : document.head;\n    }\n    if (typeof Document !== \"undefined\" && target instanceof Document) {\n        return target.head;\n    }\n    return target;\n}\n\n/**\n * Install a generated theme (or raw CSS) into the document.\n *\n * Safe to call outside a browser: with no `document` it is a no-op returning a\n * no-op disposer, so app bootstrap code does not need a `typeof window` guard.\n *\n * @param theme - A {@link GeneratedTheme} from `createTheme`, or CSS text.\n * @param options - Style element id and mount target.\n * @returns A function that removes the injected `<style>` element.\n *\n * @example\n * ```ts\n * import { applyTheme, createTheme, themePresets } from \"tempest-react-sdk\";\n *\n * const dispose = applyTheme(createTheme(themePresets.violet));\n * // later: dispose();\n * ```\n */\nexport function applyTheme(\n    theme: GeneratedTheme | string,\n    options: ApplyThemeOptions = {},\n): () => void {\n    const { id = THEME_STYLE_ID, target } = options;\n    const container = resolveContainer(target);\n    if (!container || typeof document === \"undefined\") {\n        return () => {};\n    }\n\n    const css = typeof theme === \"string\" ? theme : theme.css;\n\n    let style = Array.from(container.childNodes).find(\n        (node): node is HTMLStyleElement => node instanceof HTMLStyleElement && node.id === id,\n    );\n\n    if (!style) {\n        style = document.createElement(\"style\");\n        style.id = id;\n        container.appendChild(style);\n    }\n\n    style.textContent = css;\n\n    return () => {\n        style?.remove();\n    };\n}\n\n/**\n * Read a token's computed value from an element (default: `<html>`).\n *\n * Useful to bridge CSS tokens into JS that cannot take a `var()` — canvas\n * drawing, chart libraries that set SVG attributes, `<meta name=\"theme-color\">`.\n *\n * @param name - Token name, with or without the leading `--`.\n * @param element - Element to resolve against. Omit for `document.documentElement`;\n *   an explicit `null` (a ref that has not attached yet) reads nothing rather than\n *   silently falling back to the root, whose value may be a different theme.\n * @returns The trimmed value, or `\"\"` when unset or outside a browser.\n */\nexport function readThemeToken(name: string, element?: Element | null): string {\n    if (typeof window === \"undefined\" || typeof document === \"undefined\") return \"\";\n    if (element === null) return \"\";\n    const target = element ?? document.documentElement;\n    if (!target) return \"\";\n    const property = name.startsWith(\"--\") ? name : `--${name}`;\n    return window.getComputedStyle(target).getPropertyValue(property).trim();\n}\n"],"mappings":"AA0BA,IAAa,EAAiB,gBAE9B,SAAS,EAAiB,EAAwD,CAO9E,OANK,EAGD,OAAO,SAAa,KAAe,aAAkB,SAC9C,EAAO,KAEX,EALI,OAAO,SAAa,IAAc,KAAO,SAAS,IAMjE,CAoBA,SAAgB,EACZ,EACA,EAA6B,CAAC,EACpB,CACV,GAAM,CAAE,KAAK,EAAgB,UAAW,EAClC,EAAY,EAAiB,CAAM,EACzC,GAAI,CAAC,GAAa,OAAO,SAAa,IAClC,UAAa,CAAC,EAGlB,IAAM,EAAM,OAAO,GAAU,SAAW,EAAQ,EAAM,IAElD,EAAQ,MAAM,KAAK,EAAU,UAAU,CAAC,CAAC,KACxC,GAAmC,aAAgB,kBAAoB,EAAK,KAAO,CACxF,EAUA,OARK,IACD,EAAQ,SAAS,cAAc,OAAO,EACtC,EAAM,GAAK,EACX,EAAU,YAAY,CAAK,GAG/B,EAAM,YAAc,MAEP,CACT,GAAO,OAAO,CAClB,CACJ,CAcA,SAAgB,EAAe,EAAc,EAAkC,CAE3E,GADI,OAAO,OAAW,KAAe,OAAO,SAAa,KACrD,IAAY,KAAM,MAAO,GAC7B,IAAM,EAAS,GAAW,SAAS,gBACnC,GAAI,CAAC,EAAQ,MAAO,GACpB,IAAM,EAAW,EAAK,WAAW,IAAI,EAAI,EAAO,KAAK,IACrD,OAAO,OAAO,iBAAiB,CAAM,CAAC,CAAC,iBAAiB,CAAQ,CAAC,CAAC,KAAK,CAC3E"}