mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-18 16:11:45 +03:00
45 lines
936 B
TypeScript
45 lines
936 B
TypeScript
|
import { useEffect, useLayoutEffect, useState } from "react";
|
||
|
|
||
|
export type Theme = "dark" | "light" | "system";
|
||
|
|
||
|
/**
|
||
|
* @todo implement "system" theme
|
||
|
*/
|
||
|
export const useTheme = (): {
|
||
|
isDark: boolean;
|
||
|
isLight: boolean;
|
||
|
theme: Theme;
|
||
|
setTheme: (t: Theme) => void;
|
||
|
} => {
|
||
|
const [theme, setTheme] = useState<Theme>("light");
|
||
|
const isDark = theme === "dark";
|
||
|
const isLight = theme === "light";
|
||
|
|
||
|
useLayoutEffect(() => {
|
||
|
const savedTheme = localStorage.getItem("theme");
|
||
|
|
||
|
if (savedTheme === "dark") {
|
||
|
document.body.parentElement?.classList.add("dark");
|
||
|
|
||
|
setTheme(savedTheme);
|
||
|
}
|
||
|
}, []);
|
||
|
|
||
|
useEffect(() => {
|
||
|
if (isDark) {
|
||
|
document.body.parentElement?.classList.add("dark");
|
||
|
} else {
|
||
|
document.body.parentElement?.classList.remove("dark");
|
||
|
}
|
||
|
|
||
|
localStorage.setItem("theme", theme);
|
||
|
}, [theme, isDark]);
|
||
|
|
||
|
return {
|
||
|
isDark,
|
||
|
isLight,
|
||
|
theme,
|
||
|
setTheme,
|
||
|
};
|
||
|
};
|