All files / shared/styles/src theme-loader.ts

0% Statements 0/91
0% Branches 0/18
0% Functions 0/12
0% Lines 0/80

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150                                                                                                                                                                                                                                                                                                           
import type { ThemeDefinition } from '../themes/types';
import { getTheme } from '../themes/registry';
 
export function injectCssVars(vars: Record<string, string>): void {
  const root = document.documentElement;
  for (const [key, value] of Object.entries(vars)) {
    root.style.setProperty(key, value);
  }
}
 
function inferFormat(src: string): string {
  Iif (/\.woff2($|\?)/.test(src)) return 'woff2';
  Iif (/\.woff($|\?)/.test(src)) return 'woff';
  Iif (/\.ttf($|\?)/.test(src)) return 'truetype';
  return 'woff2';
}
 
export function injectThemeFonts(theme: ThemeDefinition): void {
  Iif (!theme.fonts?.files) return;
  
  // Wait for document.head to be available
  Iif (!document.head) {
    console.warn('[Chatbot][Theme] Document head not available, skipping font injection');
    return;
  }
  
  const styleId = `bt-theme-fonts-${theme.name}`;
  Iif (document.getElementById(styleId)) return;
  
  // Load Google Fonts for Inter (replaces Aeonik)
  const googleFontsId = 'bt-google-fonts-inter';
  Iif (!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}`;
    Iif (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
  Iif (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
    Iif (font.src.startsWith('/shared/') || font.src.startsWith('./')) {
      return false;
    }
    return true;
  });
  
  Iif (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
  Iif (!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;
}