Skip to content

Commit

Permalink
refactor: moved theme to config
Browse files Browse the repository at this point in the history
  • Loading branch information
Onizuka-wl committed Oct 2, 2024
1 parent d60d793 commit dec50f2
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 12 deletions.
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,
rainbow: rainbowTheme,
};
};
File renamed without changes.
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(),
});
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;

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 { CutomThemes } 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, CutomThemes {}
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 CutomThemes {
main: {
light: Theme;
dark: Theme;
};
mui: (currentTheme: Theme, themeName: ThemeName) => MuiTheme;
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;

switch (theme) {
case 'light':
return lightTheme;
return light;
case 'dark':
return darkTheme;
return dark;
default:
return lightTheme;
return light;
}
};

0 comments on commit dec50f2

Please sign in to comment.