2021-09-13 20:41:05 +00:00
|
|
|
import React from 'react';
|
|
|
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
|
|
|
|
|
|
interface IThemeContextProps {
|
|
|
|
theme: string;
|
2021-10-20 16:42:44 +00:00
|
|
|
themePreferences?: {
|
2021-09-13 20:41:05 +00:00
|
|
|
currentTheme: 'automatic' | 'light';
|
|
|
|
darkLevel: string;
|
|
|
|
};
|
2021-10-20 16:42:44 +00:00
|
|
|
setTheme?: (newTheme?: {}) => void;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 16:42:44 +00:00
|
|
|
export const ThemeContext = React.createContext<IThemeContextProps>({ theme: 'light' });
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2021-10-29 15:56:57 +00:00
|
|
|
export function withTheme<P extends object>(Component: React.ComponentType<P>): (props: any) => JSX.Element {
|
2021-09-13 20:41:05 +00:00
|
|
|
const ThemedComponent = (props: any) => (
|
|
|
|
<ThemeContext.Consumer>{contexts => <Component {...props} {...contexts} />}</ThemeContext.Consumer>
|
|
|
|
);
|
|
|
|
hoistNonReactStatics(ThemedComponent, Component);
|
|
|
|
return ThemedComponent;
|
|
|
|
}
|
|
|
|
|
2021-10-13 21:09:50 +00:00
|
|
|
export const useTheme = (): Partial<IThemeContextProps> => React.useContext(ThemeContext);
|