Skip to content

Commit 416186c

Browse files
authored
Theme toggle button (#80)
1 parent 77a61a4 commit 416186c

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

_includes/header_custom.html

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
2+
<symbol id="svg-sun" viewBox="0 0 24 24">
3+
<title>Light mode</title>
4+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
5+
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather-sun">
6+
<circle cx="12" cy="12" r="5"></circle>
7+
<line x1="12" y1="1" x2="12" y2="3"></line>
8+
<line x1="12" y1="21" x2="12" y2="23"></line>
9+
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
10+
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
11+
<line x1="1" y1="12" x2="3" y2="12"></line>
12+
<line x1="21" y1="12" x2="23" y2="12"></line>
13+
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
14+
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
15+
</svg>
16+
</symbol>
17+
<symbol id="svg-moon" viewBox="0 0 24 24">
18+
<title>Dark mode</title>
19+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
20+
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-moon">
21+
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
22+
<path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" />
23+
</svg>
24+
</symbol>
25+
</svg>
26+
27+
<a class="site-button" id="theme-toggle" style="cursor: pointer;"></a>
28+
29+
<script>
30+
const storageKey = 'dpfa-docs-theme';
31+
32+
const button = document.getElementById('theme-toggle');
33+
const loadedTheme = localStorage.getItem(storageKey) || 'light';
34+
35+
const setTheme = function(theme) {
36+
localStorage.setItem(storageKey, theme);
37+
38+
if (theme === 'light') {
39+
button.innerHTML = `<svg width='18px' height='18px'><use href="#svg-moon"></use></svg>`;
40+
} else {
41+
button.innerHTML = `<svg width='18px' height='18px'><use href="#svg-sun"></use></svg>`;
42+
}
43+
44+
jtd.setTheme(theme);
45+
}
46+
47+
const toggleTheme = function() {
48+
const theme = localStorage.getItem(storageKey);
49+
if (theme === 'light') {
50+
setTheme('dark');
51+
} else {
52+
setTheme('light');
53+
}
54+
}
55+
56+
button.addEventListener('click', toggleTheme);
57+
setTheme(loadedTheme);
58+
</script>

0 commit comments

Comments
 (0)