the-platform
Version:
React Hooks and Suspense-ready Components for Web API's and elements
57 lines (45 loc) • 1.84 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useDeviceMotion = void 0;
var _react = _interopRequireDefault(require("react"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
var useDeviceMotion = function useDeviceMotion() {
var _React$useState = _react.default.useState({
acceleration: {
x: null,
y: null,
z: null
},
accelerationIncludingGravity: {
x: null,
y: null,
z: null
},
rotationRate: {
alpha: null,
beta: null,
gamma: null
},
interval: 0
}),
_React$useState2 = _slicedToArray(_React$useState, 2),
motion = _React$useState2[0],
setMotion = _React$useState2[1];
_react.default.useEffect(function () {
var handle = function handle(deviceMotionEvent) {
setMotion(deviceMotionEvent);
};
window.addEventListener('devicemotion', handle);
return function () {
window.removeEventListener('devicemotion', handle);
};
}, []);
return motion;
};
exports.useDeviceMotion = useDeviceMotion;