@iterable/react-native-sdk
Version:
Iterable SDK for React Native.
47 lines (45 loc) • 1.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useDeviceOrientation = useDeviceOrientation;
var _react = require("react");
var _reactNative = require("react-native");
/**
* Represents the device orientation.
*/
/* eslint-disable tsdoc/syntax */
/**
* Custom hook to get the current device orientation.
*
* This hook returns the height, width, and a boolean indicating if the device is in portrait mode.
* It listens to changes in the window dimensions and updates the orientation accordingly.
*
* @returns {IterableDeviceOrientation} An object containing the height, width, and a boolean `isPortrait` indicating if the device is in portrait mode.
*
* @example
* const { height, width, isPortrait } = useDeviceOrientation();
*
* @remarks
* The `useEffect` hook only includes `width` in its dependency array. This is because the height and width are typically updated together,
* and including only `width` prevents unnecessary re-renders.
*/
/* eslint-enable tsdoc/syntax */
function useDeviceOrientation() {
const {
height,
width
} = (0, _reactNative.useWindowDimensions)();
const [isPortrait, setIsPortrait] = (0, _react.useState)(height >= width);
(0, _react.useEffect)(() => {
setIsPortrait(height >= width);
// MOB-10425: why is height not included in the dependency array?
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [width]);
return {
height,
width,
isPortrait
};
}
//# sourceMappingURL=useDeviceOrientation.js.map