-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
PanResponderAdapter.tsx
339 lines (290 loc) · 8.61 KB
/
PanResponderAdapter.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
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
import * as React from 'react';
import {
Animated,
type GestureResponderEvent,
Keyboard,
PanResponder,
type PanResponderGestureState,
StyleSheet,
View,
} from 'react-native';
import useLatestCallback from 'use-latest-callback';
import type {
EventEmitterProps,
Layout,
Listener,
NavigationState,
PagerProps,
Route,
} from './types';
import { useAnimatedValue } from './useAnimatedValue';
type Props<T extends Route> = PagerProps & {
layout: Layout;
onIndexChange: (index: number) => void;
navigationState: NavigationState<T>;
children: (
props: EventEmitterProps & {
// Animated value which represents the state of current index
// It can include fractional digits as it represents the intermediate value
position: Animated.AnimatedInterpolation<number>;
// Function to actually render the content of the pager
// The parent component takes care of rendering
render: (children: React.ReactNode) => React.ReactNode;
// Callback to call when switching the tab
// The tab switch animation is performed even if the index in state is unchanged
jumpTo: (key: string) => void;
}
) => React.ReactElement;
};
const DEAD_ZONE = 12;
const DefaultTransitionSpec = {
timing: Animated.spring,
stiffness: 1000,
damping: 500,
mass: 3,
overshootClamping: true,
};
export function PanResponderAdapter<T extends Route>({
layout,
keyboardDismissMode = 'auto',
swipeEnabled = true,
navigationState,
onIndexChange,
onSwipeStart,
onSwipeEnd,
children,
style,
animationEnabled = false,
layoutDirection = 'ltr',
}: Props<T>) {
const { routes, index } = navigationState;
const panX = useAnimatedValue(0);
const listenersRef = React.useRef<Listener[]>([]);
const navigationStateRef = React.useRef(navigationState);
const layoutRef = React.useRef(layout);
const onIndexChangeRef = React.useRef(onIndexChange);
const currentIndexRef = React.useRef(index);
const pendingIndexRef = React.useRef<number>();
const swipeVelocityThreshold = 0.15;
const swipeDistanceThreshold = layout.width / 1.75;
const jumpToIndex = useLatestCallback(
(index: number, animate = animationEnabled) => {
const offset = -index * layoutRef.current.width;
const { timing, ...transitionConfig } = DefaultTransitionSpec;
if (animate) {
Animated.parallel([
timing(panX, {
...transitionConfig,
toValue: offset,
useNativeDriver: false,
}),
]).start(({ finished }) => {
if (finished) {
onIndexChangeRef.current(index);
pendingIndexRef.current = undefined;
}
});
pendingIndexRef.current = index;
} else {
panX.setValue(offset);
onIndexChangeRef.current(index);
pendingIndexRef.current = undefined;
}
}
);
React.useEffect(() => {
navigationStateRef.current = navigationState;
layoutRef.current = layout;
onIndexChangeRef.current = onIndexChange;
});
React.useEffect(() => {
const offset = -navigationStateRef.current.index * layout.width;
panX.setValue(offset);
}, [layout.width, panX]);
React.useEffect(() => {
if (keyboardDismissMode === 'auto') {
Keyboard.dismiss();
}
if (layout.width && currentIndexRef.current !== index) {
currentIndexRef.current = index;
jumpToIndex(index);
}
}, [jumpToIndex, keyboardDismissMode, layout.width, index]);
const isMovingHorizontally = (
_: GestureResponderEvent,
gestureState: PanResponderGestureState
) => {
return (
Math.abs(gestureState.dx) > Math.abs(gestureState.dy * 2) &&
Math.abs(gestureState.vx) > Math.abs(gestureState.vy * 2)
);
};
const canMoveScreen = (
event: GestureResponderEvent,
gestureState: PanResponderGestureState
) => {
if (swipeEnabled === false) {
return false;
}
const diffX =
layoutDirection === 'rtl' ? -gestureState.dx : gestureState.dx;
return (
isMovingHorizontally(event, gestureState) &&
((diffX >= DEAD_ZONE && currentIndexRef.current > 0) ||
(diffX <= -DEAD_ZONE && currentIndexRef.current < routes.length - 1))
);
};
const startGesture = () => {
onSwipeStart?.();
if (keyboardDismissMode === 'on-drag') {
Keyboard.dismiss();
}
panX.stopAnimation();
// @ts-expect-error: _value is private, but docs use it as well
panX.setOffset(panX._value);
};
const respondToGesture = (
_: GestureResponderEvent,
gestureState: PanResponderGestureState
) => {
const diffX =
layoutDirection === 'rtl' ? -gestureState.dx : gestureState.dx;
if (
// swiping left
(diffX > 0 && index <= 0) ||
// swiping right
(diffX < 0 && index >= routes.length - 1)
) {
return;
}
if (layout.width) {
// @ts-expect-error: _offset is private, but docs use it as well
const position = (panX._offset + diffX) / -layout.width;
const next =
position > index ? Math.ceil(position) : Math.floor(position);
if (next !== index) {
listenersRef.current.forEach((listener) => listener(next));
}
}
panX.setValue(diffX);
};
const finishGesture = (
_: GestureResponderEvent,
gestureState: PanResponderGestureState
) => {
panX.flattenOffset();
onSwipeEnd?.();
const currentIndex =
typeof pendingIndexRef.current === 'number'
? pendingIndexRef.current
: currentIndexRef.current;
let nextIndex = currentIndex;
if (
Math.abs(gestureState.dx) > Math.abs(gestureState.dy) &&
Math.abs(gestureState.vx) > Math.abs(gestureState.vy) &&
(Math.abs(gestureState.dx) > swipeDistanceThreshold ||
Math.abs(gestureState.vx) > swipeVelocityThreshold)
) {
nextIndex = Math.round(
Math.min(
Math.max(
0,
layoutDirection === 'rtl'
? currentIndex + gestureState.dx / Math.abs(gestureState.dx)
: currentIndex - gestureState.dx / Math.abs(gestureState.dx)
),
routes.length - 1
)
);
currentIndexRef.current = nextIndex;
}
if (!isFinite(nextIndex)) {
nextIndex = currentIndex;
}
jumpToIndex(nextIndex, true);
};
const addEnterListener = useLatestCallback((listener: Listener) => {
listenersRef.current.push(listener);
return () => {
const index = listenersRef.current.indexOf(listener);
if (index > -1) {
listenersRef.current.splice(index, 1);
}
};
});
const jumpTo = useLatestCallback((key: string) => {
const index = navigationStateRef.current.routes.findIndex(
(route: { key: string }) => route.key === key
);
jumpToIndex(index);
onIndexChange(index);
});
const panResponder = PanResponder.create({
onMoveShouldSetPanResponder: canMoveScreen,
onMoveShouldSetPanResponderCapture: canMoveScreen,
onPanResponderGrant: startGesture,
onPanResponderMove: respondToGesture,
onPanResponderTerminate: finishGesture,
onPanResponderRelease: finishGesture,
onPanResponderTerminationRequest: () => true,
});
const maxTranslate = layout.width * (routes.length - 1);
const translateX = Animated.multiply(
panX.interpolate({
inputRange: [-maxTranslate, 0],
outputRange: [-maxTranslate, 0],
extrapolate: 'clamp',
}),
layoutDirection === 'rtl' ? -1 : 1
);
const position = React.useMemo(
() => (layout.width ? Animated.divide(panX, -layout.width) : null),
[layout.width, panX]
);
return children({
position: position ?? new Animated.Value(index),
addEnterListener,
jumpTo,
render: (children) => (
<Animated.View
style={[
styles.sheet,
layout.width
? {
width: routes.length * layout.width,
transform: [{ translateX }],
}
: null,
style,
]}
{...panResponder.panHandlers}
>
{React.Children.map(children, (child, i) => {
const route = routes[i];
const focused = i === index;
return (
<View
key={route.key}
style={
layout.width
? { width: layout.width }
: focused
? StyleSheet.absoluteFill
: null
}
>
{focused || layout.width ? child : null}
</View>
);
})}
</Animated.View>
),
});
}
const styles = StyleSheet.create({
sheet: {
flex: 1,
flexDirection: 'row',
alignItems: 'stretch',
},
});