Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
876 B · 26 lines
TypeScript
at main
123456789101112131415161718192021222324252627import { useCallback, useState } from 'react';
import useEventListener from './useEventListener.js';
type DeviceOrientation = Pick<DeviceOrientationEvent, 'absolute' | 'alpha' | 'beta' | 'gamma'>;
const isBrowser = typeof window !== 'undefined';
/** * Returns the device's current physical orientation. * * @returns {DeviceOrientation | null} Object with the device's current orientation */export default function useDeviceOrientation(): DeviceOrientation | null { const [orientation, setOrientation] = useState<DeviceOrientation | null>(null);
const handleDeviceOrientation = useCallback((event: DeviceOrientationEvent) => { const { absolute, alpha, beta, gamma } = event;
setOrientation({ absolute, alpha, beta, gamma }); }, []);
useEventListener(isBrowser ? window : null, 'deviceorientation', handleDeviceOrientation);
return orientation;}