Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yj-fix/themeing-state #151

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 56 additions & 93 deletions src/utils/store/GoldRush.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<GoldRushContextType>(
{} as GoldRushContextType
Expand Down Expand Up @@ -55,7 +55,12 @@ export const GoldRushProvider: React.FC<GoldRushProviderProps> = ({

const [chains, setChains] = useState<ChainItem[] | null>(null);
const [selectedChain, setSelectedChain] = useState<ChainItem | null>(null);
const [theme, setTheme] = useState<GoldRushThemeType>(defaultTheme);
const [theme, setTheme] = useState<GoldRushThemeType>(
defaultsDeep(
JSON.parse(localStorage.getItem("goldrush_theme") || "null") ?? {},
defaultsDeep(newTheme, defaultTheme)
)
);

useEffect(() => {
(async () => {
Expand All @@ -72,111 +77,69 @@ export const GoldRushProvider: React.FC<GoldRushProviderProps> = ({
}, []);

useEffect(() => {
const existingTheme = localStorage.getItem("goldrush_theme") || null;

if (newTheme) {
updateThemeHandler(newTheme);
} else if (existingTheme) {
applyThemeHandler(JSON.parse(existingTheme));
} else {
applyThemeHandler(defaultTheme);
}
}, []);
localStorage.setItem("goldrush_theme", JSON.stringify(theme));
const { borderRadius, colors, mode, style } = theme;

const updateThemeHandler = useCallback(
({ borderRadius, colors, mode, style }: Partial<GoldRushThemeType>) => {
const updatedTheme: GoldRushThemeType = { ...theme };
const body = document.body;
const root = document.documentElement;

if (borderRadius) {
updatedTheme.borderRadius = borderRadius;
switch (mode) {
case "dark": {
body.classList.add("dark");
root.classList.add("dark");
break;
}
if (mode) {
updatedTheme.mode = mode;
}
if (style) {
updatedTheme.style = style;
}
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;
});
});
case "light": {
body.classList.remove("dark");
root.classList.remove("dark");
break;
}
}

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<GoldRushThemeType>) => {
const updatedTheme: GoldRushThemeType = defaultsDeep(
updateTheme,
theme
);
setTheme(updatedTheme);
},
[]
[theme]
);

const resetThemeHandler = useCallback(() => {
Expand Down
Loading