Untitled

mail@pastecode.io avatar
unknown
typescript
a year ago
670 B
17
Indexable
Never
import { createContext, ReactNode, useMemo, useState } from "react";

export interface ITheme {
  switchTheme: () => void;
  theme: string;
};

interface IProps {
  children: ReactNode;
}

export const ThemeContext =
  createContext<ITheme | null>(null);

export const ThemeContextProvider = ({ children }: IProps) => {
  const [theme, setTheme] = useState<string>("light")

  const switchTheme = () => {
    setTheme(prev => prev === "light" ? "dark" : "light");
  }

  const contextValues = useMemo(
    () => ({ theme, switchTheme }),
    [theme]
  );

  return (
    <ThemeContext.Provider value={contextValues}>
      {children}
    </ThemeContext.Provider>
  );
};