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