All files / src/theme index.ts

93.75% Statements 15/16
81.81% Branches 9/11
66.66% Functions 2/3
100% Lines 15/15

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            1x   1x 3x 3x   3x   3x 1x     1x 1x     1x 1x 1x 1x 1x           3x    
import { getGlobalConfig, getEventConfig } from '../store'
 
/**
 * Loads and optionally applies DB-overridden CSS variables.
 * @param apply If true, applies variables to document root.
 */
let appliedThemeKeys: string[] = []
 
export const loadTheme = (apply = false): Record<string, string> => {
    const globalTheme = getGlobalConfig()?.theme?.variables || {}
    const eventTheme = getEventConfig()?.theme?.variables || {}
 
    const variables = { ...globalTheme, ...eventTheme }
 
    if (apply) {
        const root = document.documentElement
 
        // 1. Clear previously applied keys to restore CSS defaults
        appliedThemeKeys.forEach((key) => root.style.removeProperty(key))
        appliedThemeKeys = []
 
        // 2. Apply new keys if they exist
        Eif (variables) {
            Object.entries(variables).forEach(([key, value]) => {
                Eif (value) {
                    root.style.setProperty(key, String(value))
                    appliedThemeKeys.push(key)
                }
            })
        }
    }
 
    return variables
}