forked from AnujShrivastava01/AnimateItNow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-manager.js
More file actions
129 lines (111 loc) · 3.53 KB
/
theme-manager.js
File metadata and controls
129 lines (111 loc) · 3.53 KB
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
/**
* Theme Manager - Unified theme management system for the entire application
* Handles theme switching, persistence, and system preference detection
*/
class ThemeManager {
constructor() {
this.theme = 'light'; // default theme
this.init();
}
/**
* Initialize the theme manager
*/
init() {
// Check for saved theme preference
const savedTheme = localStorage.getItem('theme');
// Check for system preference
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
// Set initial theme
if (savedTheme) {
this.setTheme(savedTheme);
} else if (systemPrefersDark) {
this.setTheme('dark');
} else {
this.setTheme('light');
}
// Listen for system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
// Only update if user hasn't explicitly set a theme
if (!localStorage.getItem('theme')) {
this.setTheme(e.matches ? 'dark' : 'light');
}
});
// Set up theme toggle listeners
this.setupThemeToggle();
}
/**
* Set the theme for the entire application
* @param {string} theme - 'light' or 'dark'
*/
setTheme(theme) {
this.theme = theme;
// Update CSS variables
if (theme === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark');
document.body.classList.add('dark');
} else {
document.documentElement.removeAttribute('data-theme');
document.body.classList.remove('dark');
}
// Update theme toggle icons
this.updateThemeToggleIcons();
// Save preference
localStorage.setItem('theme', theme);
}
/**
* Toggle between light and dark themes
*/
toggleTheme() {
this.setTheme(this.theme === 'dark' ? 'light' : 'dark');
}
/**
* Update all theme toggle icons in the application
*/
updateThemeToggleIcons() {
// Update all theme toggle buttons
const themeToggles = document.querySelectorAll('#theme-toggle, .theme-toggle');
themeToggles.forEach(toggle => {
const icon = toggle.querySelector('i');
if (icon) {
if (this.theme === 'dark') {
icon.className = 'fas fa-sun';
} else {
icon.className = 'fas fa-moon';
}
}
});
}
/**
* Set up event listeners for theme toggle buttons
*/
setupThemeToggle() {
// Listen for clicks on theme toggle buttons
document.addEventListener('click', (e) => {
if (e.target.closest('#theme-toggle') || e.target.closest('.theme-toggle')) {
this.toggleTheme();
}
});
}
/**
* Get the current theme
* @returns {string} - 'light' or 'dark'
*/
getTheme() {
return this.theme;
}
/**
* Check if dark theme is active
* @returns {boolean}
*/
isDark() {
return this.theme === 'dark';
}
}
// Initialize theme manager when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
window.themeManager = new ThemeManager();
});
// Export for use in other modules (if needed)
if (typeof module !== 'undefined' && module.exports) {
module.exports = ThemeManager;
}