-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
48 lines (39 loc) · 1.46 KB
/
Copy paththeme.js
File metadata and controls
48 lines (39 loc) · 1.46 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
/**
* js/ui/theme.js
* ──────────────────────────────────────
* Dark / Light mode management.
*
* Features:
* - Detects OS preference on first visit
* - Saves choice to localStorage
* - Smooth CSS transitions (handled by tokens.css)
* - Updates aria-label on toggle button
*/
const ThemeManager = (() => {
const STORAGE_KEY = 'digit-ai:theme';
const ATTR = 'data-theme';
function init() {
const saved = localStorage.getItem(STORAGE_KEY);
const pref = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
_apply(saved || pref);
document.getElementById('themeToggle')
?.addEventListener('click', toggle);
// Sync if OS changes
window.matchMedia('(prefers-color-scheme: light)')
.addEventListener('change', e => {
if (!localStorage.getItem(STORAGE_KEY)) _apply(e.matches ? 'light' : 'dark');
});
}
function toggle() {
const current = document.documentElement.getAttribute(ATTR) || 'dark';
const next = current === 'dark' ? 'light' : 'dark';
_apply(next);
localStorage.setItem(STORAGE_KEY, next);
}
function _apply(theme) {
document.documentElement.setAttribute(ATTR, theme);
const btn = document.getElementById('themeToggle');
if (btn) btn.setAttribute('aria-label', `Switch to ${theme === 'dark' ? 'light' : 'dark'} mode`);
}
return { init, toggle };
})();