Commit f31b8a6
authored
[Web] Activate ScrollView's native gesture on real scroll instead of pointer distance (#4420)
## Description
On web, `NativeViewGestureHandler` activated after ~`15px` of pointer
movement in any direction, even though the browser does the scrolling
itself. A vertical `ScrollView` would claim horizontal drags, and once
active, `InteractionManager` failed any `Pan` whose activation criteria
(`minDistance`, `activeOffsetX`, ...) delayed activation past the slop -
such pans could never activate inside a `ScrollView` or `FlatList`.
This PR adds `ScrollEventManager` which delivers the view's `scroll`
events to handlers via a new `onScroll` hook. Handlers with the
`ScrollView` role now activate only when the view actually scrolls, with
a `2px` pointer travel requirement that ignores momentum-scroll ticks
after a touch meant to stop a fling.
> [!IMPORTANT]
> This covers only new, hook based API
## Test plan
- Unit tests for scroll-driven activation, the momentum guard and the
unchanged legacy path
- Pan activation criteria screen: all boxes activate per their criteria
inside the ScrollView, negatives stay inactive, scrolling works
- Buttons in FlatList screen: scrolling from a button doesn't fire a
press, taps do; tap-to-stop-momentum fires nothing and the next tap
works
<details>
<summary>Tested on the following code:</summary>
```tsx
import React, { useRef, useState } from 'react';
import { Button, StyleSheet, Text, View } from 'react-native';
import {
GestureDetector,
ScrollView,
usePanGesture,
} from 'react-native-gesture-handler';
import Animated, {
interpolateColor,
useAnimatedStyle,
useSharedValue,
withTiming,
} from 'react-native-reanimated';
import type { FeedbackHandle } from '../../../common';
import { COLORS, commonStyles, Feedback } from '../../../common';
type PanConfig = Parameters<typeof usePanGesture>[0];
type DraggableBoxProps = {
label: string;
comment: string;
config?: PanConfig;
onActivated: (label: string) => void;
};
function DraggableBox({ label, comment, config, onActivated }: DraggableBoxProps) {
const translateX = useSharedValue(0);
const translateY = useSharedValue(0);
const colorProgress = useSharedValue(0);
const panGesture = usePanGesture({
minDistance: config?.minDistance,
minVelocity: config?.minVelocity,
activeOffsetX: config?.activeOffsetX,
maxPointers: config?.maxPointers,
runOnJS: true,
onActivate: () => {
colorProgress.value = withTiming(1, { duration: 100 });
onActivated(label);
},
onUpdate: (event) => {
translateX.value = event.translationX;
translateY.value = event.translationY;
},
onFinalize: () => {
colorProgress.value = withTiming(0, { duration: 100 });
translateX.value = withTiming(0);
translateY.value = withTiming(0);
},
});
const animatedStyle = useAnimatedStyle(() => ({
transform: [
{ translateX: translateX.value },
{ translateY: translateY.value },
],
backgroundColor: interpolateColor(
colorProgress.value,
[0, 1],
[COLORS.NAVY, COLORS.GREEN]
),
}));
return (
<View style={[commonStyles.subcontainer, styles.entry]}>
<GestureDetector gesture={panGesture}>
<Animated.View style={[styles.box, animatedStyle]}>
<Text style={styles.label}>{label}</Text>
</Animated.View>
</GestureDetector>
<Text style={commonStyles.instructions}>{comment}</Text>
</View>
);
}
export default function PanActivationCriteriaExample() {
const [maxPointers, setMaxPointers] = useState(1);
const feedbackRef = useRef<FeedbackHandle>(null);
const onActivated = (label: string) =>
feedbackRef.current?.showMessage(`Activated: ${label}`);
return (
<View style={styles.container}>
<ScrollView style={styles.scroll}>
<Text style={commonStyles.instructions}>
Each box turns green the moment its pan activates. Drag each one and
verify the activation criteria are respected.
</Text>
<DraggableBox
label="minDistance: 100"
comment="Should activate only after the finger travels 100pt in any direction."
config={{ minDistance: 100 }}
onActivated={onActivated}
/>
<DraggableBox
label="minVelocity: 800"
comment="Should activate only on a fast drag (over 800pt/s), regardless of direction. Slow drags must never activate."
config={{ minVelocity: 800 }}
onActivated={onActivated}
/>
<DraggableBox
label="activeOffsetX: ±60"
comment="Should activate only after moving 60pt horizontally. Vertical drags must not activate."
config={{ activeOffsetX: [-60, 60] }}
onActivated={onActivated}
/>
<View style={styles.updateSection}>
<DraggableBox
label={`minDistance: 120\nmaxPointers: ${maxPointers}`}
comment="Explicit minDistance combined with another prop updated at runtime. After pressing the button below, activation must still require 120pt of travel — partial config updates must not reset minDistance."
config={{ minDistance: 120, maxPointers }}
onActivated={onActivated}
/>
<Button
title="Update unrelated prop (maxPointers)"
onPress={() => setMaxPointers((prev) => (prev === 1 ? 2 : 1))}
/>
</View>
</ScrollView>
<View style={styles.feedbackOverlay} pointerEvents="none">
<Feedback ref={feedbackRef} duration={2000} />
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
scroll: {
paddingVertical: 24,
},
feedbackOverlay: {
position: 'absolute',
bottom: 20,
alignSelf: 'center',
},
entry: {
paddingVertical: 24,
gap: 12,
},
box: {
width: 150,
height: 150,
borderRadius: 20,
justifyContent: 'center',
alignItems: 'center',
},
label: {
color: 'white',
fontWeight: '600',
textAlign: 'center',
},
updateSection: {
paddingBottom: 32,
},
});
```
</details>1 parent 7c71e64 commit f31b8a6
6 files changed
Lines changed: 309 additions & 6 deletions
File tree
- packages/react-native-gesture-handler/src
- __tests__
- web
- handlers
- tools
Lines changed: 215 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 | + | |
| 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 | + | |
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
146 | 147 | | |
147 | 148 | | |
148 | 149 | | |
| |||
398 | 399 | | |
399 | 400 | | |
400 | 401 | | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
401 | 405 | | |
402 | 406 | | |
403 | 407 | | |
| |||
Lines changed: 48 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
44 | 48 | | |
45 | 49 | | |
46 | 50 | | |
| |||
65 | 69 | | |
66 | 70 | | |
67 | 71 | | |
| 72 | + | |
68 | 73 | | |
69 | 74 | | |
70 | 75 | | |
| |||
86 | 91 | | |
87 | 92 | | |
88 | 93 | | |
| 94 | + | |
| 95 | + | |
89 | 96 | | |
90 | 97 | | |
91 | 98 | | |
| |||
144 | 151 | | |
145 | 152 | | |
146 | 153 | | |
| 154 | + | |
| 155 | + | |
147 | 156 | | |
148 | 157 | | |
149 | 158 | | |
| |||
166 | 175 | | |
167 | 176 | | |
168 | 177 | | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
173 | | - | |
174 | 178 | | |
175 | 179 | | |
176 | 180 | | |
177 | 181 | | |
178 | 182 | | |
179 | 183 | | |
180 | 184 | | |
181 | | - | |
| 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 | + | |
182 | 223 | | |
183 | 224 | | |
184 | 225 | | |
| |||
427 | 468 | | |
428 | 469 | | |
429 | 470 | | |
| 471 | + | |
430 | 472 | | |
431 | 473 | | |
432 | 474 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
| |||
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
78 | 82 | | |
79 | 83 | | |
80 | 84 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
69 | 70 | | |
70 | 71 | | |
71 | 72 | | |
| 73 | + | |
72 | 74 | | |
73 | 75 | | |
74 | 76 | | |
| |||
0 commit comments