-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrag.ts
More file actions
64 lines (58 loc) · 1.74 KB
/
drag.ts
File metadata and controls
64 lines (58 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { type PointerEvent, useRef } from "react";
import { type Vector2, vector2 } from "../common/vector2";
const DRAG_THRESHOLD = 5;
export type UseDraggableProps = {
onClick?(e: PointerEvent): void;
onDragStart?(e: PointerEvent): void;
onDrag?(e: PointerEvent): void;
onDragEnd?(e: PointerEvent): void;
};
export function useDraggable(props: UseDraggableProps) {
const { onClick, onDragStart, onDrag, onDragEnd } = props;
const dragStateRef = useRef<{
pointerId: number;
initialPosition: Vector2;
isDragging: boolean;
} | null>(null);
return {
onPointerDown: (e: PointerEvent) => {
if (!(e.buttons & 1)) return;
dragStateRef.current = {
pointerId: e.pointerId,
initialPosition: vector2.fromDomEvent(e.nativeEvent),
isDragging: false,
};
onDragStart?.(e);
e.currentTarget.setPointerCapture(e.pointerId);
},
onPointerMove: (e: PointerEvent) => {
if (!dragStateRef.current) return;
if (dragStateRef.current.pointerId !== e.pointerId) return;
if (dragStateRef.current.isDragging) {
onDrag?.(e);
return;
}
const currentPosition = vector2.fromDomEvent(e.nativeEvent);
const distance = vector2.distance(
dragStateRef.current.initialPosition,
currentPosition,
);
if (distance > DRAG_THRESHOLD) {
dragStateRef.current.isDragging = true;
onDragStart?.(e);
onDrag?.(e);
}
},
onPointerUp: (e: PointerEvent) => {
if (!dragStateRef.current) return;
if (dragStateRef.current.pointerId !== e.pointerId) return;
e.currentTarget.releasePointerCapture(e.pointerId);
},
onLostPointerCapture: (e: PointerEvent) => {
if (!dragStateRef.current) return;
if (dragStateRef.current.isDragging) onDragEnd?.(e);
else onClick?.(e);
dragStateRef.current = null;
},
};
}