Skip to content

Commit

Permalink
fix: remove theme logic (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
brettdorrans authored May 28, 2022
1 parent c83c698 commit a5c23a5
Showing 1 changed file with 2 additions and 25 deletions.
27 changes: 2 additions & 25 deletions src/components/theme-provider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, { Context, PropsWithChildren } from 'react';
import React, { PropsWithChildren } from 'react';
import {
ThemeProvider as StyledThemeProvider,
createGlobalStyle
} from 'styled-components';
import { mergeThemes, Theme } from '@lapidist/styles';
import { defaultTheme } from './defaultTheme';
import { darkTheme } from './darkTheme';

const GlobalStyle = createGlobalStyle`
html, body, div, span, applet, object, iframe,
Expand Down Expand Up @@ -51,36 +50,14 @@ export interface ThemeProviderProps extends PropsWithChildren<unknown> {
readonly theme?: Theme;
}

export const ThemeContext: Context<Theme> =
React.createContext<Theme>(defaultTheme);

export const useTheme = (): Theme => React.useContext(ThemeContext);

export const ThemeProvider: React.FC<ThemeProviderProps> = ({
children,
theme
}) => {
const initialTheme: Theme = useTheme();
const baseTheme: Theme | undefined = mergeThemes(initialTheme, theme);

const [themeContext, setThemeContext] = React.useState<Theme | undefined>(
baseTheme
);

React.useEffect(() => {
const prefersDarkTheme =
(window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches) ||
localStorage.getItem('isDarkMode') === 'true';
setThemeContext(
prefersDarkTheme ? mergeThemes(baseTheme, darkTheme) : baseTheme
);
}, [baseTheme, darkTheme]);

return (
<>
<GlobalStyle />
<StyledThemeProvider theme={themeContext}>
<StyledThemeProvider theme={mergeThemes(defaultTheme, theme)}>
{children}
</StyledThemeProvider>
</>
Expand Down

0 comments on commit a5c23a5

Please sign in to comment.