Commit bd9650f
authored
Fix
## Description
`Pan`'s velocity-based activation criteria (`minVelocity`,
`minVelocityX`, `minVelocityY`) had three
long-standing problems:
1. **Android: `minVelocityY` tested the horizontal velocity.**
`shouldActivate()` declared
`val vy = velocityY` but compared `vx` in both comparisons of the Y
branch, so a pan configured
with only `minVelocityY` activated on fast horizontal movement and never
on vertical movement.
The typo predates the 2022 repo restructure (#2270).
2. **All platforms: per-axis thresholds were compared with sign.** A
positive `minVelocityY` only
activated on downward movement, negative only on upward. This is
surprising — `minVelocityY: 100`
reads as "vertical speed of at least 100", not "drag down". The signed
logic appears to have been
copied from the offset-range checks (`activeOffsetX/Y`), where signed
semantics actually make
sense. The checks now compare absolute values: `abs(velocity) >=
abs(threshold)`, so movement in
either direction along the axis activates. (`minVelocity` already
compared the velocity vector
magnitude and is unchanged.)
3. **Web: `minVelocity` meant something different than on native.** It
was mapped onto the per-axis
X/Y thresholds ("either axis exceeds the value") instead of the vector
magnitude like Android and
iOS compute it. The `minVelocitySq` field existed for this but was never
assigned from the config.
It is now wired up, matching native behavior (e.g. a diagonal drag at
600 pt/s with
`minVelocity: 500` now activates on web as it does on native).
None of these props were documented anywhere. This PR adds them to the
docs (new API + 2.x pages)
and to the JSDoc of all three API layers, described as speed "expressed
in points per second".
> [!WARNING]
> I've used [`grep.app`](https://grep.app/) to check whether these props
are used and seems that they're not, so it _should be safe_ to introduce
these changes.
## Test plan
<details>
<summary>Tested on the following code:</summary>
```tsx
// Repro for Pan minVelocityY issues:
// 1. Android compared vx (horizontal velocity) in the minVelocityY branch,
// so a fast horizontal swipe wrongly activated the pan.
// 2. All platforms compared the velocity with its sign, so a positive
// minVelocityY only activated on downward movement. The checks now use
// absolute values: either direction along the axis activates.
//
// Setup: minDistance is set huge so distance can never activate the pan —
// only the velocity criteria can. Expected: swiping DOWN or UP faster than
// 500 activates; a fast horizontal swipe never does.
import React, { useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import {
GestureDetector,
GestureHandlerRootView,
usePanGesture,
} from 'react-native-gesture-handler';
export default function EmptyExample() {
const [log, setLog] = useState<string[]>([]);
const append = (entry: string) =>
setLog((prev) => [...prev.slice(-8), entry]);
const pan = usePanGesture({
minVelocityY: 800,
runOnJS: true,
onActivate: (e) => {
append(
`ACTIVATED vx=${Math.round(e.velocityX)} vy=${Math.round(e.velocityY)}`
);
},
onFinalize: (e) => {
if (e.canceled) {
append('finished without activation');
}
},
});
return (
<GestureHandlerRootView style={styles.container}>
<Text style={styles.title}>minVelocityY = 500, minDistance = 10000</Text>
<Text style={styles.hint}>
Fast horizontal swipe should NOT activate.{'\n'}
Fast vertical swipe (up OR down) SHOULD activate.
</Text>
<GestureDetector gesture={pan}>
<View style={styles.box} testID="panBox">
<Text style={styles.boxLabel}>swipe here</Text>
</View>
</GestureDetector>
<View style={styles.log}>
{log.map((entry, i) => (
<Text key={i} style={styles.logLine}>
{entry}
</Text>
))}
</View>
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
paddingTop: 60,
},
title: {
fontSize: 16,
fontWeight: 'bold',
},
hint: {
textAlign: 'center',
marginVertical: 12,
color: '#666',
},
box: {
width: 300,
height: 300,
backgroundColor: 'tomato',
borderRadius: 16,
justifyContent: 'center',
alignItems: 'center',
},
boxLabel: {
color: 'white',
fontSize: 18,
},
log: {
marginTop: 20,
minHeight: 200,
alignSelf: 'stretch',
paddingHorizontal: 24,
},
logLine: {
fontFamily: 'monospace',
fontSize: 13,
},
});
```
</details>minVelocity props behavior (#4327)1 parent ffb9a5f commit bd9650f
10 files changed
Lines changed: 94 additions & 17 deletions
File tree
- packages
- docs-gesture-handler
- docs/gestures
- versioned_docs/version-2.x
- gesture-handlers
- gestures
- react-native-gesture-handler
- android/src/main/java/com/swmansion/gesturehandler/core
- apple
- Handlers
- src
- handlers
- gestures
- v3/hooks/gestures/pan
- web/handlers
Lines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
126 | 126 | | |
127 | 127 | | |
128 | 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 | + | |
129 | 153 | | |
130 | 154 | | |
131 | 155 | | |
| |||
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
55 | 67 | | |
56 | 68 | | |
57 | 69 | | |
| |||
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
117 | 117 | | |
118 | 118 | | |
119 | 119 | | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
120 | 132 | | |
121 | 133 | | |
122 | 134 | | |
| |||
Lines changed: 3 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
115 | 116 | | |
116 | 117 | | |
117 | 118 | | |
118 | | - | |
119 | | - | |
120 | | - | |
| 119 | + | |
121 | 120 | | |
122 | 121 | | |
123 | 122 | | |
124 | | - | |
125 | | - | |
126 | | - | |
| 123 | + | |
127 | 124 | | |
128 | 125 | | |
129 | 126 | | |
| |||
Lines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
337 | 337 | | |
338 | 338 | | |
339 | 339 | | |
340 | | - | |
| 340 | + | |
341 | 341 | | |
342 | 342 | | |
343 | | - | |
| 343 | + | |
344 | 344 | | |
345 | 345 | | |
346 | 346 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
| 19 | + | |
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
| |||
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
66 | 70 | | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
67 | 76 | | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
68 | 82 | | |
| 83 | + | |
69 | 84 | | |
70 | 85 | | |
71 | 86 | | |
| |||
Lines changed: 6 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
| 157 | + | |
| 158 | + | |
158 | 159 | | |
159 | 160 | | |
160 | 161 | | |
| |||
163 | 164 | | |
164 | 165 | | |
165 | 166 | | |
166 | | - | |
| 167 | + | |
| 168 | + | |
167 | 169 | | |
168 | 170 | | |
169 | 171 | | |
| |||
172 | 174 | | |
173 | 175 | | |
174 | 176 | | |
175 | | - | |
| 177 | + | |
| 178 | + | |
176 | 179 | | |
177 | 180 | | |
178 | 181 | | |
| |||
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
43 | 47 | | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
44 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
45 | 59 | | |
| 60 | + | |
46 | 61 | | |
47 | 62 | | |
48 | 63 | | |
| |||
Lines changed: 3 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
79 | | - | |
| 78 | + | |
80 | 79 | | |
81 | 80 | | |
82 | 81 | | |
| |||
432 | 431 | | |
433 | 432 | | |
434 | 433 | | |
435 | | - | |
436 | | - | |
| 434 | + | |
437 | 435 | | |
438 | 436 | | |
439 | 437 | | |
440 | 438 | | |
441 | 439 | | |
442 | 440 | | |
443 | 441 | | |
444 | | - | |
445 | | - | |
| 442 | + | |
446 | 443 | | |
447 | 444 | | |
448 | 445 | | |
| |||
0 commit comments