-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
38 lines (35 loc) · 1.42 KB
/
Copy paththeme.js
File metadata and controls
38 lines (35 loc) · 1.42 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
// guerz.lol — shared day/night theme toggle
// include on every page after the <body> markup (or defer)
(function () {
function applyTheme(mode) {
document.documentElement.setAttribute('data-theme', mode);
document.querySelectorAll('[data-theme-btn]').forEach(function (btn) {
var isActive = btn.getAttribute('data-theme-btn') === mode;
btn.classList.toggle('is-active', isActive);
btn.setAttribute('aria-pressed', isActive ? 'true' : 'false');
});
var favicon = document.getElementById('favicon');
if (favicon) favicon.setAttribute('href', mode === 'night' ? 'favicon-lavender.svg' : 'favicon-peach.svg');
var avatar = document.getElementById('avatar-img');
if (avatar) avatar.setAttribute('src', mode === 'night' ? 'favicon-lavender.svg' : 'favicon-peach.svg');
try { localStorage.setItem('guerz-theme', mode); } catch (e) {}
}
function initTheme() {
var saved = 'day';
try {
var stored = localStorage.getItem('guerz-theme');
if (stored === 'day' || stored === 'night') saved = stored;
} catch (e) {}
applyTheme(saved);
document.querySelectorAll('[data-theme-btn]').forEach(function (btn) {
btn.addEventListener('click', function () {
applyTheme(btn.getAttribute('data-theme-btn'));
});
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initTheme);
} else {
initTheme();
}
})();