-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme.js
More file actions
37 lines (34 loc) · 1.3 KB
/
Copy paththeme.js
File metadata and controls
37 lines (34 loc) · 1.3 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
// Light/dark theme toggle. Loaded in <head> so a saved choice applies before first paint.
(function(){
var root = document.documentElement;
var media = window.matchMedia('(prefers-color-scheme: dark)');
function saved(){
try { return localStorage.getItem('theme'); } catch (e) { return null; }
}
function current(){
return root.getAttribute('data-theme') || (media.matches ? 'dark' : 'light');
}
function render(){
var mode = current();
document.querySelectorAll('.theme-toggle').forEach(function(btn){
btn.setAttribute('data-mode', mode);
var label = mode === 'dark' ? 'Switch to light mode' : 'Switch to dark mode';
btn.setAttribute('aria-label', label);
btn.setAttribute('title', label);
});
}
var initial = saved();
if (initial === 'dark' || initial === 'light') root.setAttribute('data-theme', initial);
media.addEventListener('change', render);
document.addEventListener('DOMContentLoaded', function(){
render();
document.querySelectorAll('.theme-toggle').forEach(function(btn){
btn.addEventListener('click', function(){
var next = current() === 'dark' ? 'light' : 'dark';
root.setAttribute('data-theme', next);
try { localStorage.setItem('theme', next); } catch (e) {}
render();
});
});
});
})();