-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
59 lines (53 loc) · 1.55 KB
/
theme.js
File metadata and controls
59 lines (53 loc) · 1.55 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
// theme.js
// === Configuration ===
// Default colors for light and dark themes
const themes = {
light: {
'--bg-color': '#f7fbff',
'--text-color': '#222',
'--form-bg': '#fff',
'--form-border': '#d7e8ff',
'--button-bg': '#003366',
'--button-text': '#fff',
'--button-hover': '#0055aa',
},
dark: {
'--bg-color': '#0a0f0f',
'--text-color': '#f2f2f2',
'--form-bg': '#101616',
'--form-border': '#1c1c1c',
'--button-bg': '#0055aa',
'--button-text': '#fff',
'--button-hover': '#0080ff',
}
};
// Apply theme by setting CSS variables
function applyTheme(theme) {
const root = document.documentElement;
const t = themes[theme];
for (const key in t) {
root.style.setProperty(key, t[key]);
}
document.body.setAttribute('data-theme', theme);
const toggleBtn = document.getElementById('theme-toggle');
if(toggleBtn){
toggleBtn.textContent = theme === 'dark' ? '☀️ Light Mode' : '🌙 Dark Mode';
}
localStorage.setItem('theme', theme);
}
// Detect saved theme or system preference
const savedTheme = localStorage.getItem('theme');
if(savedTheme) {
applyTheme(savedTheme);
} else {
const systemPrefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches;
applyTheme(systemPrefersDark ? 'dark' : 'light');
}
// Theme toggle button functionality
const toggleBtn = document.getElementById('theme-toggle');
if(toggleBtn){
toggleBtn.addEventListener('click', () => {
const current = document.body.getAttribute('data-theme');
applyTheme(current === 'dark' ? 'light' : 'dark');
});
}