UNPKG

@iterable/react-native-sdk

Version:
44 lines (41 loc) 1.41 kB
"use strict"; import { useEffect, useState } from 'react'; import { useWindowDimensions } from '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 */ export function useDeviceOrientation() { const { height, width } = useWindowDimensions(); const [isPortrait, setIsPortrait] = useState(height >= width); 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