forked from gorhom/react-native-bottom-sheet
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathuseCustomGestureEventsHandlers.ts
358 lines (330 loc) · 11 KB
/
useCustomGestureEventsHandlers.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { Keyboard, Platform, useWindowDimensions } from 'react-native';
import { runOnJS, useWorkletCallback } from 'react-native-reanimated';
import { clamp, snapPoint } from 'react-native-redash';
import {
useBottomSheetInternal,
GESTURE_SOURCE,
KEYBOARD_STATE,
SCROLLABLE_TYPE,
GestureEventHandlerCallbackType,
ANIMATION_SOURCE,
} from '@gorhom/bottom-sheet';
import { useGestureTranslationY } from './GestureTranslationContext';
const dismissKeyboardOnJs = runOnJS(Keyboard.dismiss);
export const useCustomGestureEventsHandlers = () => {
// hooks
const gestureTranslationY = useGestureTranslationY();
const {
animatedPosition,
animatedSnapPoints,
animatedKeyboardState,
animatedKeyboardHeight,
animatedContainerHeight,
animatedScrollableType,
animatedHighestSnapPoint,
animatedClosedPosition,
animatedScrollableContentOffsetY,
enableOverDrag,
enablePanDownToClose,
overDragResistanceFactor,
isInTemporaryPosition,
isScrollableRefreshable,
animateToPosition,
stopAnimation,
} = useBottomSheetInternal();
//#region gesture methods
const handleOnStart: GestureEventHandlerCallbackType = useWorkletCallback(
function handleOnStart(_, { translationY }, context) {
// cancel current animation
stopAnimation();
// store current animated position
context.initialPosition = animatedPosition.value;
context.initialKeyboardState = animatedKeyboardState.value;
/**
* if the scrollable content is scrolled, then
* we lock the position.
*/
if (animatedScrollableContentOffsetY.value > 0) {
context.isScrollablePositionLocked = true;
}
gestureTranslationY.value = translationY;
},
[animatedPosition, animatedKeyboardState, animatedScrollableContentOffsetY]
);
const handleOnActive: GestureEventHandlerCallbackType = useWorkletCallback(
function handleOnActive(source, { translationY }, context) {
gestureTranslationY.value = translationY;
let highestSnapPoint =
animatedSnapPoints.value[animatedSnapPoints.value.length - 1];
/**
* if keyboard is shown, then we set the highest point to the current
* position which includes the keyboard height.
*/
if (
isInTemporaryPosition.value &&
context.initialKeyboardState === KEYBOARD_STATE.SHOWN
) {
highestSnapPoint = context.initialPosition;
}
/**
* if current position is out of provided `snapPoints` and smaller then
* highest snap pont, then we set the highest point to the current position.
*/
if (
isInTemporaryPosition.value &&
context.initialPosition < highestSnapPoint
) {
highestSnapPoint = context.initialPosition;
}
const lowestSnapPoint = enablePanDownToClose
? animatedContainerHeight.value
: animatedSnapPoints.value[0];
/**
* if scrollable is refreshable and sheet position at the highest
* point, then do not interact with current gesture.
*/
if (
source === GESTURE_SOURCE.SCROLLABLE &&
isScrollableRefreshable.value &&
animatedPosition.value === highestSnapPoint
) {
return;
}
/**
* a negative scrollable content offset to be subtracted from accumulated
* current position and gesture translation Y to allow user to drag the sheet,
* when scrollable position at the top.
* a negative scrollable content offset when the scrollable is not locked.
*/
const negativeScrollableContentOffset =
(context.initialPosition === highestSnapPoint &&
source === GESTURE_SOURCE.SCROLLABLE) ||
!context.isScrollablePositionLocked
? animatedScrollableContentOffsetY.value * -1
: 0;
/**
* an accumulated value of starting position with gesture translation y.
*/
const draggedPosition = context.initialPosition + translationY;
/**
* an accumulated value of dragged position and negative scrollable content offset,
* this will insure locking sheet position when user is scrolling the scrollable until,
* they reach to the top of the scrollable.
*/
const accumulatedDraggedPosition =
draggedPosition + negativeScrollableContentOffset;
/**
* a clamped value of the accumulated dragged position, to insure keeping the dragged
* position between the highest and middle snap points.
*/
const secondHighestSnapPoint =
animatedSnapPoints.value[animatedSnapPoints.value.length - 2];
const isDraggingFromBottom =
context.initialPosition > secondHighestSnapPoint;
const clampedPosition = (() => {
if (source === GESTURE_SOURCE.SCROLLABLE) {
const clampSource = (() => {
if (isDraggingFromBottom) {
return accumulatedDraggedPosition;
}
return Math.min(draggedPosition, secondHighestSnapPoint);
})();
return clamp(clampSource, highestSnapPoint, lowestSnapPoint);
} else {
return clamp(
accumulatedDraggedPosition,
highestSnapPoint,
lowestSnapPoint
);
}
})();
/**
* if scrollable position is locked and the animated position
* reaches the highest point, then we unlock the scrollable position.
*/
if (
context.isScrollablePositionLocked &&
source === GESTURE_SOURCE.SCROLLABLE &&
animatedPosition.value === highestSnapPoint
) {
context.isScrollablePositionLocked = false;
}
/**
* over-drag implementation.
*/
if (enableOverDrag) {
if (
(source === GESTURE_SOURCE.HANDLE ||
animatedScrollableType.value === SCROLLABLE_TYPE.VIEW) &&
draggedPosition < highestSnapPoint
) {
const resistedPosition =
highestSnapPoint -
Math.sqrt(1 + (highestSnapPoint - draggedPosition)) *
overDragResistanceFactor;
animatedPosition.value = resistedPosition;
return;
}
if (
source === GESTURE_SOURCE.HANDLE &&
draggedPosition > lowestSnapPoint
) {
const resistedPosition =
lowestSnapPoint +
Math.sqrt(1 + (draggedPosition - lowestSnapPoint)) *
overDragResistanceFactor;
animatedPosition.value = resistedPosition;
return;
}
}
animatedPosition.value = clampedPosition;
},
[
enableOverDrag,
enablePanDownToClose,
overDragResistanceFactor,
animatedContainerHeight,
animatedKeyboardState,
animatedPosition,
animatedSnapPoints,
isInTemporaryPosition,
isScrollableRefreshable,
animatedScrollableContentOffsetY,
]
);
const windowDimensions = useWindowDimensions();
const windowHeight = windowDimensions.height;
const handleOnEnd: GestureEventHandlerCallbackType = useWorkletCallback(
function handleOnEnd(
source,
{ translationY, absoluteY, velocityY },
context
) {
const highestSnapPoint = animatedHighestSnapPoint.value;
const isSheetAtHighestSnapPoint =
animatedPosition.value === highestSnapPoint;
/**
* if the sheet is in a temporary position and the gesture ended above
* the current position, then we snap back to the temporary position.
*/
if (
isInTemporaryPosition.value &&
context.initialPosition >= animatedPosition.value
) {
if (context.initialPosition > animatedPosition.value) {
animateToPosition(
context.initialPosition,
ANIMATION_SOURCE.GESTURE,
velocityY / 2
);
}
return;
}
/**
* close keyboard if current position is below the recorded
* start position and keyboard still shown.
*/
const isScrollable =
animatedScrollableType.value !== SCROLLABLE_TYPE.UNDETERMINED &&
animatedScrollableType.value !== SCROLLABLE_TYPE.VIEW;
/**
* if keyboard is shown and the sheet is dragged down,
* then we dismiss the keyboard.
*/
if (
context.initialKeyboardState === KEYBOARD_STATE.SHOWN &&
animatedPosition.value > context.initialPosition
) {
/**
* if the platform is ios, current content is scrollable and
* the end touch point is below the keyboard position then
* we exit the method.
*
* because the the keyboard dismiss is interactive in iOS.
*/
if (
!(
Platform.OS === 'ios' &&
isScrollable &&
absoluteY > windowHeight - animatedKeyboardHeight.value
)
) {
dismissKeyboardOnJs();
}
}
/**
* reset isInTemporaryPosition value
*/
if (isInTemporaryPosition.value) {
isInTemporaryPosition.value = false;
}
/**
* clone snap points array, and insert the container height
* if pan down to close is enabled.
*/
const snapPoints = animatedSnapPoints.value.slice();
if (enablePanDownToClose) {
snapPoints.unshift(animatedClosedPosition.value);
}
/**
* calculate the destination point, using redash.
*/
const isDraggingDown = translationY > 0;
const destinationPoint = (() => {
const endingSnapPoint = snapPoint(
translationY + context.initialPosition,
velocityY,
snapPoints
);
if (source === GESTURE_SOURCE.HANDLE) {
return endingSnapPoint;
}
const secondHighestSnapPoint =
animatedSnapPoints.value[animatedSnapPoints.value.length - 2];
return isDraggingDown
? Math.min(secondHighestSnapPoint, endingSnapPoint)
: endingSnapPoint;
})();
/**
* if destination point is the same as the current position,
* then no need to perform animation.
*/
if (destinationPoint === animatedPosition.value) {
return;
}
const wasGestureHandledByScrollView =
source === GESTURE_SOURCE.SCROLLABLE &&
animatedScrollableContentOffsetY.value > 0;
/**
* prevents snapping from top to middle / bottom with repeated interrupted scrolls
*/
if (wasGestureHandledByScrollView && isSheetAtHighestSnapPoint) {
return;
}
animateToPosition(
destinationPoint,
ANIMATION_SOURCE.GESTURE,
velocityY / 2
);
},
[
enablePanDownToClose,
animateToPosition,
animatedClosedPosition,
animatedHighestSnapPoint,
animatedKeyboardHeight,
animatedPosition,
animatedScrollableType,
animatedSnapPoints,
animatedScrollableContentOffsetY,
isInTemporaryPosition,
isScrollableRefreshable,
windowHeight,
]
);
return {
handleOnStart,
handleOnActive,
handleOnEnd,
};
};