;s;
This commit is contained in:
29
lib/theme.ts
Normal file
29
lib/theme.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
export type Theme = "light" | "dark";
|
||||
|
||||
export const THEME_STORAGE_KEY = "theme";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
NomadRoamTheme?: {
|
||||
apply: (theme: Theme) => void;
|
||||
toggle: () => void;
|
||||
read: () => Theme;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export function applyTheme(theme: Theme) {
|
||||
if (typeof window !== "undefined" && window.NomadRoamTheme) {
|
||||
window.NomadRoamTheme.apply(theme);
|
||||
}
|
||||
}
|
||||
|
||||
export function readThemeFromDom(): Theme {
|
||||
if (typeof window !== "undefined" && window.NomadRoamTheme) {
|
||||
return window.NomadRoamTheme.read();
|
||||
}
|
||||
if (typeof document === "undefined") return "light";
|
||||
return document.documentElement.getAttribute("data-theme") === "dark"
|
||||
? "dark"
|
||||
: "light";
|
||||
}
|
||||
Reference in New Issue
Block a user