Skip to content

Commit b83a8e1

Browse files
committed
feat: add support for touch & Device Orientation & Motion
1 parent 7a82e1d commit b83a8e1

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ export { useSaveData } from "./save-data";
44
export { useHardwareConcurrency } from "./hardware-concurrency";
55
export { useBatteryStatus } from "./battery";
66
export { useMediaCapabilities, useMediaDevices } from "./media";
7+
8+
export { useTouch } from "./touch";
9+
export { useDeviceOrientation, useDeviceMotion } from "./orientation";

orientation.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { useState, useEffect } from "vue-hooks";
2+
3+
const useDeviceOrientation = () => {
4+
let unsupported;
5+
if (typeof window !== "undefined" && "ondeviceorientation" in window)
6+
unsupported = false;
7+
else unsupported = true;
8+
9+
const initialDeviceOrientation = {
10+
unsupported,
11+
absolute: 0,
12+
alpha: 0,
13+
beta: 0,
14+
gamma: 0
15+
};
16+
const [deviceOrientation, setDeviceOrientation] = useState(
17+
initialDeviceOrientation
18+
);
19+
20+
useEffect(() => {
21+
if (!unsupported) {
22+
const updateOrientation = event => {
23+
setDeviceOrientation({
24+
absolute: event.absolute,
25+
alpha: event.alpha,
26+
beta: event.beta,
27+
gamma: event.gamma
28+
});
29+
};
30+
window.addEventListener(
31+
"deviceorientation",
32+
updateOrientation,
33+
true
34+
);
35+
36+
return () => {
37+
navigatorConnection.removeEventListener(
38+
"change",
39+
updateOrientation
40+
);
41+
};
42+
}
43+
}, []);
44+
45+
return {
46+
...deviceOrientation,
47+
setDeviceOrientation
48+
};
49+
};
50+
51+
const useDeviceMotion = () => {
52+
let unsupported;
53+
if (typeof window !== "undefined" && "ondevicemotion" in window)
54+
unsupported = false;
55+
else unsupported = true;
56+
57+
const initialDeviceMotion = {
58+
unsupported,
59+
acceleration: null,
60+
accelerationIncludingGravity: null,
61+
rotationRate: null,
62+
interval: null
63+
};
64+
const [deviceMotion, setDeviceMotion] = useState(initialDeviceMotion);
65+
66+
useEffect(() => {
67+
if (!unsupported) {
68+
const updateMotion = event => {
69+
setDeviceMotion({
70+
acceleration: event.acceleration,
71+
accelerationIncludingGravity:
72+
event.accelerationIncludingGravity,
73+
rotationRate: event.rotationRate,
74+
interval: event.interval
75+
});
76+
};
77+
window.addEventListener("devicemotion", updateMotion, true);
78+
79+
return () => {
80+
navigatorConnection.removeEventListener("change", updateMotion);
81+
};
82+
}
83+
}, []);
84+
85+
return {
86+
...deviceMotion,
87+
setDeviceMotion
88+
};
89+
};
90+
91+
export { useDeviceOrientation, useDeviceMotion };

touch.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
let unsupported;
2+
3+
// 检查浏览器是否支持 Navigator.deviceMemory
4+
if (
5+
!!(
6+
typeof window !== "undefined" &&
7+
("ontouchstart" in window ||
8+
(window.DocumentTouch &&
9+
typeof document !== "undefined" &&
10+
document instanceof window.DocumentTouch))
11+
) ||
12+
!!(
13+
typeof navigator !== "undefined" &&
14+
(navigator.maxTouchPoints || navigator.msMaxTouchPoints)
15+
)
16+
) {
17+
unsupported = false;
18+
} else {
19+
unsupported = true;
20+
}
21+
22+
const useTouch = () => {
23+
return !unsupported ? { touch: true } : { unsupported };
24+
};
25+
26+
export { useTouch };

0 commit comments

Comments
 (0)