Skip to content

Commit

Permalink
fix: toggle update
Browse files Browse the repository at this point in the history
  • Loading branch information
meenakshi-deriv committed Mar 12, 2024
1 parent 438f3cc commit ae45ad8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
15 changes: 5 additions & 10 deletions lib/providers/theme/themeContext.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import { createContext } from "react";
import React from "react";

export type Theme = "dark" | "light";

export type ThemeContextValue = {
theme: Theme;
toggleTheme: (theme: Theme) => void;
export const initialThemeState = {
theme: "light" as Theme,
toggleTheme: (() => null) as () => void,
};

export const ThemeContext = createContext<ThemeContextValue>({
theme: "light",
toggleTheme: () => {},
});

export default ThemeContext;
export const ThemeContext = React.createContext(initialThemeState);
7 changes: 4 additions & 3 deletions lib/providers/theme/themeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ export const ThemeProvider = ({ children, theme }: ThemeProviderProps) => {

const systemPrefersDark = useMediaQuery("(prefers-color-scheme: dark)");

const toggleTheme = (updatedTheme: Theme) => {
setSelectedTheme(updatedTheme);
const toggleTheme = () => {
const newTheme = currentTheme === "dark" ? "light" : "dark";
setSelectedTheme(newTheme);
};

useEffect(() => {
Expand All @@ -36,7 +37,7 @@ export const ThemeProvider = ({ children, theme }: ThemeProviderProps) => {

return (
<ThemeContext.Provider value={{ theme: currentTheme, toggleTheme }}>
<div className={`theme--${currentTheme}`}>{children}</div>
<section className={`theme--${currentTheme}`}>{children}</section>
</ThemeContext.Provider>
);
};
Expand Down

0 comments on commit ae45ad8

Please sign in to comment.