| 123456789101112131415161718192021222324252627282930313233343536 |
- import { createContext, useCallback, useContext, useState } from 'react';
- const ThemeContext = createContext(null);
- export const useTheme = () => useContext(ThemeContext);
- const SetThemeContext = createContext(null);
- export const useSetTheme = () => useContext(SetThemeContext);
- export const ThemeProvider = ({ children }) => {
- const [theme, _setTheme] = useState(() => {
- try {
- return localStorage.getItem('theme-mode') || null;
- } catch {
- return null;
- }
- });
- const setTheme = useCallback((input) => {
- _setTheme(input ? 'dark' : 'light');
- const body = document.body;
- if (!input) {
- body.removeAttribute('theme-mode');
- localStorage.setItem('theme-mode', 'light');
- } else {
- body.setAttribute('theme-mode', 'dark');
- localStorage.setItem('theme-mode', 'dark');
- }
- }, []);
- return (
- <SetThemeContext.Provider value={setTheme}>
- <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>
- </SetThemeContext.Provider>
- );
- };
|