Repository navigation
Expand file tree
/
Copy paththeme.js
More file actions
39 lines (36 loc) · 1.51 KB
/
Copy paththeme.js
File metadata and controls
39 lines (36 loc) · 1.51 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
/* Light/dark theme: follows the device until the visitor clicks the sun or moon,
then remembers their choice on this browser. Loaded in <head> so the saved
theme applies before the page paints. */
(function () {
var KEY = 'bpmdata-theme';
var root = document.documentElement;
var media = window.matchMedia ? window.matchMedia('(prefers-color-scheme: dark)') : null;
function saved() {
try { var v = localStorage.getItem(KEY); return v === 'light' || v === 'dark' ? v : null; }
catch (e) { return null; }
}
function current() {
return root.getAttribute('data-theme') || (media && media.matches ? 'dark' : 'light');
}
function label(btn) {
btn.setAttribute('aria-label', current() === 'dark' ? 'Switch to light mode' : 'Switch to dark mode');
btn.title = btn.getAttribute('aria-label');
}
var s = saved();
if (s) root.setAttribute('data-theme', s);
document.addEventListener('DOMContentLoaded', function () {
var btns = document.querySelectorAll('[data-theme-toggle]');
Array.prototype.forEach.call(btns, function (btn) {
label(btn);
btn.addEventListener('click', function () {
var next = current() === 'dark' ? 'light' : 'dark';
root.setAttribute('data-theme', next);
try { localStorage.setItem(KEY, next); } catch (e) {}
Array.prototype.forEach.call(btns, label);
});
});
if (media && media.addEventListener) {
media.addEventListener('change', function () { Array.prototype.forEach.call(btns, label); });
}
});
})();