-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinline.html
73 lines (65 loc) · 1.73 KB
/
inline.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<title> Inline Example - Theme Selector </title>
<style>
html.light { --bg: #eeeeee; --fg: #222222; }
html.dark { --bg: #222222; --fg: #eeeeee; }
html.sage { --bg: #456e2f; --fg: #dbdbc1; }
html.hot { --bg: #e17e2a; --fg: #fff700; }
html.cool { --bg: #607a89; --fg: #cfcfcf; }
body {
background: var(--bg);
color: var(--fg);
margin: 1.5rem;
}
#selector button {
border:0;
background: none;
color: inherit;
text-decoration: underline;
cursor: pointer
}
#selector button * {
pointer-events: none;
}
</style>
<script>
let prefersDark = matchMedia('(prefers-color-scheme: dark)');
prefersDark.addEventListener('change', event => loadTheme());
function setTheme(theme) {
if (theme == 'auto') {
localStorage.removeItem('theme');
loadTheme(null);
} else {
localStorage.setItem('theme', theme);
applyTheme(theme);
}
}
function loadTheme(theme) {
theme = localStorage.getItem('theme');
theme ??= (prefersDark.matches) ? 'dark' : 'light';
applyTheme(theme);
}
function applyTheme(theme) {
console.log('Applying theme:', theme);
document.documentElement.className = theme;
}
window.setTheme = setTheme;
loadTheme();
</script>
<h1> Inline Example </h1>
<div id="selector">
<button id="auto">Auto</button>
<button id="light">Light</button>
<button id="dark">Dark</button>
<button id="sage">Sage</button>
<button id="hot">Hot</button>
<button id="cool">Cool</button>
</div>
<script>
let selector = document.getElementById('selector');
selector.addEventListener('click', event => {
if (event.target == selector) { return; }
window.setTheme(event.target.id);
});
</script>