Skip to content

Commit d0e44ad

Browse files
jk-tagbangersphilwebb
authored andcommitted
Fix dark scheme detect function
Update `isInitialThemeDark` so that any value loaded from local-storage takes precedence. Prior to this commit, if the browser preferred the dark theme then the saved light theme would never be restored. See gh-23
1 parent c3ea9d2 commit d0e44ad

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/main/js/setup/switchtheme.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121

2222
const localStorage = window.localStorage;
2323
const htmlElement = document.documentElement;
24-
const prefersDarkColorScheme = window.matchMedia(
25-
"(prefers-color-scheme: dark)"
26-
);
2724

2825
swithInitialTheme();
2926
window.addEventListener("load", onWindowLoad);
@@ -46,7 +43,18 @@
4643
}
4744

4845
function isInitialThemeDark() {
49-
return loadTheme() === "dark" || prefersDarkColorScheme.matches;
46+
const prefersDarkColorScheme = window.matchMedia(
47+
"(prefers-color-scheme: dark)"
48+
);
49+
const savedColorScheme = loadTheme();
50+
switch (savedColorScheme) {
51+
case "dark":
52+
return true;
53+
case "light":
54+
return false;
55+
default:
56+
return prefersDarkColorScheme.matches;
57+
}
5058
}
5159

5260
function onThemeChange() {

0 commit comments

Comments
 (0)