Skip to content

Commit

Permalink
feat: use dynamic custom theme
Browse files Browse the repository at this point in the history
  • Loading branch information
barinali committed Aug 11, 2023
1 parent b590f0f commit f6c500c
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions packages/web/src/components/ThemeProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
import * as React from 'react';
import { ThemeProvider as BaseThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import { ThemeProvider as BaseThemeProvider } from '@mui/material/styles';
import get from 'lodash/get';
import set from 'lodash/set';
import * as React from 'react';

import { IJSONObject } from '@automatisch/types';
import useConfig from 'hooks/useConfig';
import theme from 'styles/theme';

type ThemeProviderProps = {
children: React.ReactNode;
};

const customizeTheme = (defaultTheme: typeof theme, config: IJSONObject) => {
for (const key in config) {
const value = config[key];
const exists = get(defaultTheme, key);

if (exists) {
set(defaultTheme, key, value);
}
}

return defaultTheme;
};

const ThemeProvider = ({
children,
...props
}: ThemeProviderProps): React.ReactElement => {
const { config, loading } = useConfig();

const customTheme = React.useMemo(() => {
if (!config) return theme;

const customTheme = customizeTheme(theme, config);

return customTheme;
}, [config]);

// TODO: maybe a global loading state for the custom theme?
if (loading) return <></>;

return (
<BaseThemeProvider theme={theme} {...props}>
<BaseThemeProvider theme={customTheme} {...props}>
<CssBaseline />

{children}
Expand Down

0 comments on commit f6c500c

Please sign in to comment.