-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (38 loc) · 1.13 KB
/
index.js
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
// index.js for dynamic-theme-switcher
import debounce from 'lodash.debounce';
import { applyPolyfill } from 'css-vars-ponyfill';
class ThemeSwitcher {
constructor() {
this.currentTheme = null;
}
/**
* Initialize the theme switcher with the default theme.
* @param {Object} defaultTheme - The default theme variables.
*/
init(defaultTheme) {
this.currentTheme = defaultTheme;
this.applyTheme(defaultTheme);
}
/**
* Apply the provided theme variables to the document root.
* @param {Object} themeVariables - The theme variables to apply.
*/
applyTheme(themeVariables) {
const root = document.documentElement;
Object.keys(themeVariables).forEach(key => {
root.style.setProperty(`--${key}`, themeVariables[key]);
});
// Apply polyfill for older browsers
applyPolyfill();
}
/**
* Switch to the provided theme with a debounce to prevent rapid changes.
* @param {Object} themeVariables - The theme variables to switch to.
*/
switchTheme(themeVariables) {
debounce(() => {
this.applyTheme(themeVariables);
}, 300)();
}
}
export default new ThemeSwitcher();