Skip to content

Commit ee0392a

Browse files
committed
refactor to use simultaneousWithExternalGesture
+ updating docs with example
1 parent 6aea8e6 commit ee0392a

File tree

2 files changed

+81
-65
lines changed

2 files changed

+81
-65
lines changed

docs/docs/components/reanimated_swipeable.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,21 @@ style object for the container (`Animated.View`), for example to override `overf
128128

129129
style object for the children container (`Animated.View`), for example to apply `flex: 1`.
130130

131-
### `simultaneousGesture`
131+
### `simultaneousWithExternalGesture`
132132

133-
An optional gesture configuration that allows another gesture to be recognized simultaneously with the swipeable gesture. This can be useful for implementing complex gesture interactions where multiple gestures need to be detected at the same time.
133+
An optional gesture configuration that enables another gesture to be recognized simultaneously with the swipeable gesture. This is useful for allowing swipeable gestures to work simultaneously with other gestures.
134+
135+
For example, to enable a pan gesture alongside the swipeable gesture:
136+
137+
```jsx
138+
const panGesture = Gesture.Pan();
139+
140+
<GestureDetector gesture={panGesture}>
141+
<ReanimatedSwipeable simultaneousWithExternalGesture={panGesture} />
142+
</GestureDetector>
143+
```
144+
145+
More details can be found in the [gesture composition documentation](../fundamentals/gesture-composition.md#simultaneouswithexternalgesture).
134146

135147
### `enableTrackpadTwoFingerGesture` (iOS only)
136148

src/components/ReanimatedSwipeable.tsx

+67-63
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ export interface SwipeableProps
201201
childrenContainerStyle?: StyleProp<ViewStyle>;
202202

203203
/**
204-
* A base gesture object containing the configuration and callbacks to be
204+
* A gesture object containing the configuration and callbacks to be
205205
* used with the swipeable's gesture handler.
206206
*/
207-
simultaneousGesture?: GestureType;
207+
simultaneousWithExternalGesture?: GestureType;
208208
}
209209

210210
export interface SwipeableMethods {
@@ -250,7 +250,7 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
250250
onSwipeableClose,
251251
renderLeftActions,
252252
renderRightActions,
253-
simultaneousGesture,
253+
simultaneousWithExternalGesture,
254254
...remainingProps
255255
} = props;
256256

@@ -605,63 +605,71 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
605605
[close, rowState]
606606
);
607607

608-
const panGesture = useMemo(
609-
() =>
610-
Gesture.Pan()
611-
.enabled(enabled !== false)
612-
.enableTrackpadTwoFingerGesture(enableTrackpadTwoFingerGesture)
613-
.activeOffsetX([-dragOffsetFromRightEdge, dragOffsetFromLeftEdge])
614-
.onStart(() => {
615-
updateRightElementWidth();
616-
})
617-
.onUpdate(
618-
(event: GestureUpdateEvent<PanGestureHandlerEventPayload>) => {
619-
userDrag.value = event.translationX;
620-
621-
const direction =
622-
rowState.value === -1
623-
? SwipeDirection.RIGHT
624-
: rowState.value === 1
625-
? SwipeDirection.LEFT
626-
: event.translationX > 0
627-
? SwipeDirection.RIGHT
628-
: SwipeDirection.LEFT;
629-
630-
if (!dragStarted.value) {
631-
dragStarted.value = true;
632-
if (rowState.value === 0 && onSwipeableOpenStartDrag) {
633-
runOnJS(onSwipeableOpenStartDrag)(direction);
634-
} else if (onSwipeableCloseStartDrag) {
635-
runOnJS(onSwipeableCloseStartDrag)(direction);
636-
}
608+
const panGesture = useMemo(() => {
609+
const gesture = Gesture.Pan()
610+
.enabled(enabled !== false)
611+
.enableTrackpadTwoFingerGesture(enableTrackpadTwoFingerGesture)
612+
.activeOffsetX([-dragOffsetFromRightEdge, dragOffsetFromLeftEdge])
613+
.onStart(() => {
614+
updateRightElementWidth();
615+
})
616+
.onUpdate(
617+
(event: GestureUpdateEvent<PanGestureHandlerEventPayload>) => {
618+
userDrag.value = event.translationX;
619+
620+
const direction =
621+
rowState.value === -1
622+
? SwipeDirection.RIGHT
623+
: rowState.value === 1
624+
? SwipeDirection.LEFT
625+
: event.translationX > 0
626+
? SwipeDirection.RIGHT
627+
: SwipeDirection.LEFT;
628+
629+
if (!dragStarted.value) {
630+
dragStarted.value = true;
631+
if (rowState.value === 0 && onSwipeableOpenStartDrag) {
632+
runOnJS(onSwipeableOpenStartDrag)(direction);
633+
} else if (onSwipeableCloseStartDrag) {
634+
runOnJS(onSwipeableCloseStartDrag)(direction);
637635
}
638-
639-
updateAnimatedEvent();
640636
}
641-
)
642-
.onEnd(
643-
(event: GestureStateChangeEvent<PanGestureHandlerEventPayload>) => {
644-
handleRelease(event);
645-
}
646-
)
647-
.onFinalize(() => {
648-
dragStarted.value = false;
649-
}),
650-
[
651-
dragOffsetFromLeftEdge,
652-
dragOffsetFromRightEdge,
653-
dragStarted,
654-
enableTrackpadTwoFingerGesture,
655-
enabled,
656-
handleRelease,
657-
onSwipeableCloseStartDrag,
658-
onSwipeableOpenStartDrag,
659-
rowState,
660-
updateAnimatedEvent,
661-
updateRightElementWidth,
662-
userDrag,
663-
]
664-
);
637+
638+
updateAnimatedEvent();
639+
}
640+
)
641+
.onEnd(
642+
(event: GestureStateChangeEvent<PanGestureHandlerEventPayload>) => {
643+
handleRelease(event);
644+
}
645+
)
646+
.onFinalize(() => {
647+
dragStarted.value = false;
648+
});
649+
650+
// Add simultaneousWithExternalGesture only if it has a value
651+
if (simultaneousWithExternalGesture) {
652+
gesture.simultaneousWithExternalGesture(
653+
simultaneousWithExternalGesture
654+
);
655+
}
656+
657+
return gesture;
658+
}, [
659+
dragOffsetFromLeftEdge,
660+
dragOffsetFromRightEdge,
661+
dragStarted,
662+
enableTrackpadTwoFingerGesture,
663+
enabled,
664+
handleRelease,
665+
onSwipeableCloseStartDrag,
666+
onSwipeableOpenStartDrag,
667+
rowState,
668+
updateAnimatedEvent,
669+
updateRightElementWidth,
670+
userDrag,
671+
simultaneousWithExternalGesture,
672+
]);
665673

666674
useImperativeHandle(ref, () => swipeableMethods, [swipeableMethods]);
667675

@@ -673,12 +681,8 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
673681
[appliedTranslation, rowState]
674682
);
675683

676-
const swipeableGesture = simultaneousGesture
677-
? Gesture.Simultaneous(panGesture, simultaneousGesture)
678-
: panGesture;
679-
680684
const swipeableComponent = (
681-
<GestureDetector gesture={swipeableGesture} touchAction="pan-y">
685+
<GestureDetector gesture={panGesture} touchAction="pan-y">
682686
<Animated.View
683687
{...remainingProps}
684688
onLayout={onRowLayout}

0 commit comments

Comments
 (0)