2021-09-13 20:41:05 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Dimensions } from 'react-native';
|
|
|
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
|
|
|
2022-01-17 16:10:39 +00:00
|
|
|
import { TNavigationOptions } from './definitions/navigationTypes';
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
export interface IDimensionsContextProps {
|
|
|
|
width: number;
|
2022-03-22 20:44:27 +00:00
|
|
|
height: number;
|
2022-03-31 12:38:20 +00:00
|
|
|
scale: number;
|
2022-03-25 18:09:02 +00:00
|
|
|
fontScale: number;
|
2022-03-22 20:44:27 +00:00
|
|
|
setDimensions?: ({
|
2021-09-13 20:41:05 +00:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
scale,
|
|
|
|
fontScale
|
|
|
|
}: {
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
scale: number;
|
|
|
|
fontScale: number;
|
|
|
|
}) => void;
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:09:02 +00:00
|
|
|
export const DimensionsContext = React.createContext<IDimensionsContextProps>(
|
|
|
|
Dimensions.get('window') as IDimensionsContextProps
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-01-17 16:10:39 +00:00
|
|
|
export function withDimensions<T extends object>(Component: React.ComponentType<T> & TNavigationOptions): typeof Component {
|
|
|
|
const DimensionsComponent = (props: T) => (
|
2021-09-13 20:41:05 +00:00
|
|
|
<DimensionsContext.Consumer>{contexts => <Component {...props} {...contexts} />}</DimensionsContext.Consumer>
|
|
|
|
);
|
2022-01-17 16:10:39 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
hoistNonReactStatics(DimensionsComponent, Component);
|
|
|
|
return DimensionsComponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useDimensions = () => React.useContext(DimensionsContext);
|
|
|
|
|
|
|
|
export const useOrientation = () => {
|
|
|
|
const { width, height } = React.useContext(DimensionsContext);
|
2022-03-22 20:44:27 +00:00
|
|
|
const isPortrait = height > width;
|
2021-09-13 20:41:05 +00:00
|
|
|
return {
|
|
|
|
isPortrait,
|
|
|
|
isLandscape: !isPortrait
|
|
|
|
};
|
|
|
|
};
|