2021-09-13 20:41:05 +00:00
|
|
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
2022-03-31 23:04:29 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
import { IThemePreference } from './definitions/ITheme';
|
2022-03-31 23:04:29 +00:00
|
|
|
import { TNavigationOptions } from './definitions/navigationTypes';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { colors } from './lib/constants';
|
2022-03-31 23:04:29 +00:00
|
|
|
|
|
|
|
export type TSupportedThemes = keyof typeof colors;
|
|
|
|
export type TColors = typeof colors[TSupportedThemes];
|
2022-01-12 12:54:04 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
interface IThemeContextProps {
|
|
|
|
theme: string;
|
2022-01-12 12:54:04 +00:00
|
|
|
themePreferences?: IThemePreference;
|
2021-10-20 16:42:44 +00:00
|
|
|
setTheme?: (newTheme?: {}) => void;
|
2022-03-31 23:04:29 +00:00
|
|
|
colors: TColors;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-31 23:04:29 +00:00
|
|
|
export const ThemeContext = React.createContext<IThemeContextProps>({ theme: 'light', colors: colors.light });
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-01-17 16:10:39 +00:00
|
|
|
export function withTheme<T extends object>(Component: React.ComponentType<T> & TNavigationOptions): typeof Component {
|
|
|
|
const ThemedComponent = (props: T) => (
|
2021-09-13 20:41:05 +00:00
|
|
|
<ThemeContext.Consumer>{contexts => <Component {...props} {...contexts} />}</ThemeContext.Consumer>
|
|
|
|
);
|
2022-01-17 16:10:39 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
hoistNonReactStatics(ThemedComponent, Component);
|
|
|
|
return ThemedComponent;
|
|
|
|
}
|
|
|
|
|
2022-01-17 16:10:39 +00:00
|
|
|
export const useTheme = (): IThemeContextProps => React.useContext(ThemeContext);
|