mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-06 16:06:08 +00:00
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>
37 lines
1.2 KiB
TypeScript
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`
|
|
}
|