UNPKG

@mankindui/core

Version:

- a react component library - you can see how @mankindui/core can be used via examples: [usage examples](https://github.com/clarklindev/mankindui-test)

61 lines (60 loc) 1.55 kB
import { j as h } from "../_virtual/jsx-runtime.js"; import { createContext as i, useReducer as d, useEffect as u, useContext as S } from "react"; const s = { COLORSCHEME: "color-scheme" //context }, C = () => localStorage.getItem("colorScheme") || window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light", n = { colorScheme: C() }, p = { // eslint-disable-next-line @typescript-eslint/no-unused-vars setColorScheme: (e) => { } }, m = i({ ...n, ...p }), x = (e) => { const o = document.documentElement; e === "dark" ? o.classList.add("dark") : (o.classList.remove("dark"), o.removeAttribute("class")); }; function T({ children: e }) { const o = (t, r) => { switch (r.type) { case s.COLORSCHEME: return { ...t, colorScheme: r.payload.colorScheme }; default: return t; } }, [c, a] = d(o, n); u(() => { x(c.colorScheme); }, [c.colorScheme]); const l = { ...c, setColorScheme: (t) => { c.colorScheme !== t && (localStorage.setItem("colorScheme", t), a({ type: s.COLORSCHEME, payload: { colorScheme: t } })); } }; return ( //pass the context with updated state /* @__PURE__ */ h.jsx(m.Provider, { value: l, children: e }) ); } const w = () => { const e = S(m); if (!e) throw new Error("useTheme must be used within a ThemeContextProvider"); return e; }; export { T as ThemeContextProvider, w as useTheme }; //# sourceMappingURL=ThemeContext.js.map