Files
margin/src/theme.ts
T

17 lines
567 B
TypeScript

export type Theme = "light" | "dark";
const KEY = "margin-theme";
export function initialTheme(): Theme {
const attr = document.documentElement.getAttribute("data-theme");
if (attr === "light" || attr === "dark") return attr;
const saved = localStorage.getItem(KEY);
if (saved === "light" || saved === "dark") return saved;
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
}
export function applyTheme(theme: Theme) {
document.documentElement.setAttribute("data-theme", theme);
localStorage.setItem(KEY, theme);
}