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.
This commit is contained in:
Kirill Feoktistov
2021-06-25 18:27:13 +00:00
committed by GitHub
parent 9920a722b0
commit 4bb81da844
3 changed files with 8 additions and 4 deletions

View File

@@ -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);