Skip to content

Commit 4bb81da

Browse files
authored
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.
1 parent 9920a72 commit 4bb81da

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.idea
12
**/themes/
23
exampleSite/public/
34
exampleSite/resources/

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,4 @@
9898
- [cuso4-5h2o](https://www.cuso4.me)
9999
- [freeformz](https://icanhazdowntime.org)
100100
- [Roberto Gongora](https://yourfavourite.blog)
101+
- [Kirill Feoktistov](https://feoktistoff.org)

assets/js/coder.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ if (localStorage.getItem("colorscheme")) {
1111
setTheme(darkModeMediaQuery.matches ? "dark" : "light");
1212
}
1313

14-
darkModeToggle.addEventListener('click', () => {
15-
setTheme(body.classList.contains("colorscheme-dark") ? "light" : "dark");
16-
});
14+
if (darkModeToggle) {
15+
darkModeToggle.addEventListener('click', () => {
16+
setTheme(body.classList.contains("colorscheme-dark") ? "light" : "dark");
17+
});
18+
}
1719

1820
darkModeMediaQuery.addListener((event) => {
1921
setTheme(event.matches ? "dark" : "light");
@@ -26,7 +28,7 @@ document.addEventListener("DOMContentLoaded", function () {
2628

2729
function setTheme(theme) {
2830
body.classList.remove('colorscheme-auto');
29-
inverse = theme === 'dark' ? 'light' : 'dark';
31+
const inverse = theme === 'dark' ? 'light' : 'dark';
3032
localStorage.setItem('colorscheme', theme);
3133
body.classList.remove('colorscheme-' + inverse);
3234
body.classList.add('colorscheme-' + theme);

0 commit comments

Comments
 (0)