Files
codex-proxy-rs/frontend/src/theme/runtime/browser.ts
T
ZyyandCodex 1d862922db refactor(theme): rebuild runtime token architecture
Derive semantic and component tokens from structured appearance seeds, align visual consumers and Tailwind v4 base utilities, and keep popover arrows matched to their adjacent surfaces across placement changes.

Co-authored-by: Codex <noreply@openai.com>
2026-08-29 22:14:57 +08:00

37 lines
1.2 KiB
TypeScript

// @env browser
import type { ResolvedTheme } from '../types'
const RUNTIME_THEME_STYLE_ID = 'cp-runtime-theme'
export function applyResolvedTheme(root: HTMLElement, theme: ResolvedTheme): void {
root.dataset.theme = theme.name
root.dataset.themeColor = theme.colorId
const style = ensureRuntimeThemeStyle(root.ownerDocument)
style.dataset.theme = theme.name
style.dataset.themeColor = theme.colorId
style.textContent = serializeTheme(theme)
}
function ensureRuntimeThemeStyle(document: Document): HTMLStyleElement {
const existing = document.getElementById(RUNTIME_THEME_STYLE_ID)
if (existing?.tagName === 'STYLE')
return existing as HTMLStyleElement
existing?.remove()
const style = document.createElement('style')
style.id = RUNTIME_THEME_STYLE_ID
style.setAttribute('data-cp-runtime-theme', '')
document.head.append(style)
return style
}
function serializeTheme(theme: ResolvedTheme): string {
const declarations = Object.entries(theme.tokens)
.map(([name, value]) => ` ${name}: ${value};`)
.join('\n')
return `:root[data-theme='${theme.name}'][data-theme-color='${theme.colorId}'] {\n color-scheme: ${theme.name};\n${declarations}\n}\n`
}