react-device-sensors 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +68 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/dist/useDeviceSensors.d.ts +60 -0
- package/dist/useDeviceSensors.d.ts.map +1 -0
- package/package.json +26 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
|
|
5
|
+
// Created Thu Jan 22 01:00:51 AM IST 2026
|
|
6
|
+
/**
|
|
7
|
+
* Acceleration axes follow the Device coordinate frame (Image https://developer.mozilla.org/en-US/docs/Web/API/Device_orientation_events/Orientation_and_motion_data_explained/axes.png)
|
|
8
|
+
* > The x axis is in the plane of the screen and is positive toward the right and negative toward the left.
|
|
9
|
+
* > The y axis is in the plane of the screen and is positive toward the top and negative toward the bottom.
|
|
10
|
+
* > The z axis is perpendicular to the screen or keyboard, and is positive extending outward from the screen.
|
|
11
|
+
* Acceleration measured in m/s^2
|
|
12
|
+
*
|
|
13
|
+
* References
|
|
14
|
+
* > https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent/
|
|
15
|
+
* > https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent
|
|
16
|
+
*
|
|
17
|
+
* Unavailable properties are set to null
|
|
18
|
+
**/
|
|
19
|
+
function useDeviceSensors() {
|
|
20
|
+
const [data, setData] = react.useState({
|
|
21
|
+
rotationRateAlpha: 0,
|
|
22
|
+
rotationRateBeta: 0,
|
|
23
|
+
rotationRateGamma: 0,
|
|
24
|
+
accelerationX: 0,
|
|
25
|
+
accelerationY: 0,
|
|
26
|
+
accelerationZ: 0,
|
|
27
|
+
orientationAbsolute: false,
|
|
28
|
+
orientationAlpha: 0,
|
|
29
|
+
orientationBeta: 0,
|
|
30
|
+
orientationGamma: 0,
|
|
31
|
+
});
|
|
32
|
+
react.useEffect(() => {
|
|
33
|
+
function handleDeviceOrientation(e) {
|
|
34
|
+
setData((d) => ({
|
|
35
|
+
...d,
|
|
36
|
+
orientationAbsolute: e.absolute,
|
|
37
|
+
orientationAlpha: e.alpha,
|
|
38
|
+
orientationBeta: e.beta,
|
|
39
|
+
orientationGamma: e.gamma,
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
function handleDeviceMotion(e) {
|
|
43
|
+
if (e.rotationRate === null || e.rotationRate === undefined) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
setData((d) => ({
|
|
47
|
+
...d,
|
|
48
|
+
rotationRateAlpha: e.rotationRate?.alpha ?? null,
|
|
49
|
+
rotationRateBeta: e.rotationRate?.beta ?? null,
|
|
50
|
+
rotationRateGamma: e.rotationRate?.gamma ?? null,
|
|
51
|
+
accelerationX: e.acceleration?.x ?? null,
|
|
52
|
+
accelerationY: e.acceleration?.y ?? null,
|
|
53
|
+
accelerationZ: e.acceleration?.z ?? null,
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
window.addEventListener("devicemotion", handleDeviceMotion);
|
|
57
|
+
window.addEventListener("deviceorientation", handleDeviceOrientation);
|
|
58
|
+
console.log("PS: Ignore the above warnings");
|
|
59
|
+
return () => {
|
|
60
|
+
window.removeEventListener("devicemotion", handleDeviceMotion);
|
|
61
|
+
window.removeEventListener("deviceorientation", handleDeviceOrientation);
|
|
62
|
+
};
|
|
63
|
+
}, []);
|
|
64
|
+
return data;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
module.exports = useDeviceSensors;
|
|
68
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/useDeviceSensors.ts"],"sourcesContent":[null],"names":["useState","useEffect"],"mappings":";;;;AAAA;AA2DA;;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Y,EAAE,CAAC,IAAI,IAAI;AACxC,gBAAA,aAAa,EAAE,CAAC,CAAC,YAAY,EAAE,CAAC,IAAI,IAAI;AACxC,gBAAA,aAAa,EAAE,CAAC,CAAC,YAAY,EAAE,CAAC,IAAI,IAAI;AAC3C,aAAA,CAAC,CAAC;QACP;AAEA,QAAA,MAAM,CAAC,gBAAgB,CAAC,cAAc,EAAE,kBAAkB,CAAC;AAC3D,QAAA,MAAM,CAAC,gBAAgB,CAAC,mBAAmB,EAAE,uBAAuB,CAAC;AACrE,QAAA,OAAO,CAAC,GAAG,CAAC,+BAA+B,CAAC;AAE5C,QAAA,OAAO,MAAK;AACR,YAAA,MAAM,CAAC,mBAAmB,CAAC,cAAc,EAAE,kBAAkB,CAAC;AAC9D,YAAA,MAAM,CAAC,mBAAmB,CACtB,mBAAmB,EACnB,uBAAuB,CAC1B;AACL,QAAA,CAAC;IACL,CAAC,EAAE,EAAE,CAAC;AAEN,IAAA,OAAO,IAAI;AACf;;;;"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAC;AAC7C,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
// Created Thu Jan 22 01:00:51 AM IST 2026
|
|
4
|
+
/**
|
|
5
|
+
* Acceleration axes follow the Device coordinate frame (Image https://developer.mozilla.org/en-US/docs/Web/API/Device_orientation_events/Orientation_and_motion_data_explained/axes.png)
|
|
6
|
+
* > The x axis is in the plane of the screen and is positive toward the right and negative toward the left.
|
|
7
|
+
* > The y axis is in the plane of the screen and is positive toward the top and negative toward the bottom.
|
|
8
|
+
* > The z axis is perpendicular to the screen or keyboard, and is positive extending outward from the screen.
|
|
9
|
+
* Acceleration measured in m/s^2
|
|
10
|
+
*
|
|
11
|
+
* References
|
|
12
|
+
* > https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent/
|
|
13
|
+
* > https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent
|
|
14
|
+
*
|
|
15
|
+
* Unavailable properties are set to null
|
|
16
|
+
**/
|
|
17
|
+
function useDeviceSensors() {
|
|
18
|
+
const [data, setData] = useState({
|
|
19
|
+
rotationRateAlpha: 0,
|
|
20
|
+
rotationRateBeta: 0,
|
|
21
|
+
rotationRateGamma: 0,
|
|
22
|
+
accelerationX: 0,
|
|
23
|
+
accelerationY: 0,
|
|
24
|
+
accelerationZ: 0,
|
|
25
|
+
orientationAbsolute: false,
|
|
26
|
+
orientationAlpha: 0,
|
|
27
|
+
orientationBeta: 0,
|
|
28
|
+
orientationGamma: 0,
|
|
29
|
+
});
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
function handleDeviceOrientation(e) {
|
|
32
|
+
setData((d) => ({
|
|
33
|
+
...d,
|
|
34
|
+
orientationAbsolute: e.absolute,
|
|
35
|
+
orientationAlpha: e.alpha,
|
|
36
|
+
orientationBeta: e.beta,
|
|
37
|
+
orientationGamma: e.gamma,
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
function handleDeviceMotion(e) {
|
|
41
|
+
if (e.rotationRate === null || e.rotationRate === undefined) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
setData((d) => ({
|
|
45
|
+
...d,
|
|
46
|
+
rotationRateAlpha: e.rotationRate?.alpha ?? null,
|
|
47
|
+
rotationRateBeta: e.rotationRate?.beta ?? null,
|
|
48
|
+
rotationRateGamma: e.rotationRate?.gamma ?? null,
|
|
49
|
+
accelerationX: e.acceleration?.x ?? null,
|
|
50
|
+
accelerationY: e.acceleration?.y ?? null,
|
|
51
|
+
accelerationZ: e.acceleration?.z ?? null,
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
window.addEventListener("devicemotion", handleDeviceMotion);
|
|
55
|
+
window.addEventListener("deviceorientation", handleDeviceOrientation);
|
|
56
|
+
console.log("PS: Ignore the above warnings");
|
|
57
|
+
return () => {
|
|
58
|
+
window.removeEventListener("devicemotion", handleDeviceMotion);
|
|
59
|
+
window.removeEventListener("deviceorientation", handleDeviceOrientation);
|
|
60
|
+
};
|
|
61
|
+
}, []);
|
|
62
|
+
return data;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { useDeviceSensors as default };
|
|
66
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/useDeviceSensors.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAAA;AA2DA;;;;;;;;;;;;AAYI;AACU,SAAU,gBAAgB,GAAA;AACpC,IAAA,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,QAAQ,CAAmB;AAC/C,QAAA,iBAAiB,EAAE,CAAC;AACpB,QAAA,gBAAgB,EAAE,CAAC;AACnB,QAAA,iBAAiB,EAAE,CAAC;AAEpB,QAAA,aAAa,EAAE,CAAC;AAChB,QAAA,aAAa,EAAE,CAAC;AAChB,QAAA,aAAa,EAAE,CAAC;AAEhB,QAAA,mBAAmB,EAAE,KAAK;AAC1B,QAAA,gBAAgB,EAAE,CAAC;AACnB,QAAA,eAAe,EAAE,CAAC;AAClB,QAAA,gBAAgB,EAAE,CAAC;AACtB,KAAA,CAAC;IACF,SAAS,CAAC,MAAK;QACX,SAAS,uBAAuB,CAAC,CAAyB,EAAA;AACtD,YAAA,OAAO,CAAC,CAAC,CAAC,MAAM;AACZ,gBAAA,GAAG,CAAC;gBACJ,mBAAmB,EAAE,CAAC,CAAC,QAAQ;gBAC/B,gBAAgB,EAAE,CAAC,CAAC,KAAK;gBACzB,eAAe,EAAE,CAAC,CAAC,IAAI;gBACvB,gBAAgB,EAAE,CAAC,CAAC,KAAK;AAC5B,aAAA,CAAC,CAAC;QACP;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
+
* The rate at which the device is rotating about its X axis; that is, front to back.
|
|
7
|
+
*/
|
|
8
|
+
rotationRateAlpha: number | null;
|
|
9
|
+
/**
|
|
10
|
+
* The rate at which the device is rotating about its Y axis; that is, side to side.
|
|
11
|
+
*/
|
|
12
|
+
rotationRateBeta: number | null;
|
|
13
|
+
/**
|
|
14
|
+
* The rate at which the device is rotating about its Z axis; that is, being twisted about a line perpendicular to the screen.
|
|
15
|
+
*/
|
|
16
|
+
rotationRateGamma: number | null;
|
|
17
|
+
/**
|
|
18
|
+
* Represents the acceleration along the x axis
|
|
19
|
+
*/
|
|
20
|
+
accelerationX: number | null;
|
|
21
|
+
/**
|
|
22
|
+
* Represents the acceleration along the y axis
|
|
23
|
+
*/
|
|
24
|
+
accelerationY: number | null;
|
|
25
|
+
/**
|
|
26
|
+
* Represents the acceleration along the z axis
|
|
27
|
+
*/
|
|
28
|
+
accelerationZ: number | null;
|
|
29
|
+
/**
|
|
30
|
+
* A boolean that indicates whether or not the device is providing orientation data absolutely.
|
|
31
|
+
*/
|
|
32
|
+
orientationAbsolute: boolean | null;
|
|
33
|
+
/**
|
|
34
|
+
* A number representing the motion of the device around the z axis, express in degrees with values ranging from 0 (inclusive) to 360 (exclusive).
|
|
35
|
+
*/
|
|
36
|
+
orientationAlpha: number | null;
|
|
37
|
+
/**
|
|
38
|
+
* A number representing the motion of the device around the x axis, express in degrees with values ranging from -180 (inclusive) to 180 (exclusive). This represents a front to back motion of the device.
|
|
39
|
+
*/
|
|
40
|
+
orientationBeta: number | null;
|
|
41
|
+
/**
|
|
42
|
+
* A number representing the motion of the device around the y axis, express in degrees with values ranging from -90 (inclusive) to 90 (exclusive). This represents a left to right motion of the device.
|
|
43
|
+
*/
|
|
44
|
+
orientationGamma: number | null;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Acceleration axes follow the Device coordinate frame (Image https://developer.mozilla.org/en-US/docs/Web/API/Device_orientation_events/Orientation_and_motion_data_explained/axes.png)
|
|
48
|
+
* > The x axis is in the plane of the screen and is positive toward the right and negative toward the left.
|
|
49
|
+
* > The y axis is in the plane of the screen and is positive toward the top and negative toward the bottom.
|
|
50
|
+
* > The z axis is perpendicular to the screen or keyboard, and is positive extending outward from the screen.
|
|
51
|
+
* Acceleration measured in m/s^2
|
|
52
|
+
*
|
|
53
|
+
* References
|
|
54
|
+
* > https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent/
|
|
55
|
+
* > https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent
|
|
56
|
+
*
|
|
57
|
+
* Unavailable properties are set to null
|
|
58
|
+
**/
|
|
59
|
+
export default function useDeviceSensors(): DeviceSensorData;
|
|
60
|
+
//# sourceMappingURL=useDeviceSensors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useDeviceSensors.d.ts","sourceRoot":"","sources":["../src/useDeviceSensors.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC3B;;OAEG;IACH,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;IAEjC;;OAEG;IACH,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IAEhC;;OAEG;IACH,iBAAiB,EAAE,MAAM,GAAG,IAAI,CAAC;IAEjC;;OAEG;IACH,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7B;;OAEG;IACH,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7B;;OAEG;IACH,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;IAE7B;;OAEG;IACH,mBAAmB,EAAE,OAAO,GAAG,IAAI,CAAC;IAEpC;;OAEG;IACH,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IAEhC;;OAEG;IACH,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAE/B;;OAEG;IACH,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;CACnC,CAAC;AAEF;;;;;;;;;;;;IAYI;AACJ,MAAM,CAAC,OAAO,UAAU,gBAAgB,qBAuDvC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"type": "module",
|
|
3
|
+
"name": "react-device-sensors",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"version": "0.0.1",
|
|
6
|
+
"description": "Simple react hook for accessing device's Sensor data",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"keywords": [],
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "GPL-3.0-only",
|
|
11
|
+
"peerDependencies": {
|
|
12
|
+
"react": "^19.2.3"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@rollup/plugin-typescript": "^12.3.0",
|
|
19
|
+
"@types/react": "^19.2.9",
|
|
20
|
+
"rollup": "^4.55.3",
|
|
21
|
+
"tslib": "^2.8.1"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "pnpx rollup -c"
|
|
25
|
+
}
|
|
26
|
+
}
|