Skip to content

Commit 74c3cd8

Browse files
committed
#202 🧊 rework use device motion
1 parent c57db04 commit 74c3cd8

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

‎src/hooks/useDeviceMotion/useDeviceMotion.demo.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { useDeviceMotion } from "./useDeviceMotion";
1+
import { useDeviceMotion } from './useDeviceMotion';
22

33
const Demo = () => {
4-
const deviceMotionData = useDeviceMotion(1000);
4+
const deviceMotionData = useDeviceMotion();
55

66
return (
7-
<pre lang="json">
8-
<b>DeviceMotionEvent data:</b>
7+
<pre lang='json'>
8+
<b>Device motion data:</b>
99
<p>{JSON.stringify(deviceMotionData, null, 2)}</p>
1010
</pre>
1111
);

‎src/hooks/useDeviceMotion/useDeviceMotion.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,64 +2,72 @@ import { useEffect, useRef, useState } from 'react';
22

33
import { throttle } from '@/utils/helpers';
44

5-
export interface DeviceMotionData {
5+
export interface UseDeviceMotionReturn {
66
interval: DeviceMotionEvent['interval'];
7-
rotationRate: Exclude<DeviceMotionEvent['rotationRate'], null>;
8-
acceleration: Exclude<DeviceMotionEvent['acceleration'], null>;
9-
accelerationIncludingGravity: Exclude<
10-
DeviceMotionEvent['accelerationIncludingGravity'],
11-
null
12-
>;
7+
acceleration: DeviceMotionEventAcceleration
8+
accelerationIncludingGravity: DeviceMotionEventAcceleration
9+
rotationRate: DeviceMotionEventRotationRate
10+
}
11+
12+
export interface UseDeviceMotionParams {
13+
delay?: number;
14+
callback?: (event: DeviceMotionEvent) => void;
15+
enabled?: boolean;
1316
}
1417

1518
/**
1619
* @name useDeviceMotion
17-
* @description Hook that provides DeviceMotionEvent data
20+
* @description Hook that work with device motion
1821
* @category Utilities
1922
*
20-
* @param {number} delay The data update delay
21-
* @param {Function} callback The event listener callback
22-
* @returns {DeviceMotionData} DeviceMotionEvent data
23+
* @param {number} [delay=1000] The delay in milliseconds
24+
* @param {(event: DeviceMotionEvent) => void} [callback] The callback function to be invoked
25+
* @param {boolean} [enabled=true] Whether to enable the hook
26+
* @returns {UseDeviceMotionReturn} The device motion data and interval
27+
*
28+
* @example
29+
* const { interval, rotationRate, acceleration, accelerationIncludingGravity } = useDeviceMotion();
2330
*/
24-
export const useDeviceMotion = (
25-
delay: number,
26-
callback?: (event: DeviceMotionEvent) => void
27-
) => {
28-
const [deviceMotionData, setDeviceMotionData] = useState<DeviceMotionData>({
31+
export const useDeviceMotion = (params?: UseDeviceMotionParams) => {
32+
const enabled = params?.enabled ?? true;
33+
const delay = params?.delay ?? 1000;
34+
const [deviceMotionData, setDeviceMotionData] = useState<UseDeviceMotionReturn>({
2935
interval: 0,
3036
rotationRate: { alpha: null, beta: null, gamma: null },
3137
acceleration: { x: null, y: null, z: null },
3238
accelerationIncludingGravity: { x: null, y: null, z: null }
3339
});
34-
const internalCallbackRef = useRef(callback);
35-
internalCallbackRef.current = callback;
40+
const internalCallbackRef = useRef(params?.callback);
41+
internalCallbackRef.current = params?.callback;
3642

3743
useEffect(() => {
44+
if (!enabled) return;
45+
3846
const onDeviceMotion = throttle<[DeviceMotionEvent]>((event) => {
3947
internalCallbackRef.current?.(event);
40-
setDeviceMotionData((prevState) => ({
48+
setDeviceMotionData({
4149
interval: event.interval,
4250
rotationRate: {
43-
...prevState.rotationRate,
51+
...deviceMotionData.rotationRate,
4452
...event.rotationRate
4553
},
4654
acceleration: {
47-
...prevState.acceleration,
55+
...deviceMotionData.acceleration,
4856
...event.acceleration
4957
},
5058
accelerationIncludingGravity: {
51-
...prevState.accelerationIncludingGravity,
59+
...deviceMotionData.accelerationIncludingGravity,
5260
...event.accelerationIncludingGravity
5361
}
54-
}));
62+
});
5563
}, delay);
5664

5765
window.addEventListener('devicemotion', onDeviceMotion);
5866

5967
return () => {
6068
window.removeEventListener('devicemotion', onDeviceMotion);
6169
};
62-
}, [delay]);
70+
}, [delay, enabled]);
6371

6472
return deviceMotionData;
6573
};

0 commit comments

Comments
 (0)