mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-04 20:17:03 +00:00
17 lines
567 B
TypeScript
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);
|
|
}
|