Skip to content

Commit f78045c

Browse files
committed
dark-mode: allow following the system preference again
After toggling dark mode on and off again, a subsequent change of the system preference to dark mode will not respected because the toggling stored the user's preference. Let's store the user preference only when it differs from the user's choice. That way, in the scenario above the system preference would be followed again. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 8889c3a commit f78045c

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

assets/js/application.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,8 @@ var DarkMode = {
643643
theme = document.documentElement.dataset.theme === "dark" ? "light" : "dark"
644644
}
645645
document.documentElement.dataset.theme = theme
646-
localStorage.setItem("theme", theme);
646+
if (prefersDarkScheme === (theme === "dark")) localStorage.removeItem("theme");
647+
else localStorage.setItem("theme", theme);
647648
button.attr("src", `${baseURLPrefix}images/${theme === "dark" ? "light" : "dark"}-mode.svg`);
648649
});
649650
},

layouts/_default/baseof.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ <h1>Redirecting&hellip;</h1>
4949
if (currentTheme) {
5050
// Check dark mode preference at the OS level
5151
const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)").matches
52-
if ((prefersDarkScheme && currentTheme === "light")
52+
if (prefersDarkScheme === (currentTheme === "dark")) localStorage.removeItem("theme")
53+
else if ((prefersDarkScheme && currentTheme === "light")
5354
|| (!prefersDarkScheme && currentTheme === "dark")) {
5455
document.documentElement.dataset.theme = currentTheme
5556
}

0 commit comments

Comments
 (0)