-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnappable.test.tsx
43 lines (36 loc) · 1.01 KB
/
snappable.test.tsx
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
const THRESHOLD = 10;
const Knight = () => {
const { draggable } = useDraggable({
kind: "KNIGHT",
data: { color: "white" },
move: true,
shouldDrag({ dragStartEvent, event, data, top, left }) {
if (
Math.abs(event.x - dragStartEvent.x) < THRESHOLD &&
Math.abs(event.y - dragStartEvent.y) < THRESHOLD
) {
return false;
}
return true;
},
transformCoords({ dragStartEvent, event, data, dropTargets }) {
if (!dropTargets.length || dropTargets[0].data.type !== "SQUARE") {
return;
}
const dropTarget = dropTargets[0];
const { top, left, width, height } =
dropTarget.element.getBoundingClientRect();
const offsetLeft = event.x - left;
const offsetTop = event.y - top;
if (
offsetLeft >= THRESHOLD &&
offsetLeft <= width - THRESHOLD &&
offsetTop >= THRESHOLD &&
offsetTop <= height - THRESHOLD
) {
return { top, left };
}
return;
},
});
};