@fibery/react 1.4.5 → 1.4.7
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 -12
- package/src/use-drag.ts +96 -0
package/package.json
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fibery/react",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.7",
|
|
4
4
|
"private": false,
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"files": [
|
|
7
7
|
"src"
|
|
8
8
|
],
|
|
9
9
|
"peerDependencies": {
|
|
10
|
-
"react": "18.
|
|
10
|
+
"react": "18.3.1"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@tanstack/react-query": "5.62.7",
|
|
14
|
-
"@fibery/helpers": "1.
|
|
14
|
+
"@fibery/helpers": "1.4.0"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@testing-library/react": "
|
|
18
|
-
"@testing-library/react-hooks": "
|
|
17
|
+
"@testing-library/react": "16.3.1",
|
|
18
|
+
"@testing-library/react-hooks": "8.0.1",
|
|
19
19
|
"@types/jest": "30.0.0",
|
|
20
|
-
"@types/react": "18.
|
|
21
|
-
"@types/react-dom": "18.
|
|
20
|
+
"@types/react": "18.3.12",
|
|
21
|
+
"@types/react-dom": "18.3.1",
|
|
22
22
|
"jest": "30.2.0",
|
|
23
23
|
"jest-junit": "16.0.0",
|
|
24
|
-
"react": "18.
|
|
25
|
-
"react-dom": "18.
|
|
26
|
-
"react-test-renderer": "18.
|
|
24
|
+
"react": "18.3.1",
|
|
25
|
+
"react-dom": "18.3.1",
|
|
26
|
+
"react-test-renderer": "18.3.1",
|
|
27
27
|
"typescript": "5.9.3",
|
|
28
|
-
"@fibery/
|
|
29
|
-
"@fibery/
|
|
28
|
+
"@fibery/babel-preset": "7.4.1",
|
|
29
|
+
"@fibery/eslint-config": "8.6.2"
|
|
30
30
|
},
|
|
31
31
|
"jest": {
|
|
32
32
|
"coveragePathIgnorePatterns": [
|
package/src/use-drag.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import {useRef} from "react";
|
|
2
|
+
|
|
3
|
+
type Point = {
|
|
4
|
+
x: number;
|
|
5
|
+
y: number;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* very simple hook for simple isolated drag-like operations.
|
|
10
|
+
* e.g. dragging border to resize, when you dont need drop targets, auto-scroll, etc.
|
|
11
|
+
*
|
|
12
|
+
* For complex operations you most likely want to use atlaskit/pragmatic-drag-and-drop
|
|
13
|
+
*/
|
|
14
|
+
export const useDrag = ({
|
|
15
|
+
threshold = 0,
|
|
16
|
+
onDragStart,
|
|
17
|
+
onDrag,
|
|
18
|
+
onDragEnd,
|
|
19
|
+
}: {
|
|
20
|
+
/** Minimum movement before drag starts */
|
|
21
|
+
threshold?: number;
|
|
22
|
+
onDragStart?: () => void;
|
|
23
|
+
onDrag?: (translation: Point) => void;
|
|
24
|
+
onDragEnd?: (translation: Point) => void;
|
|
25
|
+
}) => {
|
|
26
|
+
const ref = useRef<HTMLDivElement>(null);
|
|
27
|
+
const state = useRef<"idle" | "press" | "drag" | "drag-end">("idle");
|
|
28
|
+
|
|
29
|
+
const lastPosition = useRef<Point>({x: 0, y: 0});
|
|
30
|
+
const translation = useRef<Point>({x: 0, y: 0});
|
|
31
|
+
|
|
32
|
+
function onClick(e: MouseEvent) {
|
|
33
|
+
if (state.current === "drag-end") {
|
|
34
|
+
e.preventDefault();
|
|
35
|
+
e.stopPropagation();
|
|
36
|
+
state.current = "idle";
|
|
37
|
+
ref.current?.removeEventListener("click", onClick);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function onPointerDown(e: React.PointerEvent) {
|
|
42
|
+
lastPosition.current = {x: e.clientX, y: e.clientY};
|
|
43
|
+
translation.current = {x: 0, y: 0};
|
|
44
|
+
state.current = "press";
|
|
45
|
+
ref.current?.setPointerCapture(e.pointerId);
|
|
46
|
+
ref.current?.addEventListener("click", onClick);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function onPointerMove(e: React.PointerEvent) {
|
|
50
|
+
if (state.current === "press") {
|
|
51
|
+
const dx = e.clientX - lastPosition.current.x;
|
|
52
|
+
const dy = e.clientY - lastPosition.current.y;
|
|
53
|
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
54
|
+
|
|
55
|
+
if (distance >= threshold) {
|
|
56
|
+
state.current = "drag";
|
|
57
|
+
onDragStart?.();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (state.current !== "drag") {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const currentPosition = {x: e.clientX, y: e.clientY};
|
|
66
|
+
|
|
67
|
+
const dx = currentPosition.x - lastPosition.current.x;
|
|
68
|
+
const dy = currentPosition.y - lastPosition.current.y;
|
|
69
|
+
lastPosition.current = currentPosition;
|
|
70
|
+
|
|
71
|
+
translation.current = {
|
|
72
|
+
x: translation.current.x + dx,
|
|
73
|
+
y: translation.current.y + dy,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
onDrag?.(translation.current);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function onPointerUp(e: React.PointerEvent) {
|
|
80
|
+
if (state.current === "drag") {
|
|
81
|
+
state.current = "drag-end";
|
|
82
|
+
} else {
|
|
83
|
+
state.current = "idle";
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
onDragEnd?.(translation.current);
|
|
87
|
+
ref.current?.releasePointerCapture(e.pointerId);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return {
|
|
91
|
+
ref,
|
|
92
|
+
onPointerDown,
|
|
93
|
+
onPointerMove,
|
|
94
|
+
onPointerUp,
|
|
95
|
+
};
|
|
96
|
+
};
|