Skip to content

Commit 14472d3

Browse files
authored
Merge pull request #2007 from 0xh3xa/bugfix/invalid-theme-option
Load the user's preferred color scheme if the URL contains an invalid theme
2 parents d3357d2 + 9f30dd0 commit 14472d3

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/web/waiters/OptionsWaiter.mjs

+18-3
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,33 @@ class OptionsWaiter {
160160

161161
// Update theme selection
162162
const themeSelect = document.getElementById("theme");
163-
themeSelect.selectedIndex = themeSelect.querySelector(`option[value="${theme}"`).index;
163+
let themeOption = themeSelect.querySelector(`option[value="${theme}"]`);
164+
165+
if (!themeOption) {
166+
const preferredColorScheme = this.getPreferredColorScheme();
167+
document.querySelector(":root").className = preferredColorScheme;
168+
themeOption = themeSelect.querySelector(`option[value="${preferredColorScheme}"]`);
169+
}
170+
171+
themeSelect.selectedIndex = themeOption.index;
164172
}
165173

166174
/**
167175
* Applies the user's preferred color scheme using the `prefers-color-scheme` media query.
168176
*/
169177
applyPreferredColorScheme() {
170-
const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)").matches;
171-
const theme = prefersDarkScheme ? "dark" : "classic";
178+
const theme = this.getPreferredColorScheme();
172179
this.changeTheme(theme);
173180
}
174181

182+
/**
183+
* Get the user's preferred color scheme using the `prefers-color-scheme` media query.
184+
*/
185+
getPreferredColorScheme() {
186+
const prefersDarkScheme = window.matchMedia("(prefers-color-scheme: dark)").matches;
187+
return prefersDarkScheme ? "dark" : "classic";
188+
}
189+
175190
/**
176191
* Changes the console logging level.
177192
*

0 commit comments

Comments
 (0)