@swmansion/react-native-bottom-sheet 0.3.1 → 0.4.0

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.
@@ -0,0 +1,208 @@
1
+ import type { PanGesture } from 'react-native-gesture-handler';
2
+ import { Gesture } from 'react-native-gesture-handler';
3
+ import { scheduleOnRN } from 'react-native-worklets';
4
+ import {
5
+ measure,
6
+ scrollTo,
7
+ type AnimatedRef,
8
+ type SharedValue,
9
+ useSharedValue,
10
+ } from 'react-native-reanimated';
11
+
12
+ import { findSnapTarget } from './bottomSheetUtils';
13
+
14
+ interface BottomSheetPanGestureParams {
15
+ animationTarget: SharedValue<number>;
16
+ translateY: SharedValue<number>;
17
+ sheetHeight: SharedValue<number>;
18
+ detentsValue: SharedValue<number[]>;
19
+ isDraggableValue: SharedValue<boolean[]>;
20
+ currentIndex: SharedValue<number>;
21
+ scrollOffset: SharedValue<number>;
22
+ hasScrollable: SharedValue<boolean>;
23
+ isScrollableGestureActive: SharedValue<boolean>;
24
+ isScrollableLocked: SharedValue<boolean>;
25
+ scrollableRef: AnimatedRef<any>;
26
+ handleIndexChange: (nextIndex: number) => void;
27
+ animateToIndex: (targetIndex: number, velocity?: number) => void;
28
+ }
29
+
30
+ export const useBottomSheetPanGesture = ({
31
+ animationTarget,
32
+ translateY,
33
+ sheetHeight,
34
+ detentsValue,
35
+ isDraggableValue,
36
+ currentIndex,
37
+ scrollOffset,
38
+ hasScrollable,
39
+ isScrollableGestureActive,
40
+ isScrollableLocked,
41
+ scrollableRef,
42
+ handleIndexChange,
43
+ animateToIndex,
44
+ }: BottomSheetPanGestureParams): PanGesture => {
45
+ const isDraggingSheet = useSharedValue(false);
46
+ const isDraggingFromScrollable = useSharedValue(false);
47
+ const panStartY = useSharedValue(0);
48
+ const panActivated = useSharedValue(false);
49
+ const dragStartTranslateY = useSharedValue(0);
50
+ const isTouchWithinScrollable = useSharedValue(false);
51
+
52
+ return Gesture.Pan()
53
+ .manualActivation(true)
54
+ .onTouchesDown((event) => {
55
+ 'worklet';
56
+ panActivated.set(false);
57
+ isDraggingSheet.set(false);
58
+ isDraggingFromScrollable.set(false);
59
+ isScrollableLocked.set(false);
60
+ isTouchWithinScrollable.set(false);
61
+ const touch = event.changedTouches[0] ?? event.allTouches[0];
62
+ if (touch !== undefined) {
63
+ panStartY.set(touch.absoluteY);
64
+ if (hasScrollable.value) {
65
+ const layout = measure(scrollableRef);
66
+ if (layout !== null) {
67
+ const withinX =
68
+ touch.absoluteX >= layout.pageX &&
69
+ touch.absoluteX <= layout.pageX + layout.width;
70
+ const withinY =
71
+ touch.absoluteY >= layout.pageY &&
72
+ touch.absoluteY <= layout.pageY + layout.height;
73
+ isTouchWithinScrollable.set(withinX && withinY);
74
+ }
75
+ }
76
+ }
77
+ })
78
+ .onTouchesMove((event, stateManager) => {
79
+ 'worklet';
80
+ if (panActivated.value) return;
81
+ const touch = event.changedTouches[0] ?? event.allTouches[0];
82
+ if (!touch) return;
83
+ const deltaY = touch.absoluteY - panStartY.value;
84
+ if (
85
+ hasScrollable.value &&
86
+ scrollOffset.value > 0 &&
87
+ isTouchWithinScrollable.value
88
+ ) {
89
+ return;
90
+ }
91
+ if (deltaY > 0 || translateY.value > 0) {
92
+ panActivated.set(true);
93
+ stateManager.activate();
94
+ }
95
+ })
96
+ .onBegin(() => {
97
+ 'worklet';
98
+ animationTarget.set(NaN);
99
+ isDraggingSheet.set(false);
100
+ isDraggingFromScrollable.set(false);
101
+ dragStartTranslateY.set(translateY.value);
102
+ })
103
+ .onUpdate((event) => {
104
+ 'worklet';
105
+ if (isDraggingSheet.value) {
106
+ if (isDraggingFromScrollable.value) {
107
+ scrollTo(scrollableRef, 0, 0, false);
108
+ }
109
+ } else {
110
+ const isDraggingDown = event.translationY > 0;
111
+ const canStartDrag =
112
+ !hasScrollable.value ||
113
+ scrollOffset.value <= 0 ||
114
+ !isTouchWithinScrollable.value;
115
+ if (!canStartDrag || (!isDraggingDown && translateY.value <= 0)) {
116
+ return;
117
+ }
118
+ const isScrollableActive =
119
+ hasScrollable.value && isScrollableGestureActive.value;
120
+ isDraggingSheet.set(true);
121
+ isDraggingFromScrollable.set(
122
+ isScrollableActive && isTouchWithinScrollable.value
123
+ );
124
+ dragStartTranslateY.set(translateY.value - event.translationY);
125
+ isScrollableLocked.set(hasScrollable.value);
126
+ if (isTouchWithinScrollable.value && hasScrollable.value) {
127
+ scrollTo(scrollableRef, 0, 0, false);
128
+ }
129
+ }
130
+ const rawTranslate = dragStartTranslateY.value + event.translationY;
131
+ const nextTranslate = Math.min(
132
+ Math.max(rawTranslate, 0),
133
+ sheetHeight.value
134
+ );
135
+ translateY.set(nextTranslate);
136
+ if (
137
+ isDraggingSheet.value &&
138
+ rawTranslate < 0 &&
139
+ isTouchWithinScrollable.value &&
140
+ hasScrollable.value
141
+ ) {
142
+ isDraggingSheet.set(false);
143
+ isScrollableLocked.set(false);
144
+ const resolvedDetentValues = detentsValue.value;
145
+ const draggable = isDraggableValue.value;
146
+ let targetSnapIndex = -1;
147
+ let targetSnapValue = -1;
148
+ for (let i = resolvedDetentValues.length - 1; i >= 0; i--) {
149
+ const detentValue = resolvedDetentValues[i];
150
+ if (
151
+ detentValue !== undefined &&
152
+ (draggable[i] ?? true) &&
153
+ detentValue > targetSnapValue
154
+ ) {
155
+ targetSnapValue = detentValue;
156
+ targetSnapIndex = i;
157
+ }
158
+ }
159
+ if (targetSnapIndex === -1) {
160
+ const maxSnap = sheetHeight.value;
161
+ for (let i = resolvedDetentValues.length - 1; i >= 0; i--) {
162
+ if (resolvedDetentValues[i] === maxSnap) {
163
+ targetSnapIndex = i;
164
+ break;
165
+ }
166
+ }
167
+ }
168
+ if (targetSnapIndex !== -1) {
169
+ if (targetSnapIndex !== currentIndex.value) {
170
+ scheduleOnRN(handleIndexChange, targetSnapIndex);
171
+ }
172
+ animateToIndex(targetSnapIndex);
173
+ }
174
+ }
175
+ })
176
+ .onEnd((event) => {
177
+ 'worklet';
178
+ const wasDragging = isDraggingSheet.value;
179
+ isScrollableLocked.set(false);
180
+ isDraggingSheet.set(false);
181
+ animationTarget.set(NaN);
182
+ if (!wasDragging) {
183
+ animateToIndex(currentIndex.value);
184
+ return;
185
+ }
186
+ const maxSnap = sheetHeight.value;
187
+ const draggable = isDraggableValue.value;
188
+ const allPositions = detentsValue.value.map((detentValue, snapIndex) => ({
189
+ index: snapIndex,
190
+ translateY: maxSnap - detentValue,
191
+ isDraggable: draggable[snapIndex] ?? true,
192
+ }));
193
+ const targetIndex = findSnapTarget(
194
+ translateY.value,
195
+ event.velocityY,
196
+ currentIndex.value,
197
+ allPositions
198
+ );
199
+ const hasIndexChanged = targetIndex !== currentIndex.value;
200
+ if (hasIndexChanged) scheduleOnRN(handleIndexChange, targetIndex);
201
+ const shouldApplyVelocity =
202
+ hasIndexChanged && Number.isFinite(event.velocityY);
203
+ animateToIndex(
204
+ targetIndex,
205
+ shouldApplyVelocity ? event.velocityY : undefined
206
+ );
207
+ });
208
+ };