Commit 1a713d0
Ensure no gesture is used across multiple detectors (#4285)
## Description
Currently there's no error when the same gesture is used in multiple
detectors. This may results in a bug, e.g. on Android and iOS only last
detector captures gestures, on web all of them. This PR adds JS side
check whether instance of a gesture was passed into more than one
detector.
> [!NOTE]
> The example below crashes on web, in reattaching scenario. However,
this seems to be unrelated to this PR, so I left that for a follow-up.
## Test plan
<details>
<summary>Tested on the following code:</summary>
```tsx
import { useRef, useState } from 'react';
import { Pressable, StyleSheet, Text, View } from 'react-native';
import { GestureDetector, useTapGesture } from 'react-native-gesture-handler';
import type { FeedbackHandle } from '../../../common';
import { COLORS, commonStyles, Feedback } from '../../../common';
type Mode = 'separate' | 'reattach' | 'shared';
// Demonstrates the guard that forbids attaching a single gesture instance to
// more than one GestureDetector at a time.
//
// - "Separate gestures (OK)" -> each detector gets its own gesture.
// - "Reattach mode (OK)" -> tapping the ⭐ box moves the SAME gesture to
// the other detector. The detector losing it
// releases it before the other claims it, so
// it never throws. The ⭐ badge and its tap
// counter travel with the instance, so you can
// see it is literally the same gesture moving.
// - "Share one gesture (throws)" -> the SAME gesture is rendered in BOTH
// detectors at once. This is the misuse we
// catch - it throws on the second detector.
export default function SharedGestureExample() {
const [mode, setMode] = useState<Mode>('separate');
// In reattach mode, which detector currently holds the shared gesture.
const [sharedOnTop, setSharedOnTop] = useState(true);
// Tap count of the shared gesture - kept in a ref so incrementing it does not
// trigger a second state update (the setSharedOnTop re-render reads it fresh).
const sharedTapsRef = useRef(0);
const feedbackRef = useRef<FeedbackHandle>(null);
const sharedGesture = useTapGesture({
onActivate: () => {
sharedTapsRef.current += 1;
// Tapping the box that holds the shared gesture moves (reattaches) it to
// the other detector - exactly one detector ever holds it, so no throw.
setSharedOnTop((prev) => !prev);
feedbackRef.current?.showMessage('⭐ shared gesture tapped → moving');
},
runOnJS: true,
});
const topOwnGesture = useTapGesture({
onActivate: () => feedbackRef.current?.showMessage('top gesture tapped'),
runOnJS: true,
});
const bottomOwnGesture = useTapGesture({
onActivate: () => feedbackRef.current?.showMessage('bottom gesture tapped'),
runOnJS: true,
});
// Single placeholder used by whichever box does NOT currently hold the shared
// gesture in reattach mode (mirrors the existing `reattaching` example).
const placeholderGesture = useTapGesture({
onActivate: () => feedbackRef.current?.showMessage('inert box tapped'),
runOnJS: true,
});
// Decide which gesture each detector receives, and whether it's the shared one.
let topGesture = topOwnGesture;
let bottomGesture = bottomOwnGesture;
let topHasShared = false;
let bottomHasShared = false;
if (mode === 'shared') {
// Same instance in both detectors at the same time -> throws.
topGesture = sharedGesture;
bottomGesture = sharedGesture;
topHasShared = true;
bottomHasShared = true;
} else if (mode === 'reattach') {
// Exactly one detector holds the shared gesture at any moment -> allowed.
topGesture = sharedOnTop ? sharedGesture : placeholderGesture;
bottomGesture = sharedOnTop ? placeholderGesture : sharedGesture;
topHasShared = sharedOnTop;
bottomHasShared = !sharedOnTop;
}
return (
<View style={commonStyles.centerView}>
<View style={styles.controls}>
<Button
label="Separate gestures (OK)"
onPress={() => setMode('separate')}
/>
<Button
label="Reattach mode (OK)"
onPress={() => {
setMode('reattach');
setSharedOnTop(true);
}}
/>
<Button
label="Share one gesture (throws)"
danger
onPress={() => setMode('shared')}
/>
</View>
<Text style={styles.status}>
mode: {mode}
{mode === 'reattach'
? ` · tap the ⭐ box to move the shared gesture`
: ''}
</Text>
<GestureDetector gesture={topGesture}>
<Box
title="Top"
color={COLORS.NAVY}
hasShared={topHasShared}
sharedTaps={sharedTapsRef.current}
/>
</GestureDetector>
<GestureDetector gesture={bottomGesture}>
<Box
title="Bottom"
color={COLORS.GREEN}
hasShared={bottomHasShared}
sharedTaps={sharedTapsRef.current}
/>
</GestureDetector>
<Feedback ref={feedbackRef} />
</View>
);
}
function Box({
title,
color,
hasShared,
sharedTaps,
}: {
title: string;
color: string;
hasShared: boolean;
sharedTaps: number;
}) {
return (
<View
style={[
commonStyles.box,
{ backgroundColor: color },
hasShared && styles.boxWithShared,
]}>
<Text style={styles.boxText}>{title}</Text>
{hasShared ? (
<Text style={styles.badge}>
⭐ shared{'\n'}
{sharedTaps} taps
</Text>
) : (
<Text style={styles.boxSubText}>own gesture</Text>
)}
</View>
);
}
function Button({
label,
onPress,
danger,
}: {
label: string;
onPress: () => void;
danger?: boolean;
}) {
return (
<Pressable
onPress={onPress}
style={[styles.button, danger && styles.buttonDanger]}>
<Text style={styles.buttonText}>{label}</Text>
</Pressable>
);
}
const styles = StyleSheet.create({
controls: {
gap: 8,
marginBottom: 24,
width: '100%',
paddingHorizontal: 16,
},
button: {
backgroundColor: COLORS.NAVY,
paddingVertical: 12,
borderRadius: 10,
alignItems: 'center',
},
buttonDanger: {
backgroundColor: COLORS.RED,
},
buttonText: {
color: 'white',
fontWeight: '600',
},
status: {
marginBottom: 16,
color: COLORS.NAVY,
fontWeight: '600',
},
boxWithShared: {
borderWidth: 4,
borderColor: COLORS.KINDA_YELLOW,
},
boxText: {
color: 'white',
fontWeight: '700',
fontSize: 18,
},
boxSubText: {
color: 'white',
opacity: 0.8,
marginTop: 4,
},
badge: {
color: COLORS.KINDA_YELLOW,
fontWeight: '700',
textAlign: 'center',
marginTop: 4,
},
});
```
</details>
---------
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent ede0f4d commit 1a713d0
5 files changed
Lines changed: 160 additions & 5 deletions
File tree
- packages/react-native-gesture-handler/src
- __tests__
- v3/detectors
- VirtualDetector
Lines changed: 88 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 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 | + | |
86 | 174 | | |
87 | 175 | | |
88 | 176 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| 42 | + | |
| 43 | + | |
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
233 | 234 | | |
234 | 235 | | |
235 | 236 | | |
| 237 | + | |
| 238 | + | |
236 | 239 | | |
237 | 240 | | |
238 | 241 | | |
| |||
Lines changed: 13 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
60 | 71 | | |
61 | 72 | | |
62 | 73 | | |
63 | 74 | | |
64 | 75 | | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | 76 | | |
70 | 77 | | |
71 | 78 | | |
| |||
96 | 103 | | |
97 | 104 | | |
98 | 105 | | |
| 106 | + | |
99 | 107 | | |
100 | 108 | | |
101 | 109 | | |
| |||
Lines changed: 53 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
0 commit comments