-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
30 lines (27 loc) · 1.11 KB
/
theme.js
File metadata and controls
30 lines (27 loc) · 1.11 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
// Theme toggle — persists choice in localStorage
(function() {
const saved = localStorage.getItem('theme');
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
const theme = saved || (prefersDark ? 'dark' : 'dark'); // default dark
if (theme === 'light') document.documentElement.setAttribute('data-theme', 'light');
document.addEventListener('DOMContentLoaded', function() {
const btn = document.querySelector('.theme-toggle');
if (!btn) return;
function update() {
const isLight = document.documentElement.getAttribute('data-theme') === 'light';
btn.textContent = isLight ? '🌙' : '☀️';
}
update();
btn.addEventListener('click', function() {
const isLight = document.documentElement.getAttribute('data-theme') === 'light';
if (isLight) {
document.documentElement.removeAttribute('data-theme');
localStorage.setItem('theme', 'dark');
} else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light');
}
update();
});
});
})();