-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathuseDraggable.ts
147 lines (135 loc) · 4.64 KB
/
useDraggable.ts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* eslint-disable @typescript-eslint/no-dynamic-delete */
import { useLayoutEffect, useMemo, useRef } from "react";
import { LayoutRectangle, View, ViewProps } from "react-native";
import { runOnUI, useAnimatedReaction, useSharedValue } from "react-native-reanimated";
import { DraggableState, useDndContext } from "../DndContext";
import { useEvent, useLatestSharedValue } from "../hooks";
import { Data, UniqueIdentifier } from "../types";
import { isReanimatedSharedValue } from "../utils";
import { useSharedPoint } from "./useSharedPoint";
export type DraggableConstraints = {
activationDelay: number;
activationTolerance: number;
};
export type UseDraggableOptions = Partial<DraggableConstraints> & {
id: UniqueIdentifier;
data?: Data;
disabled?: boolean;
};
/**
* useDraggable is a custom hook that provides functionality for making a component draggable within a drag and drop context.
*
* @function
* @example
* const { offset, setNodeRef, activeId, setNodeLayout, draggableState } = useDraggable({ id: 'draggable-1' });
*
* @param {object} options - The options that define the behavior of the draggable component.
* @param {string} options.id - A unique identifier for the draggable component.
* @param {object} [options.data={}] - Optional data associated with the draggable component.
* @param {boolean} [options.disabled=false] - A flag that indicates whether the draggable component is disabled.
* @param {number} [options.activationDelay=0] - A number representing the duration, in milliseconds, that this draggable item needs to be held for before allowing a drag to start.
* @param {number} [options.activationTolerance=Infinity] - A number representing the distance, in points, of motion that is tolerated before the drag operation is aborted.
*/
export const useDraggable = ({
id,
data = {},
disabled = false,
activationDelay = 0,
activationTolerance = Infinity,
}: UseDraggableOptions) => {
const {
draggableLayouts,
draggableOffsets,
draggableRestingOffsets,
draggableOptions,
draggableStates,
draggableActiveId,
draggablePendingId,
containerRef,
panGestureState,
} = useDndContext();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const ref = useRef<View>(null!);
// eslint-disable-next-line react-hooks/rules-of-hooks
const sharedData = isReanimatedSharedValue(data) ? data : useLatestSharedValue(data);
const layout = useSharedValue<LayoutRectangle>({
x: 0,
y: 0,
width: 0,
height: 0,
});
const offset = useSharedPoint(0, 0);
const restingOffset = useSharedPoint(0, 0);
const state = useSharedValue<DraggableState>("resting");
// Register early to allow proper referencing in useDraggableStyle
draggableStates.value[id] = state;
useLayoutEffect(() => {
const runLayoutEffect = () => {
"worklet";
requestAnimationFrame(() => {
draggableLayouts.value[id] = layout;
draggableOffsets.value[id] = offset;
draggableRestingOffsets.value[id] = restingOffset;
draggableStates.value[id] = state;
draggableOptions.value[id] = {
id,
data: sharedData,
disabled,
activationDelay,
activationTolerance,
};
});
};
runOnUI(runLayoutEffect)();
return () => {
const cleanupLayoutEffect = () => {
"worklet";
requestAnimationFrame(() => {
delete draggableLayouts.value[id];
delete draggableOffsets.value[id];
delete draggableRestingOffsets.value[id];
delete draggableOptions.value[id];
delete draggableStates.value[id];
});
};
runOnUI(cleanupLayoutEffect)();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [id]);
const onLayout: ViewProps["onLayout"] = useEvent((_event) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!ref.current || !containerRef.current) {
return;
}
ref.current.measureLayout(containerRef.current, (x, y, width, height) => {
layout.value = { x, y, width, height };
});
});
// Track disabled prop changes
useAnimatedReaction(
() => disabled,
(next, prev) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (next !== prev && draggableOptions.value[id]) {
draggableOptions.value[id].disabled = disabled;
}
},
[disabled],
);
const props = useMemo(
() => ({
ref,
onLayout,
}),
[onLayout],
);
return {
offset,
state,
activeId: draggableActiveId,
pendingId: draggablePendingId,
setNodeLayout: onLayout,
panGestureState,
props,
};
};