summaryrefslogtreecommitdiff
path: root/layouts/partials/header.html
diff options
context:
space:
mode:
authorReorx <novoreorx@gmail.com>2022-06-01 17:14:46 +0800
committerReorx <novoreorx@gmail.com>2022-06-01 17:15:04 +0800
commit07c4add7bed65cb14f70aed58e7f71133df32814 (patch)
treea8901cd6caf1e898a4e61a0549dae84b843a1a1c /layouts/partials/header.html
parent0aa7a35ee0535a0ba5bdde7ed1a6c4fa12b3008d (diff)
refactor toggleThemeCallbacks
- use object instead of array to ensure uniqueness for each callback - do not use <script data-no-instant> to add callback, they won't be called if the page is not loaded first time; instead, try to set the callback each time the page is loaded
Diffstat (limited to 'layouts/partials/header.html')
-rw-r--r--layouts/partials/header.html10
1 files changed, 6 insertions, 4 deletions
diff --git a/layouts/partials/header.html b/layouts/partials/header.html
index 775f2e3..2e7512d 100644
--- a/layouts/partials/header.html
+++ b/layouts/partials/header.html
@@ -28,15 +28,15 @@ function setPrefTheme(theme) {
localStorage.setItem("pref-theme", theme);
}
-const toggleThemeCallbacks = []
-toggleThemeCallbacks.push((isDark) => {
+const toggleThemeCallbacks = {}
+toggleThemeCallbacks['main'] = (isDark) => {
// console.log('window toggle-theme 1')
if (isDark) {
setPrefTheme('light');
} else {
setPrefTheme('dark');
}
-})
+}
// listen to set-theme event,
// because window is never changed by InstantClick,
@@ -44,7 +44,9 @@ toggleThemeCallbacks.push((isDark) => {
window.addEventListener('toggle-theme', function() {
// console.log('window toggle-theme')
const isDark = isDarkTheme()
- toggleThemeCallbacks.forEach(callback => callback(isDark))
+ for (const key in toggleThemeCallbacks) {
+ toggleThemeCallbacks[key](isDark)
+ }
});
</script>
<script>