UNPKG

use-screen-orientation

Version:

hook for handling screen orientation in react projects for web

36 lines (35 loc) 1.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = require("react"); var useScreenOrientation = function () { var _a = (0, react_1.useState)([ 'portrait', 0, ]), orientation = _a[0], setOrientation = _a[1]; var calc = function () { var orientation, angle; if (window.screen.orientation) { orientation = Math.abs(window.screen.orientation.angle) === 90 ? 'landscape' : 'portrait', angle = window.screen.orientation.angle; } else { orientation = Math.abs(window.orientation) === 90 ? "landscape" : "portrait"; angle = window.orientation; } return [orientation, angle]; }; var orientationEvent = function () { setOrientation(calc()); }; (0, react_1.useEffect)(function () { if ('onorientationchange' in window) { window.addEventListener('orientationchange', orientationEvent); setOrientation(calc()); } return function () { window.removeEventListener('orientationchange', orientationEvent); }; }, []); return orientation; }; exports.default = useScreenOrientation;