Skip to content

Commit

Permalink
Merge pull request #24 from deriv-com/meenu-fix-toggle-issue
Browse files Browse the repository at this point in the history
  • Loading branch information
prince-deriv authored Mar 12, 2024
2 parents 05e83d0 + 7748378 commit 6dc6a46
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
6 changes: 3 additions & 3 deletions lib/providers/theme/themeContext.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { createContext } from "react";
import React from "react";

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

export type ThemeContextValue = {
theme: Theme;
toggleTheme: (theme: Theme) => void;
toggleTheme: () => void;
};

export const ThemeContext = createContext<ThemeContextValue>({
export const ThemeContext = React.createContext<ThemeContextValue>({
theme: "light",
toggleTheme: () => {},
});
Expand Down
16 changes: 4 additions & 12 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 @@ -25,18 +26,9 @@ export const ThemeProvider = ({ children, theme }: ThemeProviderProps) => {
setCurrentTheme(theme);
}, [selectedTheme, systemPrefersDark]);

useEffect(() => {
const root = document.documentElement;
if (currentTheme === "dark") {
root.classList.add("theme--dark");
} else {
root.classList.remove("theme--dark");
}
}, [currentTheme]);

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 6dc6a46

Please sign in to comment.