From 4bb81da844466d72d08c31545b64832c7319b5eb Mon Sep 17 00:00:00 2001 From: Kirill Feoktistov Date: Fri, 25 Jun 2021 18:27:13 +0000 Subject: [PATCH] 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. --- .gitignore | 1 + CONTRIBUTORS.md | 1 + assets/js/coder.js | 10 ++++++---- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 6043df9..287b745 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea **/themes/ exampleSite/public/ exampleSite/resources/ diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 6c7d33f..1eadbc0 100644 --- a/CONTRIBUTORS.md +++ b/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) diff --git a/assets/js/coder.js b/assets/js/coder.js index 3956286..bb7197f 100644 --- a/assets/js/coder.js +++ b/assets/js/coder.js @@ -11,9 +11,11 @@ if (localStorage.getItem("colorscheme")) { setTheme(darkModeMediaQuery.matches ? "dark" : "light"); } -darkModeToggle.addEventListener('click', () => { - setTheme(body.classList.contains("colorscheme-dark") ? "light" : "dark"); -}); +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 @@ document.addEventListener("DOMContentLoaded", function () { 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);