mirror of
https://github.com/priyanshujain/margin.git
synced 2026-10-05 04:27:03 +00:00
feat(theme): persistent light/dark toggle in the titlebar
This commit is contained in:
1 parent
a5b3c0c0e3
commit
9991d318ba
5 files changed
+61
-3
No files matched your search
@@ -0,0 +1,16 @@
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user