43 lines
1.4 KiB
JavaScript
43 lines
1.4 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var react_1 = require("react");
|
|
var util_1 = require("./misc/util");
|
|
var defaultState = {
|
|
angle: 0,
|
|
type: 'landscape-primary',
|
|
};
|
|
var useOrientation = function (initialState) {
|
|
if (initialState === void 0) { initialState = defaultState; }
|
|
var _a = react_1.useState(initialState), state = _a[0], setState = _a[1];
|
|
react_1.useEffect(function () {
|
|
var screen = window.screen;
|
|
var mounted = true;
|
|
var onChange = function () {
|
|
if (mounted) {
|
|
var orientation_1 = screen.orientation;
|
|
if (orientation_1) {
|
|
var angle = orientation_1.angle, type = orientation_1.type;
|
|
setState({ angle: angle, type: type });
|
|
}
|
|
else if (window.orientation !== undefined) {
|
|
setState({
|
|
angle: typeof window.orientation === 'number' ? window.orientation : 0,
|
|
type: '',
|
|
});
|
|
}
|
|
else {
|
|
setState(initialState);
|
|
}
|
|
}
|
|
};
|
|
util_1.on(window, 'orientationchange', onChange);
|
|
onChange();
|
|
return function () {
|
|
mounted = false;
|
|
util_1.off(window, 'orientationchange', onChange);
|
|
};
|
|
}, []);
|
|
return state;
|
|
};
|
|
exports.default = useOrientation;
|