Commit 6eb7261
authored
[Web] Fix handling of
## Description
Right now, if one changes `enabled` property of handlers to `false`,
they can never activate again. In the following example:
```ts
const [enabled, setEnabled] = useState(false);
const pan = Gesture.Pan().enabled(enabled)
```
gesture will never become active, even if `enabled` changes to `true`.
Same happens when `enabled` is set to `true` and then changed to `false`
and `true` again.
This happens because we set `enabled` to `true` by default in
`setGestureConfig`, thus the following check:
```ts
if (config.enabled !== undefined && this.enabled !== config.enabled) { ... }
```
passes only when `config.enabled` is `false`.
I've set `enabled` to `null` by default, and then if `config.enabled` is
defined, it is assigned given value. Else default will be `true`. To fix
the issue, I've removed reset of `enabled` in `resetConfig` method.
Alternative approach would be to follow Android implementation and
always react to changes in `enabled` even in cases like `false`
$\rightarrow$ `false` and `true` $\rightarrow$ `true`.
## Test plan
<details>
<summary>Tested on the following code:</summary>
```tsx
import React from 'react';
import { Button, StyleSheet, View } from 'react-native';
import {
Gesture,
GestureDetector,
GestureHandlerRootView,
NativeDetector,
usePan,
} from 'react-native-gesture-handler';
import Animated, {
useAnimatedStyle,
useSharedValue,
} from 'react-native-reanimated';
export default function EmptyExample() {
const [showDetector, setShowDetector] = React.useState(true);
const [enablePan1, setEnablePan1] = React.useState(true);
const enablePan2 = useSharedValue(true);
const pan1 = Gesture.Pan().enabled(enablePan1);
const pan2 = usePan({
enabled: enablePan2,
});
const as = useAnimatedStyle(() => {
return {
backgroundColor: enablePan2.value ? 'lightgreen' : 'crimson',
};
});
return (
<GestureHandlerRootView style={styles.container}>
<Button
title="Toggle old API enabled!"
onPress={() => {
setEnablePan1((prev) => !prev);
}}
/>
<GestureDetector gesture={pan1}>
<View
style={[
styles.box,
{ backgroundColor: enablePan1 ? 'lightgreen' : 'crimson' },
]}
/>
</GestureDetector>
<Button
title="Toggle new API enabled!"
onPress={() => {
enablePan2.value = !enablePan2.value;
}}
/>
<Button
title="Toggle NativeDetetor"
onPress={() => {
setShowDetector((prev) => !prev);
}}
/>
{showDetector && (
<NativeDetector gesture={pan2}>
<Animated.View style={[styles.box, as]} />
</NativeDetector>
)}
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
box: {
width: 200,
height: 200,
borderRadius: 20,
},
});
```
</details>enabled prop (#3726)1 parent d2cbb50 commit 6eb7261
File tree
4 files changed
+38
-23
lines changed- packages/react-native-gesture-handler/src/web
- handlers
- tools
4 files changed
+38
-23
lines changedLines changed: 17 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
43 | | - | |
| 43 | + | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| |||
712 | 712 | | |
713 | 713 | | |
714 | 714 | | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
715 | 730 | | |
716 | 731 | | |
717 | 732 | | |
718 | 733 | | |
719 | 734 | | |
720 | 735 | | |
721 | | - | |
722 | | - | |
723 | | - | |
724 | | - | |
| 736 | + | |
725 | 737 | | |
726 | 738 | | |
727 | 739 | | |
| |||
911 | 923 | | |
912 | 924 | | |
913 | 925 | | |
914 | | - | |
915 | 926 | | |
916 | 927 | | |
917 | 928 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
97 | 106 | | |
98 | 107 | | |
99 | 108 | | |
| |||
Lines changed: 11 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
42 | | - | |
43 | | - | |
44 | 42 | | |
45 | 43 | | |
46 | 44 | | |
| |||
49 | 47 | | |
50 | 48 | | |
51 | 49 | | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
56 | 50 | | |
57 | 51 | | |
58 | 52 | | |
| |||
64 | 58 | | |
65 | 59 | | |
66 | 60 | | |
| 61 | + | |
| 62 | + | |
67 | 63 | | |
68 | 64 | | |
69 | 65 | | |
| |||
73 | 69 | | |
74 | 70 | | |
75 | 71 | | |
76 | | - | |
| 72 | + | |
77 | 73 | | |
| 74 | + | |
78 | 75 | | |
79 | 76 | | |
80 | 77 | | |
| 78 | + | |
| 79 | + | |
81 | 80 | | |
82 | 81 | | |
83 | 82 | | |
| |||
203 | 202 | | |
204 | 203 | | |
205 | 204 | | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
215 | 208 | | |
216 | 209 | | |
217 | 210 | | |
| |||
246 | 239 | | |
247 | 240 | | |
248 | 241 | | |
| 242 | + | |
| 243 | + | |
249 | 244 | | |
250 | 245 | | |
251 | 246 | | |
| |||
0 commit comments