All files / ui/themes/src theme-loader.ts

0% Statements 0/61
0% Branches 0/5
0% Functions 0/6
0% Lines 0/60

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                                                                                                                                                                                                                             
import { getTheme, type ThemeDefinition } from '@buzztrail/shared-themes';
 
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);
  }
}
 
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);
  Iif (!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);
  Iif (theme.typography.heading) {
    root.style.setProperty('--bt-font-family-heading', theme.typography.heading);
  }
  
  // Inject font sizes
  Iif (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
  Iif (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
  Iif (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);
}