Skip to content

Commit f47d04e

Browse files
committed
conver to rngh 2, fix buggy initial page
1 parent aa5a2d0 commit f47d04e

File tree

3 files changed

+299
-97
lines changed

3 files changed

+299
-97
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
],
3434
"peerDependencies": {
3535
"react-native": ">=0.62.0",
36-
"react-native-gesture-handler": ">=1.10.1",
37-
"react-native-reanimated": ">=2.0.0"
36+
"react-native-gesture-handler": ">=2.0.0",
37+
"react-native-reanimated": ">=2.8.0"
3838
},
3939
"devDependencies": {
4040
"@react-native-community/eslint-config": "^2.0.0",
@@ -49,8 +49,8 @@
4949
"react": "~16.9.0",
5050
"react-native": "^0.62.2",
5151
"react-native-builder-bob": "^0.18.1",
52-
"react-native-gesture-handler": "^1.10.3",
53-
"react-native-reanimated": "^2.2.0",
52+
"react-native-gesture-handler": "^2.0.0",
53+
"react-native-reanimated": "^2.8.0",
5454
"typescript": "^4.2.4"
5555
},
5656
"react-native-builder-bob": {

src/index.tsx

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ import React, {
77
import { StyleProp, StyleSheet, ViewStyle } from "react-native";
88
import Animated, {
99
useAnimatedStyle,
10-
useAnimatedGestureHandler,
1110
useSharedValue,
1211
withSpring,
1312
useDerivedValue,
1413
useAnimatedReaction,
1514
runOnJS,
1615
interpolate,
16+
WithSpringConfig,
1717
} from "react-native-reanimated";
1818
import {
19-
GestureEvent,
20-
PanGestureHandler,
21-
PanGestureHandlerEventPayload,
19+
ComposedGesture,
20+
Gesture,
21+
GestureDetector,
22+
GestureType,
2223
} from "react-native-gesture-handler";
2324

24-
export const DEFAULT_ANIMATION_CONFIG: Animated.WithSpringConfig = {
25+
export const DEFAULT_ANIMATION_CONFIG: WithSpringConfig = {
2526
damping: 20,
2627
mass: 0.2,
2728
stiffness: 100,
@@ -52,9 +53,9 @@ type Props = {
5253
pageInterpolator?: typeof defaultPageInterpolator;
5354
minIndex?: number;
5455
maxIndex?: number;
55-
simultaneousHandlers?: React.Ref<unknown> | React.Ref<unknown>[];
56+
simultaneousGestures?: (ComposedGesture | GestureType)[];
5657
gesturesDisabled?: boolean;
57-
animationConfig?: Partial<Animated.WithSpringConfig>;
58+
animationConfig?: Partial<WithSpringConfig>;
5859
};
5960

6061
type ImperativeApiOptions = {
@@ -78,7 +79,7 @@ function InfinitePager(
7879
pageInterpolator = defaultPageInterpolator,
7980
minIndex = -Infinity,
8081
maxIndex = Infinity,
81-
simultaneousHandlers,
82+
simultaneousGestures = [],
8283
gesturesDisabled,
8384
animationConfig = {},
8485
}: Props,
@@ -158,48 +159,42 @@ function InfinitePager(
158159
[]
159160
);
160161

161-
const gestureHandler = useAnimatedGestureHandler<
162-
GestureEvent<PanGestureHandlerEventPayload>,
163-
{ startX: number }
164-
>(
165-
{
166-
onStart: (_, ctx) => {
167-
ctx.startX = translateX.value;
168-
},
169-
onActive: (event, ctx) => {
170-
const rawVal = ctx.startX + event.translationX;
171-
const page = -rawVal / pageWidth.value;
172-
if (page >= minIndex && page <= maxIndex) {
173-
translateX.value = rawVal;
174-
}
175-
},
176-
onEnd: (evt) => {
177-
const isFling = Math.abs(evt.velocityX) > 500;
178-
let velocityModifier = isFling ? pageWidth.value / 2 : 0;
179-
if (evt.velocityX < 0) velocityModifier *= -1;
180-
let page =
181-
-1 *
182-
Math.round((translateX.value + velocityModifier) / pageWidth.value);
183-
if (page < minIndex) page = minIndex;
184-
if (page > maxIndex) page = maxIndex;
162+
const startX = useSharedValue(0);
163+
164+
const panGesture = Gesture.Pan()
165+
.onBegin(() => {
166+
startX.value = translateX.value;
167+
})
168+
.onUpdate((evt) => {
169+
const rawVal = startX.value + evt.translationX;
170+
const page = -rawVal / pageWidth.value;
171+
if (page >= minIndex && page <= maxIndex) {
172+
translateX.value = rawVal;
173+
}
174+
})
175+
.onEnd((evt) => {
176+
const isFling = Math.abs(evt.velocityX) > 500;
177+
let velocityModifier = isFling ? pageWidth.value / 2 : 0;
178+
if (evt.velocityX < 0) velocityModifier *= -1;
179+
let page =
180+
-1 *
181+
Math.round((translateX.value + velocityModifier) / pageWidth.value);
182+
if (page < minIndex) page = minIndex;
183+
if (page > maxIndex) page = maxIndex;
185184

186-
const animCfg = Object.assign(
187-
{},
188-
DEFAULT_ANIMATION_CONFIG,
189-
animCfgRef.current
190-
);
185+
const animCfg = Object.assign(
186+
{},
187+
DEFAULT_ANIMATION_CONFIG,
188+
animCfgRef.current
189+
);
191190

192-
translateX.value = withSpring(-page * pageWidth.value, animCfg);
193-
},
194-
},
195-
[minIndex, maxIndex]
196-
);
191+
translateX.value = withSpring(-page * pageWidth.value, animCfg);
192+
})
193+
.enabled(!gesturesDisabled);
197194

198195
return (
199-
<PanGestureHandler
200-
enabled={!gesturesDisabled}
201-
onGestureEvent={gestureHandler}
202-
simultaneousHandlers={simultaneousHandlers}
196+
<GestureDetector
197+
gesture={Gesture.Simultaneous(panGesture, ...simultaneousGestures)}
203198
>
204199
<Animated.View
205200
style={style}
@@ -222,7 +217,7 @@ function InfinitePager(
222217
);
223218
})}
224219
</Animated.View>
225-
</PanGestureHandler>
220+
</GestureDetector>
226221
);
227222
}
228223

@@ -284,11 +279,12 @@ const PageWrapper = React.memo(
284279
const animStyle = useAnimatedStyle(() => {
285280
// Short circuit page interpolation to prevent buggy initial values due to possible race condition:
286281
// https://github.com/software-mansion/react-native-reanimated/issues/2571
287-
if (!pageWidth.value) return {};
282+
const isInactivePageBeforeInit = index !== 0 && !pageWidth.value;
283+
const _pageWidth = isInactivePageBeforeInit ? focusAnim : pageWidth;
288284
return pageInterpolatorRef.current({
289285
focusAnim,
290286
pageAnim,
291-
pageWidth,
287+
pageWidth: _pageWidth,
292288
index,
293289
});
294290
}, [pageWidth, pageAnim, index, translation]);

0 commit comments

Comments
 (0)