2020-06-17 17:35:58 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { Dimensions } from 'react-native';
|
|
|
|
import hoistNonReactStatics from 'hoist-non-react-statics';
|
|
|
|
|
2021-07-15 19:47:53 +00:00
|
|
|
export interface IDimensionsContextProps {
|
2021-07-15 13:54:47 +00:00
|
|
|
width: number;
|
2021-07-15 19:47:53 +00:00
|
|
|
height?: number;
|
2021-07-15 13:54:47 +00:00
|
|
|
scale: number;
|
|
|
|
fontScale: number;
|
|
|
|
setDimensions: ({ width, height, scale, fontScale }: { width: number; height: number; scale: number; fontScale: number; }) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const DimensionsContext = React.createContext<Partial<IDimensionsContextProps>>(Dimensions.get('window'));
|
2020-06-17 17:35:58 +00:00
|
|
|
|
2021-07-15 13:54:47 +00:00
|
|
|
export function withDimensions(Component: any) {
|
|
|
|
const DimensionsComponent = (props: any) => (
|
2020-06-17 17:35:58 +00:00
|
|
|
<DimensionsContext.Consumer>
|
|
|
|
{contexts => <Component {...props} {...contexts} />}
|
|
|
|
</DimensionsContext.Consumer>
|
|
|
|
);
|
|
|
|
hoistNonReactStatics(DimensionsComponent, Component);
|
|
|
|
return DimensionsComponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useDimensions = () => React.useContext(DimensionsContext);
|
|
|
|
|
|
|
|
export const useOrientation = () => {
|
|
|
|
const { width, height } = React.useContext(DimensionsContext);
|
2021-07-15 13:54:47 +00:00
|
|
|
const isPortrait = (height)! > (width)!;
|
2020-06-17 17:35:58 +00:00
|
|
|
return {
|
|
|
|
isPortrait,
|
|
|
|
isLandscape: !isPortrait
|
|
|
|
};
|
|
|
|
};
|