-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
22 lines (22 loc) · 778 Bytes
/
script.js
File metadata and controls
22 lines (22 loc) · 778 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(function() {
const root = document.documentElement;
const toggle = document.getElementById('theme-toggle');
const saved = localStorage.getItem('theme');
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (saved) {
root.setAttribute('data-theme', saved);
} else if (prefersDark) {
root.setAttribute('data-theme', 'dark');
}
const update = () => {
const theme = root.getAttribute('data-theme');
toggle.textContent = theme === 'dark' ? '☀️' : '🌙';
};
update();
toggle.addEventListener('click', () => {
const newTheme = root.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
root.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
update();
});
})();