Skip to content

Commit

Permalink
Allow a default theme to be specified by adding a URL query string pa…
Browse files Browse the repository at this point in the history
…rameter: `&theme=dark` or `&theme=light`. Default is to auto-detect.
  • Loading branch information
stefansundin committed Sep 20, 2023
1 parent c5dd6cd commit dd83e91
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions public/js/main.js
Expand Up @@ -380,13 +380,19 @@ document.addEventListener("DOMContentLoaded", () => {
}
});

// Pass theme=dark in the query string to default to dark mode
let theme = window.location.search.substring(1).split('&').find(v => v.startsWith('theme='))?.split('=')?.[1]
if (localStorageUsable) {
const theme = localStorage.getItem("theme");
if (theme === "dark" || (theme === null && window.matchMedia("(prefers-color-scheme: dark)").matches)) {
checkbox.click();
// localStorage has preference over query parameter
const localTheme = localStorage.getItem("theme");
if (localTheme) {
theme = localTheme;
}
checkbox.indeterminate = (theme === null);
}
if (theme === "dark" || (theme === undefined && window.matchMedia("(prefers-color-scheme: dark)").matches)) {
checkbox.click();
}
checkbox.indeterminate = (theme === undefined);
});

window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (e) => {
Expand Down

0 comments on commit dd83e91

Please sign in to comment.