leumas-universal-crud-react
Version:
Leumas Universal CRUD to a dynamic Endpoint, Setup your own Dynamic Endpoint and Use Leumas API to send to your MONGO clusters
39 lines (32 loc) • 1.08 kB
JSX
import { createContext, useContext, useState, useEffect } from 'react';
const ThemeContext = createContext();
export const useTheme = () => {
return useContext(ThemeContext);
};
export const ThemeProvider = ({ children }) => {
const [theme, setTheme] = useState(() => {
// Initially get theme from localStorage or fallback to 'light'
const savedTheme = localStorage.getItem('theme');
if(savedTheme === undefined || savedTheme === '' || savedTheme === null){
setTheme('light')
}
return savedTheme || 'light';
});
useEffect(() => {
// Save theme to localStorage whenever it changes
localStorage.setItem('theme', theme);
}, [theme]);
const toggleTheme = () => {
setTheme((prevTheme) => {
const newTheme = prevTheme === 'light' ? 'dark' : 'light';
return newTheme;
});
};
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
<div className={`theme-transition theme-${theme}`}>
{children}
</div>
</ThemeContext.Provider>
);
};