mirror of
https://github.com/TheRealOwenRees/echecsfrance.git
synced 2026-11-18 07:22:06 +00:00
Reorganise code - use src folder
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { Dispatch, SetStateAction, useEffect, useState } from "react";
|
||||
|
||||
function useDarkMode(): [string, Dispatch<SetStateAction<string>>] {
|
||||
const prefersDarkMode =
|
||||
typeof window !== "undefined" &&
|
||||
window.matchMedia("(prefers-color-scheme: dark)").matches;
|
||||
|
||||
const [theme, setTheme] = useState(
|
||||
typeof window !== "undefined"
|
||||
? localStorage.theme || (prefersDarkMode ? "dark" : "light")
|
||||
: "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);
|
||||
}
|
||||
}, [colorTheme, theme]);
|
||||
|
||||
return [colorTheme, setTheme];
|
||||
}
|
||||
|
||||
export default useDarkMode;
|
||||
Reference in New Issue
Block a user