UNPKG

1.55 kBJavaScriptView Raw
1"use strict";
2var __importStar = (this && this.__importStar) || function (mod) {
3 if (mod && mod.__esModule) return mod;
4 var result = {};
5 if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
6 result["default"] = mod;
7 return result;
8};
9Object.defineProperty(exports, "__esModule", { value: true });
10var React = __importStar(require("react"));
11var useKeyDown_1 = require("./useKeyDown");
12var getOrientation = function () {
13 if (typeof window === 'undefined') {
14 return 'portrait';
15 }
16 if (window.screen && window.screen.orientation && window.screen.orientation.angle) {
17 return Math.abs(window.screen.orientation.angle) === 90 ? 'landscape' : 'portrait';
18 }
19 // Support IOS safari
20 if (window.orientation) {
21 return Math.abs(Number(window.orientation)) === 90 ? 'landscape' : 'portrait';
22 }
23 return 'portrait';
24};
25function useIsLandscape(customOrientation) {
26 var _a = React.useState(getOrientation()), orientation = _a[0], setOrientation = _a[1];
27 var eventHandler = React.useCallback(function () { return setOrientation(getOrientation()); }, []);
28 useKeyDown_1.useIsomorphicEffect(function () {
29 window.addEventListener('orientationchange', eventHandler);
30 return function () { return window.removeEventListener('orientationchange', eventHandler); };
31 }, [eventHandler]);
32 var orientationToUse = customOrientation || orientation;
33 return orientationToUse === 'landscape';
34}
35exports.useIsLandscape = useIsLandscape;