Fix incorrect theme when loading settings

This commit is contained in:
JovannMC 2025-02-06 19:05:32 +03:00
parent ba4288b3be
commit 6e0b10b055
No known key found for this signature in database
2 changed files with 11 additions and 7 deletions

View File

@ -14,7 +14,9 @@
const prefersDark = window.matchMedia(
"(prefers-color-scheme: dark)",
).matches;
console.log(`Theme: ${theme || "default"}, prefers dark: ${prefersDark}`);
console.log(
`Theme: ${theme || "N/A"}, prefers dark: ${prefersDark}`,
);
if (theme !== "light" && theme !== "dark") {
console.log("Invalid theme, setting to default");
@ -23,14 +25,15 @@
// first time visitor
window.addEventListener("load", () => {
window.plausible("Theme set", {
props: { theme: prefersDark ? "dark" : "light" },
props: {
theme: prefersDark ? "dark" : "light",
},
});
});
}
// invalid theme or first time visitor, set to default
theme = prefersDark ? "dark" : "light";
console.log(`Setting theme to ${theme}`);
localStorage.setItem("theme", theme);
}

View File

@ -16,6 +16,7 @@
isMobile,
motion,
showGradient,
theme,
} from "$lib/store/index.svelte";
import {
InfoIcon,
@ -93,8 +94,8 @@
isMobile.set(window.innerWidth <= 768);
});
// defaults to true if not set
motion.set(localStorage.getItem("motion") !== "false");
motion.set(localStorage.getItem("motion") !== "false"); // defaults to true if not set
theme.set(localStorage.getItem("theme") as "light" | "dark" || "light");
});
let goingLeft = $state(false);