fix(share): light theme not properly restored

This commit is contained in:
Elian Doran 2025-10-29 18:35:27 +02:00
parent bc86fb95b5
commit b58e1f146c
No known key found for this signature in database

View File

@ -1,5 +1,5 @@
const prefersDark = localStorage.getItem("theme") === "dark" || (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches);
if (prefersDark) {
const themeToLoad = getThemeToLoad();
if (themeToLoad === "dark") {
document.body.classList.add("theme-dark");
document.body.classList.remove("theme-light");
} else {
@ -12,7 +12,7 @@ export default function setupThemeSelector() {
const themeSelection: HTMLDivElement = document.querySelector(".theme-selection")!;
themeSelection.classList.add("no-transition");
themeSwitch.checked = prefersDark;
themeSwitch.checked = (themeToLoad === "dark");
setTimeout(() => themeSelection.classList.remove("no-transition"), 400);
themeSwitch?.addEventListener("change", () => {
@ -27,3 +27,14 @@ export default function setupThemeSelector() {
}
});
}
function getThemeToLoad() {
const storedTheme = localStorage.getItem("theme");
if (storedTheme) {
// Respect user's choice if one has already been made.
return storedTheme;
} else if (window.matchMedia) {
// Fallback to browser's color preference otherwise.
return window.matchMedia('(prefers-color-scheme: dark)').matches;
}
}