import type { ThemeDefinition } from '@buzztrail/shared-themes/types'; import { getTheme } from '@buzztrail/shared-themes/registry'; export function injectCssVars(vars: Record): void { const root = document.documentElement; for (const [key, value] of Object.entries(vars)) { root.style.setProperty(key, value); } } function inferFormat(src: string): string { if (/\.woff2($|\?)/.test(src)) return 'woff2'; if (/\.woff($|\?)/.test(src)) return 'woff'; if (/\.ttf($|\?)/.test(src)) return 'truetype'; return 'woff2'; } export function injectThemeFonts(theme: ThemeDefinition): void { if (!theme.fonts?.files) return; // Wait for document.head to be available if (!document.head) { console.warn('[Chatbot][Theme] Document head not available, skipping font injection'); return; } const styleId = `bt-theme-fonts-${theme.name}`; if (document.getElementById(styleId)) return; // Load Google Fonts for Inter (replaces Aeonik) const googleFontsId = 'bt-google-fonts-inter'; if (!document.getElementById(googleFontsId)) { const googleFontsLink = document.createElement('link'); googleFontsLink.id = googleFontsId; googleFontsLink.rel = 'stylesheet'; googleFontsLink.href = 'https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap'; googleFontsLink.onerror = (): void => console.warn('[Chatbot][Theme] Failed to load Google Fonts'); document.head.appendChild(googleFontsLink); } // Separate CSS files from font files const cssFiles = theme.fonts.files.filter(font => font.src.includes('.css')); const fontFiles = theme.fonts.files.filter(font => !font.src.includes('.css')); // Inject CSS files (like Font Awesome) cssFiles.forEach(font => { const linkId = `bt-theme-css-${theme.name}-${font.family}-${font.weight}`; if (document.getElementById(linkId)) return; const link = document.createElement('link'); link.id = linkId; link.rel = 'stylesheet'; link.href = font.src; link.onerror = (): void => console.warn(`[Chatbot][Theme] Failed to load CSS: ${font.src}`); try { document.head.appendChild(link); } catch (error) { console.error('[Chatbot][Theme] Failed to inject CSS:', error); } }); // Inject font files if (fontFiles.length === 0) { return; } const style = document.createElement('style'); style.id = styleId; // Filter out fonts that might not be available in the current context const availableFonts = fontFiles.filter(font => { // Skip fonts with relative paths that might not resolve in injected context if (font.src.startsWith('/shared/') || font.src.startsWith('./')) { return false; } return true; }); if (availableFonts.length === 0) { return; } style.textContent = availableFonts .map((f) => { const fmt = inferFormat(f.src); return `@font-face { font-family: '${f.family}'; src: url('${f.src}') format('${fmt}'); font-weight: ${f.weight}; font-style: ${f.style ?? 'normal'}; font-display: swap; }`; }) .join('\n'); try { document.head.appendChild(style); } catch (error) { console.error('[Chatbot][Theme] Failed to inject fonts:', error); } } export function applyTheme(name?: string): ThemeDefinition { const theme = getTheme(name); // Ensure document is ready before applying theme if (!document.documentElement) { console.warn('[Chatbot][Theme] Document not ready, deferring theme application'); setTimeout(() => applyTheme(name), 100); return theme; } // Inject main CSS variables const mainVars = { '--bt-font-base': theme.typography.base, '--bt-font-heading': theme.typography.heading ?? theme.typography.base, ...theme.cssVars, }; injectCssVars(mainVars); // Inject spacing variables for (const [k, v] of Object.entries(theme.spacing)) { document.documentElement.style.setProperty(`--bt-space-${k}`, v); } // Inject radii variables for (const [k, v] of Object.entries(theme.radii)) { document.documentElement.style.setProperty(`--bt-radius-${k}`, v); } // Inject border variables for (const [k, v] of Object.entries(theme.borders.widths)) { document.documentElement.style.setProperty(`--bt-border-${k}`, v); } document.documentElement.style.setProperty('--bt-border-color', theme.borders.color); document.documentElement.style.setProperty('--bt-focus-ring', theme.borders.focusRing); // Inject control variables document.documentElement.style.setProperty('--bt-control-h-sm', theme.controls.heights.sm); document.documentElement.style.setProperty('--bt-control-h-md', theme.controls.heights.md); document.documentElement.style.setProperty('--bt-control-h-lg', theme.controls.heights.lg); document.documentElement.style.setProperty('--bt-gap-sm', theme.controls.gaps.sm); document.documentElement.style.setProperty('--bt-gap-md', theme.controls.gaps.md); document.documentElement.style.setProperty('--bt-gap-lg', theme.controls.gaps.lg); document.documentElement.style.setProperty('--bt-icon-sm', theme.controls.icon.sm); document.documentElement.style.setProperty('--bt-icon-md', theme.controls.icon.md); document.documentElement.style.setProperty('--bt-icon-lg', theme.controls.icon.lg); injectThemeFonts(theme); return theme; }