2021-10-01 18:12:19 +00:00
|
|
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
2022-04-28 18:44:40 +00:00
|
|
|
import React from 'react';
|
2021-10-01 18:12:19 +00:00
|
|
|
|
2022-01-24 20:12:25 +00:00
|
|
|
import { IThemePreference } from './definitions/ITheme';
|
2022-04-28 18:44:40 +00:00
|
|
|
import { TNavigationOptions } from './definitions/navigationTypes';
|
|
|
|
import { colors } from './lib/constants';
|
|
|
|
|
|
|
|
export type TSupportedThemes = keyof typeof colors;
|
|
|
|
export type TColors = typeof colors[TSupportedThemes];
|
2022-01-24 20:12:25 +00:00
|
|
|
|
2022-05-30 22:01:25 +00:00
|
|
|
export interface IThemeContextProps {
|
2022-04-28 18:44:40 +00:00
|
|
|
theme: TSupportedThemes;
|
2022-01-24 20:12:25 +00:00
|
|
|
themePreferences?: IThemePreference;
|
2021-10-28 17:49:08 +00:00
|
|
|
setTheme?: (newTheme?: {}) => void;
|
2022-04-28 18:44:40 +00:00
|
|
|
colors: TColors;
|
2021-10-01 18:12:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 18:44:40 +00:00
|
|
|
export const ThemeContext = React.createContext<IThemeContextProps>({ theme: 'light', colors: colors.light });
|
2021-10-01 18:12:19 +00:00
|
|
|
|
2022-01-24 20:12:25 +00:00
|
|
|
export function withTheme<T extends object>(Component: React.ComponentType<T> & TNavigationOptions): typeof Component {
|
|
|
|
const ThemedComponent = (props: T) => (
|
2021-10-01 18:12:19 +00:00
|
|
|
<ThemeContext.Consumer>{contexts => <Component {...props} {...contexts} />}</ThemeContext.Consumer>
|
|
|
|
);
|
2022-01-24 20:12:25 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
hoistNonReactStatics(ThemedComponent, Component);
|
|
|
|
return ThemedComponent;
|
|
|
|
}
|
|
|
|
|
2022-01-24 20:12:25 +00:00
|
|
|
export const useTheme = (): IThemeContextProps => React.useContext(ThemeContext);
|