fix(theme): make preference state deterministic

This commit is contained in:
cesnimda
2026-08-09 18:35:31 +02:00
parent 378807b3b9
commit 11734ee058
8 changed files with 240 additions and 23 deletions
+17
View File
@@ -0,0 +1,17 @@
export const THEME_BOOTSTRAP_SCRIPT = `(() => {
try {
const read = (key) => {
const value = window.localStorage.getItem(key);
return value === "light" || value === "dark" || value === "system" ? value : null;
};
const userKey = window.localStorage.getItem("authUserKey") || "anon";
const preference = read("themeMode:" + userKey) || (userKey !== "anon" ? read("themeMode:anon") : null) || "system";
const mode = preference === "dark" || (preference === "system" && window.matchMedia("(prefers-color-scheme: dark)").matches) ? "dark" : "light";
document.documentElement.setAttribute("data-color-scheme", mode);
document.documentElement.style.colorScheme = mode;
} catch {
const mode = window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
document.documentElement.setAttribute("data-color-scheme", mode);
document.documentElement.style.colorScheme = mode;
}
})();`;