@os-design/use-long-press 1.0.9 → 1.0.10
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/package.json +12 -4
- package/src/index.ts +59 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@os-design/use-long-press",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.10",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"repository": "git@gitlab.com:os-team/libs/os-design.git",
|
|
6
6
|
"main": "dist/cjs/index.js",
|
|
@@ -14,7 +14,15 @@
|
|
|
14
14
|
"./package.json": "./package.json"
|
|
15
15
|
},
|
|
16
16
|
"files": [
|
|
17
|
-
"dist"
|
|
17
|
+
"dist",
|
|
18
|
+
"src",
|
|
19
|
+
"!**/*.test.ts",
|
|
20
|
+
"!**/*.test.tsx",
|
|
21
|
+
"!**/__tests__",
|
|
22
|
+
"!**/*.stories.tsx",
|
|
23
|
+
"!**/*.stories.mdx",
|
|
24
|
+
"!**/*.example.tsx",
|
|
25
|
+
"!**/*.emotion.d.ts"
|
|
18
26
|
],
|
|
19
27
|
"sideEffects": false,
|
|
20
28
|
"scripts": {
|
|
@@ -29,10 +37,10 @@
|
|
|
29
37
|
"access": "public"
|
|
30
38
|
},
|
|
31
39
|
"dependencies": {
|
|
32
|
-
"@os-design/use-prevent-default-event": "^1.0.
|
|
40
|
+
"@os-design/use-prevent-default-event": "^1.0.10"
|
|
33
41
|
},
|
|
34
42
|
"peerDependencies": {
|
|
35
43
|
"react": ">=18"
|
|
36
44
|
},
|
|
37
|
-
"gitHead": "
|
|
45
|
+
"gitHead": "3d6b264027712ef81a75379fe3fde3c76c3079af"
|
|
38
46
|
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import usePreventDefaultEvent from '@os-design/use-prevent-default-event';
|
|
2
|
+
import {
|
|
3
|
+
TouchEventHandler,
|
|
4
|
+
useCallback,
|
|
5
|
+
useEffect,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
} from 'react';
|
|
9
|
+
|
|
10
|
+
export interface LongPressHandlers {
|
|
11
|
+
onTouchStart: TouchEventHandler;
|
|
12
|
+
onTouchMove: () => void;
|
|
13
|
+
onTouchEnd: () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const useLongPress = (
|
|
17
|
+
callback: TouchEventHandler,
|
|
18
|
+
ms = 500
|
|
19
|
+
): LongPressHandlers => {
|
|
20
|
+
const callbackRef = useRef(callback);
|
|
21
|
+
const msRef = useRef(ms);
|
|
22
|
+
const timeout = useRef<NodeJS.Timeout>();
|
|
23
|
+
const [pressed, setPressed] = useState(false);
|
|
24
|
+
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
callbackRef.current = callback;
|
|
27
|
+
}, [callback]);
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
msRef.current = ms;
|
|
31
|
+
}, [ms]);
|
|
32
|
+
|
|
33
|
+
const stopTimeout = useCallback(() => {
|
|
34
|
+
if (!timeout.current) return;
|
|
35
|
+
clearTimeout(timeout.current);
|
|
36
|
+
}, []);
|
|
37
|
+
|
|
38
|
+
// Stop the timeout if the component was unmounted
|
|
39
|
+
useEffect(() => () => stopTimeout(), [stopTimeout]);
|
|
40
|
+
|
|
41
|
+
// Disable the context menu
|
|
42
|
+
usePreventDefaultEvent(document, 'contextmenu', pressed);
|
|
43
|
+
|
|
44
|
+
const onTouchStart = useCallback<TouchEventHandler>((e) => {
|
|
45
|
+
setPressed(true);
|
|
46
|
+
timeout.current = setTimeout(() => callbackRef.current(e), msRef.current);
|
|
47
|
+
}, []);
|
|
48
|
+
|
|
49
|
+
const onTouchMove = useCallback(() => stopTimeout(), [stopTimeout]);
|
|
50
|
+
|
|
51
|
+
const onTouchEnd = useCallback(() => {
|
|
52
|
+
setPressed(false);
|
|
53
|
+
stopTimeout();
|
|
54
|
+
}, [stopTimeout]);
|
|
55
|
+
|
|
56
|
+
return { onTouchStart, onTouchMove, onTouchEnd };
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export default useLongPress;
|