Repository navigation
Expand file tree
/
Copy paththeme.js
More file actions
45 lines (40 loc) · 1.65 KB
/
Copy paththeme.js
File metadata and controls
45 lines (40 loc) · 1.65 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
(() => {
const root = document.documentElement;
const system = window.matchMedia('(prefers-color-scheme: dark)');
let preference = null;
try {
const stored = localStorage.getItem('sqlstack-theme');
if (stored === 'light' || stored === 'dark') preference = stored;
} catch {
// The toggle still works when browser storage is unavailable.
}
function apply(theme) {
root.dataset.theme = theme;
document.querySelector('meta[name="theme-color"]').content = theme === 'dark' ? '#0d0f12' : '#ffffff';
// Match browser chrome, even when the page theme is explicitly overridden.
document.querySelector('[data-favicon]').href = system.matches ? 'assets/favicon-dark-32.png?v=2' : 'assets/favicon-32.png?v=2';
const toggle = document.querySelector('[data-theme-toggle]');
if (toggle) {
toggle.setAttribute('aria-pressed', String(theme === 'dark'));
toggle.title = theme === 'dark' ? 'Switch to light mode' : 'Switch to dark mode';
}
}
apply(preference || (system.matches ? 'dark' : 'light'));
system.addEventListener('change', () => {
apply(preference || (system.matches ? 'dark' : 'light'));
});
document.addEventListener('DOMContentLoaded', () => {
const toggle = document.querySelector('[data-theme-toggle]');
apply(root.dataset.theme);
toggle.hidden = false;
toggle.addEventListener('click', () => {
preference = root.dataset.theme === 'dark' ? 'light' : 'dark';
apply(preference);
try {
localStorage.setItem('sqlstack-theme', preference);
} catch {
// Keep the selected theme for this page even without persistent storage.
}
});
});
})();