stackpress
Version:
Incept is a content management framework.
20 lines (19 loc) • 738 B
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { useState, useEffect } from 'react';
import UniversalCookie from 'universal-cookie';
import ThemeContext from './ThemeContext.js';
const cookie = new UniversalCookie();
export default function ThemeProvider(props) {
const { children, theme: init = 'light' } = props;
const [theme, setTheme] = useState(init);
const toggle = () => {
const newTheme = theme === 'light' ? 'dark' : 'light';
setTheme(newTheme);
cookie.set('theme', newTheme);
};
const value = { theme, toggle };
useEffect(() => {
setTheme(cookie.get('theme') || init);
}, []);
return (_jsx(ThemeContext.Provider, { value: value, children: children }));
}