forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththemeBootstrap.ts
More file actions
31 lines (26 loc) · 834 Bytes
/
Copy paththemeBootstrap.ts
File metadata and controls
31 lines (26 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const STORAGE_KEY = "app-theme";
type Theme = "light" | "dark" | "system";
export function bootstrapTheme() {
if (typeof window === "undefined") return;
try {
const savedTheme = localStorage.getItem(STORAGE_KEY) as Theme | null;
const theme = savedTheme || "system";
let resolvedTheme: "light" | "dark";
if (theme === "system") {
resolvedTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
} else {
resolvedTheme = theme as "light" | "dark";
}
const root = document.documentElement;
if (resolvedTheme === "dark") {
root.classList.add("dark");
} else {
root.classList.remove("dark");
}
root.setAttribute("data-theme", resolvedTheme);
} catch (e) {
console.error("Theme bootstrap failed", e);
}
}