Skip to content

Commit

Permalink
feat: Add ThemeContext for dark mode support with
Browse files Browse the repository at this point in the history
  • Loading branch information
sweep-ai[bot] authored Feb 12, 2024
1 parent a8cdc1d commit bee1eb9
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/context/ThemeContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React, { createContext, useContext, ReactNode } from "react";
import { usePersistedState } from "../hooks/use-persisted-state";

type ThemeContextType = {
theme: string;
toggleTheme: () => void;
};

const defaultState: ThemeContextType = {
theme: "light",
toggleTheme: () => {},
};

const ThemeContext = createContext<ThemeContextType>(defaultState);

interface ThemeProviderProps {
children: ReactNode;
}

export const ThemeProvider = ({ children }: ThemeProviderProps) => {
const [theme, setTheme] = usePersistedState<string>("theme", {
encode: JSON.stringify,
decode: JSON.parse,
});

const toggleTheme = () => {
setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light"));
};

return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
{children}
</ThemeContext.Provider>
);
};

export const useTheme = () => useContext(ThemeContext);

0 comments on commit bee1eb9

Please sign in to comment.