import { getTheme, type ThemeDefinition } from '@buzztrail/shared-themes'; export function injectCssVars(vars: Record): void { const root = document.documentElement; for (const [key, value] of Object.entries(vars)) { root.style.setProperty(key, value); } } export function removeCssVars(vars: string[]): void { const root = document.documentElement; for (const key of vars) { root.style.removeProperty(key); } } export function applyTheme(themeName: string): void { const theme = getTheme(themeName); if (!theme) { console.warn(`Theme "${themeName}" not found`); return; } applyThemeDefinition(theme); } export function applyThemeDefinition(theme: ThemeDefinition): void { const root = document.documentElement; // Set Radix UI theme attributes root.setAttribute('data-theme', theme.radix.appearance); root.setAttribute('data-accent-color', theme.radix.accentColor); root.setAttribute('data-gray-color', theme.radix.grayColor); root.setAttribute('data-radius', theme.radix.radius); // Inject CSS variables injectCssVars(theme.cssVars); // Inject typography variables root.style.setProperty('--bt-font-family', theme.typography.base); if (theme.typography.heading) { root.style.setProperty('--bt-font-family-heading', theme.typography.heading); } // Inject font sizes if (theme.typography.sizes) { for (const [k, v] of Object.entries(theme.typography.sizes)) { root.style.setProperty(`--bt-font-size-${k}`, String(v)); } } // Inject line heights if (theme.typography.lineHeights) { for (const [k, v] of Object.entries(theme.typography.lineHeights)) { root.style.setProperty(`--bt-line-height-${k}`, String(v)); } } // Inject font weights if (theme.typography.weights) { for (const [k, v] of Object.entries(theme.typography.weights)) { root.style.setProperty(`--bt-font-weight-${k}`, String(v)); } } // Inject spacing variables for (const [k, v] of Object.entries(theme.spacing)) { root.style.setProperty(`--bt-space-${k}`, String(v)); } // Inject radii variables for (const [k, v] of Object.entries(theme.radii)) { root.style.setProperty(`--bt-radius-${k}`, String(v)); } // Inject border variables for (const [k, v] of Object.entries(theme.borders.widths)) { root.style.setProperty(`--bt-border-${k}`, String(v)); } root.style.setProperty('--bt-border-color', theme.borders.color); root.style.setProperty('--bt-focus-ring', theme.borders.focusRing); // Inject control variables root.style.setProperty('--bt-control-h-sm', theme.controls.heights.sm); root.style.setProperty('--bt-control-h-md', theme.controls.heights.md); root.style.setProperty('--bt-control-h-lg', theme.controls.heights.lg); root.style.setProperty('--bt-gap-sm', theme.controls.gaps.sm); root.style.setProperty('--bt-gap-md', theme.controls.gaps.md); root.style.setProperty('--bt-gap-lg', theme.controls.gaps.lg); root.style.setProperty('--bt-icon-sm', theme.controls.icon.sm); root.style.setProperty('--bt-icon-md', theme.controls.icon.md); root.style.setProperty('--bt-icon-lg', theme.controls.icon.lg); } export function clearTheme(): void { const root = document.documentElement; // Remove Radix UI theme attributes root.removeAttribute('data-theme'); root.removeAttribute('data-accent-color'); root.removeAttribute('data-gray-color'); root.removeAttribute('data-radius'); // Remove all BuzzTrail CSS variables const computedStyle = getComputedStyle(root); const cssVars = Array.from(computedStyle).filter(prop => prop.startsWith('--bt-')); removeCssVars(cssVars); }