mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-07-23 12:36:57 +00:00
24 lines
620 B
TypeScript
24 lines
620 B
TypeScript
import { Dispatch, SetStateAction, useEffect, useState } from "react";
|
|
|
|
function useDarkMode(): [string, Dispatch<SetStateAction<string>>] {
|
|
const [theme, setTheme] = useState(
|
|
typeof window !== "undefined" ? localStorage.theme : "dark"
|
|
);
|
|
const colorTheme = theme === "dark" ? "light" : "dark";
|
|
|
|
useEffect(() => {
|
|
const root = window.document.documentElement;
|
|
|
|
root.classList.remove(colorTheme);
|
|
root.classList.add(theme);
|
|
|
|
if (typeof window !== "undefined") {
|
|
localStorage.setItem("theme", theme);
|
|
}
|
|
}, [theme]);
|
|
|
|
return [colorTheme, setTheme];
|
|
}
|
|
|
|
export default useDarkMode;
|