ensure the toggle theme click event listener is only added once
This commit is contained in:
parent
07c4add7be
commit
b67d5df2e0
|
@ -34,10 +34,11 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let button = document.getElementById("theme-toggle")
|
||||||
|
// remove the listeners first to prevent adding duplicated listener when history goes back and forth
|
||||||
|
button.removeEventListener('click', toggleThemeListener)
|
||||||
// listen to toggle button
|
// listen to toggle button
|
||||||
document.getElementById("theme-toggle").addEventListener("click", () => {
|
button.addEventListener('click', toggleThemeListener)
|
||||||
window.dispatchEvent(new CustomEvent('toggle-theme'));
|
|
||||||
});
|
|
||||||
})();
|
})();
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
@ -48,6 +48,13 @@ window.addEventListener('toggle-theme', function() {
|
||||||
toggleThemeCallbacks[key](isDark)
|
toggleThemeCallbacks[key](isDark)
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// this function is used as the event listener for toggle-theme button click
|
||||||
|
function toggleThemeListener() {
|
||||||
|
// console.log('click theme-toggle')
|
||||||
|
window.dispatchEvent(new CustomEvent('toggle-theme'));
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
<script>
|
<script>
|
||||||
// load theme, as early as possible
|
// load theme, as early as possible
|
||||||
|
|
Loading…
Reference in New Issue