-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
95 lines (83 loc) · 3.02 KB
/
Copy paththeme.js
File metadata and controls
95 lines (83 loc) · 3.02 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
(() => {
const STORAGE_KEY = 'satelite_theme';
const LIGHT = 'light';
const DARK = 'dark';
function readStoredTheme() {
try {
return localStorage.getItem(STORAGE_KEY) === LIGHT ? LIGHT : DARK;
} catch {
return DARK;
}
}
function updateControls(theme) {
const isLight = theme === LIGHT;
document.querySelectorAll('[data-theme-toggle]').forEach((control) => {
if (control instanceof HTMLInputElement) {
control.checked = isLight;
} else {
control.textContent = isLight ? '☾' : '☀';
control.setAttribute('aria-checked', String(isLight));
control.setAttribute('aria-label', isLight ? 'Usar modo escuro' : 'Usar modo claro');
control.title = isLight ? 'Usar modo escuro' : 'Usar modo claro';
}
});
}
function applyTheme(theme, persist = true) {
const normalizedTheme = theme === LIGHT ? LIGHT : DARK;
document.documentElement.dataset.theme = normalizedTheme;
document.documentElement.style.colorScheme = normalizedTheme;
if (persist) {
try {
localStorage.setItem(STORAGE_KEY, normalizedTheme);
} catch {
// The selected theme still applies for the current page.
}
}
updateControls(normalizedTheme);
}
function createMenuControl(nav) {
const control = document.createElement('label');
control.className = 'theme-control';
const label = document.createElement('span');
label.className = 'theme-control-label';
label.textContent = 'Modo claro';
const input = document.createElement('input');
input.type = 'checkbox';
input.dataset.themeToggle = '';
input.setAttribute('aria-label', 'Ativar modo claro');
input.checked = document.documentElement.dataset.theme === LIGHT;
input.addEventListener('change', () => applyTheme(input.checked ? LIGHT : DARK));
const track = document.createElement('span');
track.className = 'theme-switch';
track.setAttribute('aria-hidden', 'true');
control.append(label, input, track);
nav.appendChild(control);
}
function createFloatingControl() {
const button = document.createElement('button');
button.type = 'button';
button.className = 'theme-icon-button';
button.dataset.themeToggle = '';
button.setAttribute('role', 'switch');
button.addEventListener('click', () => {
const nextTheme = document.documentElement.dataset.theme === LIGHT ? DARK : LIGHT;
applyTheme(nextTheme);
});
document.body.appendChild(button);
}
function mountControl() {
const nav = document.querySelector('.page-nav');
if (nav) createMenuControl(nav);
else createFloatingControl();
updateControls(document.documentElement.dataset.theme);
}
applyTheme(readStoredTheme(), false);
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', mountControl, { once: true });
} else {
mountControl();
}
window.addEventListener('storage', (event) => {
if (event.key === STORAGE_KEY) applyTheme(event.newValue, false);
});
})();