All files / src/utils/theme theme.ts

100% Statements 44/44
100% Branches 15/15
100% Functions 8/8
100% Lines 44/44

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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199                                                                          95x   10x                                                                                                                                       4x 4x 4x   4x   4x                 10x   10x 150x       10x 10x 18x     10x   10x 10x 4x   10x 10x 2x       95x 10x         10x   10x 8x 8x   8x 1x     8x   2x 3x 1x 1x     2x   2x             8x   3x 4x 1x 1x     3x         10x   10x     95x  
import { IDictionary } from '@zeedhi/core';
 
interface ThemeBase {
	defaultTheme: string;
	variations: {
		colors: string[];
		lighten: number;
		darken: number;
	};
	themes: IDictionary<{
		dark?: boolean;
		colors: IDictionary<string>;
	}>;
}
 
/**
 * Interface for the default Zeedhi Theme definition
 */
interface IDefaultTheme extends ThemeBase {
	minifyTheme: (css: string) => string;
	zeedhi: IDictionary<IDictionary<string>>;
}
 
/**
 * Interface for the Theme defined in the app
 */
export interface ITheme extends Partial<IDefaultTheme> {
	cspNonce?: string;
}
 
/**
 * Interface for the Theme to be returned by initTheme
 */
interface IMaterialTheme extends ThemeBase {
	cspNonce?: string;
}
 
const defaultThemes: IDefaultTheme = {
	defaultTheme: 'light',
	minifyTheme: (css: string) => css.replace(/[\r\n|\r|\n]/g, ''),
	variations: {
		colors: ['primary', 'secondary', 'grey'],
		darken: 4,
		lighten: 5,
	},
	themes: {
		light: {
			colors: {
				primary: '#772583',
				secondary: '#A0A0A0',
				background: '#FFFFFF',
				surface: '#FFFFFF',
				accent: '#A08CA0',
				error: '#FF5252',
				info: '#2196F3',
				success: '#4CAF50',
				warning: '#FFC107',
				anchor: '#772583',
				grey: '#6A6A6A',
			},
		},
		dark: {
			colors: {
				primary: '#AE5AB9',
				secondary: '#A0A0A0',
				background: '#121212',
				surface: '#1E1E1E',
				accent: '#FF4081',
				error: '#FF5252',
				info: '#2196F3',
				success: '#4CAF50',
				warning: '#FB8C00',
				anchor: '#772583',
				grey: '#6A6A6A',
			},
		},
	},
	zeedhi: {
		light: {
			'scrollbar-track': '#F0F0F0',
			'scrollbar-thumb': '#D4D4D4',
		},
		dark: {
			'scrollbar-track': '#252525',
			'scrollbar-thumb': '#6A6A6A',
		},
		variables: {
			'default-padding': '16px',
			'font-headline-size': '20px',
			'font-headline-weight': '400',
			'font-title-size': '18px',
			'font-title-weight': '400',
			'font-body1-size': '13px',
			'font-body1-weight': '400',
			'font-body2-size': '13px',
			'font-body2-weight': '500',
			'font-body3-size': '14px',
			'font-body3-weight': '400',
			'font-body4-size': '14px',
			'font-body4-weight': '500',
			'font-caption-size': '11px',
			'font-caption-weight': '400',
		},
	},
};
 
function createStyleElement() {
	const styleEl = document.createElement('style');
	styleEl.type = 'text/css';
	styleEl.id = 'zeedhi-theme-stylesheet';
 
	document.head.appendChild(styleEl);
 
	return styleEl;
}
 
function genZeedhiTheme(
	zeedhiVariables: IDictionary<IDictionary<string>>,
	defaultTheme: string,
	minifyTheme: (css: string) => string = defaultThemes.minifyTheme,
	cspNonce?: string,
) {
	let css = '';
	// TODO: re-factory this forEach for better performance
	Object.keys(zeedhiVariables.variables).forEach((key) => {
		css += `  --zd-${key}: ${zeedhiVariables.variables[key]};\n`;
	});
 
	// TODO: re-factory this forEach for better performance
	const themeColors = zeedhiVariables[defaultTheme] || {};
	Object.keys(themeColors).forEach((key) => {
		css += `  --zd-${key}: ${themeColors[key]};\n`;
	});
 
	css = `:root {\n${css}}\n\n`;
 
	let styleEl = document.getElementById('zeedhi-theme-stylesheet');
	if (!styleEl) {
		styleEl = createStyleElement();
	}
	styleEl.innerHTML = minifyTheme(css);
	if (cspNonce) {
		styleEl.setAttribute('nonce', cspNonce);
	}
}
 
const initTheme = (theme?: ITheme): IMaterialTheme => {
	const newTheme: IMaterialTheme = {
		defaultTheme: defaultThemes.defaultTheme,
		variations: { ...defaultThemes.variations },
		themes: { ...defaultThemes.themes },
	};
	const zeedhiVariables = { ...defaultThemes.zeedhi };
 
	if (theme) {
		newTheme.defaultTheme = theme.defaultTheme ?? defaultThemes.defaultTheme;
		newTheme.cspNonce = theme.cspNonce;
 
		if (theme.variations) {
			newTheme.variations = { ...defaultThemes.variations, ...theme.variations };
		}
 
		if (theme.themes) {
			// TODO: re-factory this forEach for better performance
			Object.keys(theme.themes).forEach((key) => {
				if (!defaultThemes.themes[key]) {
					newTheme.themes[key] = { ...theme.themes![key] };
					return;
				}
 
				newTheme.themes[key].dark = theme.themes![key].dark ?? defaultThemes.themes[key].dark;
 
				newTheme.themes[key].colors = {
					...defaultThemes.themes[key].colors,
					...theme.themes![key].colors,
				};
			});
		}
 
		if (theme.zeedhi) {
			// TODO: re-factory this forEach for better performance
			Object.keys(theme.zeedhi).forEach((key) => {
				if (!defaultThemes.zeedhi[key]) {
					zeedhiVariables[key] = { ...theme.zeedhi![key] };
					return;
				}
 
				zeedhiVariables[key] = { ...defaultThemes.zeedhi[key], ...theme.zeedhi![key] };
			});
		}
	}
 
	genZeedhiTheme(zeedhiVariables, newTheme.defaultTheme, theme?.minifyTheme, theme?.cspNonce);
 
	return newTheme;
};
 
export { initTheme };