Commit 43b28f4
authored
## Description
Adds `cancelOnLeave` prop to the `Touchable` component, set to `true` by
default. It doesn't take effect on the web for now.
It prevents `NativeViewGestureHandler` from canceling when the pointer
leaves the view bounds, and tracks `pointerInside` from update events to
call `onPressIn` and `onPressOut` callbacks. `onPress` is called only if
the pointer is released in the bounds of the button.
## Test plan
<details>
<summary>Expand</summary>
```jsx
import React, { useState } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Touchable } from 'react-native-gesture-handler';
export default function EmptyExample() {
const [isActive1, setIsActive1] = useState(false);
const [isActive2, setIsActive2] = useState(false);
return (
<View style={styles.container}>
<Touchable
style={[styles.button, isActive1 && styles.buttonActive]}
onPress={() => console.log('pressed')}
onPressIn={() => {
console.log('onPressIn');
setIsActive1(true)
}}
onPressOut={() => {
console.log('onPressOut');
setIsActive1(false)
}}
activeScale={1.05}
exclusive={false}
cancelOnLeave={true}>
<Text style={styles.label}>{isActive1 ? 'PRESS ME' : 'Press me'}</Text>
</Touchable>
<Touchable
style={[styles.button, isActive2 && styles.buttonActive]}
onPress={() => console.log('pressed')}
onPressIn={() => {
console.log('onPressIn');
setIsActive2(true)
}}
onPressOut={() => {
console.log('onPressOut');
setIsActive2(false)
}}
activeScale={1.05}
exclusive={false}
cancelOnLeave={true}>
<Text style={styles.label}>{isActive2 ? 'PRESS ME' : 'Press me'}</Text>
</Touchable>
<Text style={styles.hint}>
Press the button, then slowly drag your pointer away from it.
</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 24,
gap: 24,
},
button: {
backgroundColor: '#ddd',
paddingHorizontal: 32,
paddingVertical: 16,
borderRadius: 8,
},
buttonActive: {
backgroundColor: '#f97316',
},
label: {
fontSize: 20,
fontWeight: '600',
},
hint: {
textAlign: 'center',
opacity: 0.6,
fontSize: 14,
},
});
```
</details>
|-|cancelOnLeave=true|cancelOnLeave=false|
|-|-|-|
|Android|<video
src="https://github.com/user-attachments/assets/b2fb1d24-4dd1-4324-99ec-81c11a419af9"
/>|<video
src="https://github.com/user-attachments/assets/7080ef75-6a86-44c9-ad7a-2f49326c03ad"
/>|
|iOS|<video
src="https://github.com/user-attachments/assets/bcaa4f1d-8ce2-4265-a9ff-76a86479814f"
/>|<video
src="https://github.com/user-attachments/assets/e01b6a31-799b-45cb-ba5a-46854c29abf0"
/>|
1 parent bfa4f35 commit 43b28f4
5 files changed
Lines changed: 138 additions & 16 deletions
File tree
- packages
- docs-gesture-handler/docs/components
- react-native-gesture-handler
- android/src/main/java/com/swmansion/gesturehandler/react
- apple
- src/v3/components/Touchable
Lines changed: 32 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
270 | 270 | | |
271 | 271 | | |
272 | 272 | | |
273 | | - | |
274 | | - | |
275 | | - | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
276 | 295 | | |
277 | 296 | | |
278 | 297 | | |
| |||
314 | 333 | | |
315 | 334 | | |
316 | 335 | | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
Lines changed: 33 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
379 | 379 | | |
380 | 380 | | |
381 | 381 | | |
| 382 | + | |
382 | 383 | | |
383 | 384 | | |
384 | 385 | | |
| |||
481 | 482 | | |
482 | 483 | | |
483 | 484 | | |
484 | | - | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
485 | 509 | | |
486 | 510 | | |
487 | 511 | | |
| |||
719 | 743 | | |
720 | 744 | | |
721 | 745 | | |
| 746 | + | |
| 747 | + | |
| 748 | + | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
722 | 752 | | |
723 | 753 | | |
724 | 754 | | |
| |||
728 | 758 | | |
729 | 759 | | |
730 | 760 | | |
731 | | - | |
732 | | - | |
733 | | - | |
734 | | - | |
735 | | - | |
| 761 | + | |
| 762 | + | |
736 | 763 | | |
737 | 764 | | |
738 | 765 | | |
| |||
Lines changed: 11 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
| |||
566 | 568 | | |
567 | 569 | | |
568 | 570 | | |
| 571 | + | |
569 | 572 | | |
570 | 573 | | |
571 | 574 | | |
572 | 575 | | |
573 | 576 | | |
574 | 577 | | |
575 | 578 | | |
| 579 | + | |
576 | 580 | | |
577 | 581 | | |
578 | 582 | | |
579 | 583 | | |
580 | 584 | | |
581 | 585 | | |
582 | 586 | | |
| 587 | + | |
583 | 588 | | |
584 | | - | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
585 | 594 | | |
586 | 595 | | |
587 | 596 | | |
| |||
Lines changed: 47 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
25 | 31 | | |
26 | 32 | | |
27 | 33 | | |
| |||
35 | 41 | | |
36 | 42 | | |
37 | 43 | | |
| 44 | + | |
38 | 45 | | |
39 | 46 | | |
40 | 47 | | |
41 | 48 | | |
42 | 49 | | |
43 | 50 | | |
| 51 | + | |
44 | 52 | | |
45 | 53 | | |
46 | 54 | | |
| |||
62 | 70 | | |
63 | 71 | | |
64 | 72 | | |
| 73 | + | |
65 | 74 | | |
66 | 75 | | |
67 | 76 | | |
68 | 77 | | |
69 | 78 | | |
| 79 | + | |
| 80 | + | |
70 | 81 | | |
71 | 82 | | |
72 | 83 | | |
| |||
80 | 91 | | |
81 | 92 | | |
82 | 93 | | |
83 | | - | |
84 | | - | |
| 94 | + | |
| 95 | + | |
85 | 96 | | |
86 | 97 | | |
87 | 98 | | |
88 | 99 | | |
89 | 100 | | |
90 | 101 | | |
91 | 102 | | |
92 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
93 | 108 | | |
94 | 109 | | |
95 | 110 | | |
| |||
99 | 114 | | |
100 | 115 | | |
101 | 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 | + | |
102 | 143 | | |
103 | 144 | | |
104 | 145 | | |
| |||
118 | 159 | | |
119 | 160 | | |
120 | 161 | | |
| 162 | + | |
121 | 163 | | |
122 | 164 | | |
123 | | - | |
| 165 | + | |
| 166 | + | |
124 | 167 | | |
125 | 168 | | |
126 | 169 | | |
| |||
Lines changed: 15 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
22 | 25 | | |
23 | 26 | | |
24 | 27 | | |
25 | 28 | | |
26 | 29 | | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
27 | 35 | | |
28 | 36 | | |
29 | 37 | | |
| |||
38 | 46 | | |
39 | 47 | | |
40 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
41 | 55 | | |
0 commit comments