fix(theme): make preference state deterministic
This commit is contained in:
@@ -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;
|
||||
}
|
||||
})();`;
|
||||
Reference in New Issue
Block a user