-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
37 lines (33 loc) · 1.15 KB
/
Copy paththeme.js
File metadata and controls
37 lines (33 loc) · 1.15 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
(function () {
var button = document.querySelector('[data-theme-toggle]');
var label = document.querySelector('[data-theme-label]');
var root = document.documentElement;
var storageKey = 'local-web-fix-theme';
function systemTheme() {
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
function applyTheme(theme) {
root.setAttribute('data-theme', theme);
if (button && label) {
var next = theme === 'dark' ? 'Light' : 'Dark';
label.textContent = next;
button.setAttribute('aria-pressed', theme === 'dark' ? 'true' : 'false');
button.setAttribute('aria-label', 'Switch to ' + next.toLowerCase() + ' theme');
}
}
try {
applyTheme(localStorage.getItem(storageKey) || systemTheme());
} catch (error) {
applyTheme(systemTheme());
}
if (button) {
button.addEventListener('click', function () {
var current = root.getAttribute('data-theme') || systemTheme();
var next = current === 'dark' ? 'light' : 'dark';
try {
localStorage.setItem(storageKey, next);
} catch (error) {}
applyTheme(next);
});
}
}());