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

refactor: moved theme to config #21

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 0 additions & 3 deletions src/components/Theme/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './Theme';
export * from './Disclaimer';
14 changes: 14 additions & 0 deletions src/config/Theme/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getMuiThemeConfig } from './muiThemeConfig';
import { customTheme as rainbowTheme } from './rainbowTheme';
import { lightTheme, darkTheme } from './theme';

export const getCustomThemes = () => {
return {
main: {
light: lightTheme,
dark: darkTheme,
},
mui: getMuiThemeConfig,
0xShishigami marked this conversation as resolved.
Show resolved Hide resolved
rainbow: rainbowTheme,
};
};
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createPublicClient, http } from 'viem';
import { mainnet } from 'viem/chains';
import { Config } from '~/types';
import { getCustomThemes } from './Theme';
import { getConstants } from './constants';
import { getEnv } from './env';

Expand All @@ -13,4 +14,5 @@ export const publicClient = createPublicClient({
export const getConfig = (): Config => ({
...getEnv(),
...getConstants(),
...getCustomThemes(),
});
0xShishigami marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 4 additions & 2 deletions src/providers/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createContext, useEffect, useMemo, useState } from 'react';
import { ThemeProvider as MuiThemeProvider } from '@mui/material/styles';
import { getMuiThemeConfig } from '~/components';
import { getConfig } from '~/config';
import { Theme, ThemeName } from '~/types';
import { THEME_KEY, getTheme } from '~/utils';

Expand All @@ -17,6 +17,8 @@ interface StateProps {
export const ThemeContext = createContext({} as ContextType);

export const ThemeProvider = ({ children }: StateProps) => {
const getMuiThemeConfig = getConfig().mui;
0xShishigami marked this conversation as resolved.
Show resolved Hide resolved

const defaultTheme = 'dark';

const [theme, setTheme] = useState<ThemeName>(defaultTheme);
Expand All @@ -27,7 +29,7 @@ export const ThemeProvider = ({ children }: StateProps) => {
localStorage.setItem(THEME_KEY, newTheme);
setTheme(newTheme);
};
const muiTheme = useMemo(() => getMuiThemeConfig(currentTheme, theme), [currentTheme, theme]);
const muiTheme = useMemo(() => getMuiThemeConfig(currentTheme, theme), [currentTheme, theme, getMuiThemeConfig]);

// Load theme from local storage on load
useEffect(() => {
Expand Down
4 changes: 3 additions & 1 deletion src/types/Config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CustomThemes } from '~/types';

export interface Env {
RPC_URL: string;
PROJECT_ID: string;
Expand All @@ -8,4 +10,4 @@ export interface Constants {
//...
}

export interface Config extends Env, Constants {}
export interface Config extends Env, Constants, CustomThemes {}
12 changes: 12 additions & 0 deletions src/types/Theme.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { Theme as MuiTheme } from '@mui/material';
import { Theme as RainbowTheme } from '@rainbow-me/rainbowkit';

export type ThemeName = 'light' | 'dark';

export interface Theme {
Expand All @@ -17,3 +20,12 @@ export interface Theme {
export interface PropTheme {
theme: Theme;
}

export interface CustomThemes {
main: {
light: Theme;
dark: Theme;
};
mui: (currentTheme: Theme, themeName: ThemeName) => MuiTheme;
0xShishigami marked this conversation as resolved.
Show resolved Hide resolved
rainbow: RainbowTheme;
}
2 changes: 1 addition & 1 deletion src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { connectorsForWallets } from '@rainbow-me/rainbowkit';
import { rainbowWallet, walletConnectWallet, injectedWallet } from '@rainbow-me/rainbowkit/wallets';
import { createConfig, http, cookieStorage, createStorage } from 'wagmi';
import { localhost, sepolia } from 'wagmi/chains';
import { getConfig } from '../config';
import { getConfig } from '~/config';

const { PROJECT_ID } = getConfig();

Expand Down
10 changes: 6 additions & 4 deletions src/utils/getTheme.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { darkTheme, lightTheme } from '~/components/Theme';
import { getConfig } from '~/config';
import { Theme, ThemeName } from '~/types';

export const getTheme = (theme?: ThemeName): Theme => {
const { dark, light } = getConfig().main;

0xShishigami marked this conversation as resolved.
Show resolved Hide resolved
switch (theme) {
case 'light':
return lightTheme;
return light;
case 'dark':
return darkTheme;
return dark;
default:
return lightTheme;
return light;
}
};
Loading