quickabdest/contexts/Themes.js

35 lines
786 B
JavaScript
Raw Normal View History

2024-12-11 19:52:06 +03:00
import React, { useEffect, useState, createContext } from "react";
import { THEMES } from "../constants";
const initialState = {
theme: THEMES.DEFAULT,
setTheme: (theme) => {},
};
const ThemeContext = createContext(initialState);
function ThemeProvider({ children }) {
const [theme, setTheme] = useState(initialState.theme);
useEffect(() => {
const storedTheme = localStorage.getItem("theme");
if (storedTheme) {
setTheme(JSON.parse(storedTheme));
}
}, []);
const updateTheme = (theme) => {
localStorage.setItem("theme", JSON.stringify(theme));
setTheme(theme);
};
return (
<ThemeContext.Provider value={{ theme, setTheme: updateTheme }}>
{children}
</ThemeContext.Provider>
);
}
export { ThemeProvider, ThemeContext };