import { useEffect, useState } from 'react'; import { off, on } from './misc/util'; var defaultState = { acceleration: { x: null, y: null, z: null, }, accelerationIncludingGravity: { x: null, y: null, z: null, }, rotationRate: { alpha: null, beta: null, gamma: null, }, interval: 16, }; var useMotion = function (initialState) { if (initialState === void 0) { initialState = defaultState; } var _a = useState(initialState), state = _a[0], setState = _a[1]; useEffect(function () { var handler = function (event) { var acceleration = event.acceleration, accelerationIncludingGravity = event.accelerationIncludingGravity, rotationRate = event.rotationRate, interval = event.interval; setState({ acceleration: { x: acceleration.x, y: acceleration.y, z: acceleration.z, }, accelerationIncludingGravity: { x: accelerationIncludingGravity.x, y: accelerationIncludingGravity.y, z: accelerationIncludingGravity.z, }, rotationRate: { alpha: rotationRate.alpha, beta: rotationRate.beta, gamma: rotationRate.gamma, }, interval: interval, }); }; on(window, 'devicemotion', handler); return function () { off(window, 'devicemotion', handler); }; }, []); return state; }; export default useMotion;