mirror of https://github.com/luizdepra/hugo-coder.git

Kirill Feoktistov
25.27.2021 4bb81da844466d72d08c31545b64832c7319b5eb
Fixing darkModeToggle listener (#577)

If hideColorSchemeToggle is true, darkModeToggle will not be rendered and adding a listener to a non-existing object will cause error.
3 files modified
6 ■■■■ changed files
.gitignore 1 ●●●● patch | view | raw | blame | history
CONTRIBUTORS.md 1 ●●●● patch | view | raw | blame | history
assets/js/coder.js 4 ●●● patch | view | raw | blame | history
.gitignore
@@ -1,3 +1,4 @@
.idea
**/themes/
exampleSite/public/
exampleSite/resources/
CONTRIBUTORS.md
@@ -98,3 +98,4 @@
- [cuso4-5h2o](https://www.cuso4.me)
- [freeformz](https://icanhazdowntime.org)
- [Roberto Gongora](https://yourfavourite.blog)
- [Kirill Feoktistov](https://feoktistoff.org)
assets/js/coder.js
@@ -11,9 +11,11 @@
    setTheme(darkModeMediaQuery.matches ? "dark" : "light");
}
if (darkModeToggle) {
darkModeToggle.addEventListener('click', () => {
    setTheme(body.classList.contains("colorscheme-dark") ? "light" : "dark");
});
}
darkModeMediaQuery.addListener((event) => {
    setTheme(event.matches ? "dark" : "light");
@@ -26,7 +28,7 @@
function setTheme(theme) {
    body.classList.remove('colorscheme-auto');
    inverse = theme === 'dark' ? 'light' : 'dark';
    const inverse = theme === 'dark' ? 'light' : 'dark';
    localStorage.setItem('colorscheme', theme);
    body.classList.remove('colorscheme-' + inverse);
    body.classList.add('colorscheme-' + theme);