summaryrefslogtreecommitdiff
path: root/layouts/partials/header.html
diff options
context:
space:
mode:
authorReorx <novoreorx@gmail.com>2022-05-27 22:40:21 +0800
committerReorx <novoreorx@gmail.com>2022-05-27 23:39:55 +0800
commit47a2fc815386d762d9e61376dce36c983787e093 (patch)
treef1d71a7c4a6331df169d3afe24982135b8e3ebd7 /layouts/partials/header.html
parent28511efd081dba9dac8cbf8fa2c8135216264243 (diff)
refactor theme toggle; add delegate-it
Diffstat (limited to 'layouts/partials/header.html')
-rw-r--r--layouts/partials/header.html82
1 files changed, 50 insertions, 32 deletions
diff --git a/layouts/partials/header.html b/layouts/partials/header.html
index f708cb7..166eee1 100644
--- a/layouts/partials/header.html
+++ b/layouts/partials/header.html
@@ -1,43 +1,61 @@
-{{- /* theme-toggle is enabled */}}
-{{- if (not site.Params.disableThemeToggle) }}
-{{- /* theme is light */}}
-{{- if (eq site.Params.defaultTheme "light") }}
-<script>
- if (localStorage.getItem("pref-theme") === "dark") {
+<script data-no-instant>
+function switchTheme(theme) {
+ switch (theme) {
+ case 'light':
+ document.body.classList.remove('dark');
+ break;
+ case 'dark':
+ document.body.classList.add('dark');
+ break;
+ // auto
+ default:
+ if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.body.classList.add('dark');
- }
+ }
+ }
+}
-</script>
-{{- /* theme is dark */}}
-{{- else if (eq site.Params.defaultTheme "dark") }}
-<script>
- if (localStorage.getItem("pref-theme") === "light") {
- document.body.classList.remove('dark')
- }
+function isDarkTheme() {
+ return document.body.className.includes("dark");
+}
-</script>
-{{- else }}
-{{- /* theme is auto */}}
-<script>
- if (localStorage.getItem("pref-theme") === "dark") {
- document.body.classList.add('dark');
- } else if (localStorage.getItem("pref-theme") === "light") {
- document.body.classList.remove('dark')
- } else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
- document.body.classList.add('dark');
- }
+function getPrefTheme() {
+ return localStorage.getItem("pref-theme");
+}
+
+function setPrefTheme(theme) {
+ switchTheme(theme)
+ localStorage.setItem("pref-theme", theme);
+}
+const toggleThemeCallbacks = []
+toggleThemeCallbacks.push((isDark) => {
+ // console.log('window toggle-theme')
+ if (isDark) {
+ setPrefTheme('light');
+ } else {
+ setPrefTheme('dark');
+ }
+})
+
+// listen to set-theme event,
+// because window is never changed by InstantClick,
+// we add the listener to window to ensure the event is always received
+window.addEventListener('toggle-theme', function() {
+ const isDark = isDarkTheme()
+ toggleThemeCallbacks.forEach(callback => callback(isDark))
+});
</script>
-{{- end }}
-{{- /* theme-toggle is disabled and theme is auto */}}
-{{- else if (and (ne site.Params.defaultTheme "light") (ne site.Params.defaultTheme "dark"))}}
<script>
- if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
- document.body.classList.add('dark');
- }
+ // load theme, as early as possible
+ (function() {
+ const defaultTheme = '{{ site.Params.defaultTheme | default "light" }}';
+ const prefTheme = getPrefTheme();
+ const theme = prefTheme ? prefTheme : defaultTheme;
+ switchTheme(theme);
+ })();
</script>
-{{- end }}
<header class="header">
<nav class="nav">