From 611d7e40116419bb8abbf0829634ecd7f988cffa Mon Sep 17 00:00:00 2001 From: Yashvardhan Jagnani Date: Tue, 9 Apr 2024 16:22:33 +0530 Subject: [PATCH 1/2] fix: theme selection --- src/utils/store/GoldRush.tsx | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/utils/store/GoldRush.tsx b/src/utils/store/GoldRush.tsx index 702e801e..1d9084d5 100644 --- a/src/utils/store/GoldRush.tsx +++ b/src/utils/store/GoldRush.tsx @@ -74,12 +74,14 @@ export const GoldRushProvider: React.FC = ({ useEffect(() => { const existingTheme = localStorage.getItem("goldrush_theme") || null; + if (!existingTheme) { + applyThemeHandler(defaultTheme); + } else { + applyThemeHandler(JSON.parse(existingTheme)); + } + if (newTheme) { updateThemeHandler(newTheme); - } else if (existingTheme) { - applyThemeHandler(JSON.parse(existingTheme)); - } else { - applyThemeHandler(defaultTheme); } }, []); From 1d5c6131a54062a15f9026fba8bb541ab8c34d6d Mon Sep 17 00:00:00 2001 From: Yashvardhan Jagnani Date: Tue, 9 Apr 2024 17:25:49 +0530 Subject: [PATCH 2/2] fix(theme): state updation --- src/utils/store/GoldRush.tsx | 151 +++++++++++++---------------------- 1 file changed, 56 insertions(+), 95 deletions(-) diff --git a/src/utils/store/GoldRush.tsx b/src/utils/store/GoldRush.tsx index 1d9084d5..b256ff2b 100644 --- a/src/utils/store/GoldRush.tsx +++ b/src/utils/store/GoldRush.tsx @@ -10,11 +10,11 @@ import { type GoldRushThemeType, type GoldRushContextType, type GoldRushProviderProps, - type GoldRushThemeColorType, } from "../types/store.types"; import { type ChainItem, CovalentClient } from "@covalenthq/client-sdk"; import { primaryShades } from "../functions"; import { SEARCH_RESULTS_TYPE } from "../constants/shared.constants"; +import defaultsDeep from "lodash/defaultsDeep"; const GoldRushContext = createContext( {} as GoldRushContextType @@ -55,7 +55,12 @@ export const GoldRushProvider: React.FC = ({ const [chains, setChains] = useState(null); const [selectedChain, setSelectedChain] = useState(null); - const [theme, setTheme] = useState(defaultTheme); + const [theme, setTheme] = useState( + defaultsDeep( + JSON.parse(localStorage.getItem("goldrush_theme") || "null") ?? {}, + defaultsDeep(newTheme, defaultTheme) + ) + ); useEffect(() => { (async () => { @@ -72,113 +77,69 @@ export const GoldRushProvider: React.FC = ({ }, []); useEffect(() => { - const existingTheme = localStorage.getItem("goldrush_theme") || null; - - if (!existingTheme) { - applyThemeHandler(defaultTheme); - } else { - applyThemeHandler(JSON.parse(existingTheme)); - } - - if (newTheme) { - updateThemeHandler(newTheme); - } - }, []); + localStorage.setItem("goldrush_theme", JSON.stringify(theme)); + const { borderRadius, colors, mode, style } = theme; - const updateThemeHandler = useCallback( - ({ borderRadius, colors, mode, style }: Partial) => { - const updatedTheme: GoldRushThemeType = { ...theme }; + const body = document.body; + const root = document.documentElement; - if (borderRadius) { - updatedTheme.borderRadius = borderRadius; - } - if (mode) { - updatedTheme.mode = mode; + switch (mode) { + case "dark": { + body.classList.add("dark"); + root.classList.add("dark"); + break; } - if (style) { - updatedTheme.style = style; + case "light": { + body.classList.remove("dark"); + root.classList.remove("dark"); + break; } - if (colors) { - Object.entries(colors).forEach(([_mode, _types]) => { - Object.entries(_types).forEach(([_type, value]) => { - updatedTheme.colors[_mode as "dark" | "light"]![ - _type as keyof GoldRushThemeColorType - ] = value; - }); - }); - } - - applyThemeHandler(updatedTheme); - }, - [theme] - ); - - const applyThemeHandler = useCallback( - ({ borderRadius, colors, mode, style }: GoldRushThemeType) => { - const body = document.body; - const root = document.documentElement; + } - switch (mode) { - case "dark": { - body.classList.add("dark"); - root.classList.add("dark"); - break; - } - case "light": { - body.classList.remove("dark"); - root.classList.remove("dark"); - break; - } + switch (style) { + case "neo": { + body.classList.add("neo"); + root.classList.add("neo"); + break; } - - switch (style) { - case "neo": { - body.classList.add("neo"); - root.classList.add("neo"); - break; - } - case "classic": { - body.classList.remove("neo"); - root.classList.remove("neo"); - break; - } + case "classic": { + body.classList.remove("neo"); + root.classList.remove("neo"); + break; } + } - root.style.setProperty("--grk-border-radius", `${borderRadius}px`); + root.style.setProperty("--grk-border-radius", `${borderRadius}px`); - Object.entries(colors).forEach(([_mode, _types]) => { - Object.entries(_types).forEach(([_type, value]) => { - if (_type === "primary") { - const shades = primaryShades( - value, - _mode as GoldRushThemeType["mode"] - ); - Object.entries(shades).forEach(([shade, color]) => { - root.style.setProperty( - `--grk-${_type}-${_mode}-${shade}`, - color - ); - }); - } else { + Object.entries(colors).forEach(([_mode, _types]) => { + Object.entries(_types).forEach(([_type, value]) => { + if (_type === "primary") { + const shades = primaryShades( + value, + _mode as GoldRushThemeType["mode"] + ); + Object.entries(shades).forEach(([shade, color]) => { root.style.setProperty( - `--grk-${_type}-${_mode}`, - value + `--grk-${_type}-${_mode}-${shade}`, + color ); - } - }); + }); + } else { + root.style.setProperty(`--grk-${_type}-${_mode}`, value); + } }); + }); + }, [theme]); - const _theme: GoldRushThemeType = { - borderRadius, - colors, - mode, - style, - }; - - setTheme(_theme); - localStorage.setItem("goldrush_theme", JSON.stringify(_theme)); + const updateThemeHandler = useCallback( + (updateTheme: Partial) => { + const updatedTheme: GoldRushThemeType = defaultsDeep( + updateTheme, + theme + ); + setTheme(updatedTheme); }, - [] + [theme] ); const resetThemeHandler = useCallback(() => {