Commit ddf9ffa
authored
[Web | iOS] Fix
## Description
This PR aims to align `Switch` behavior across platforms.
### iOS
On `iOS` only first tap on switch would work, later calls would be
suppressed. Turns out that `UISwitch` triggers only
`UIControlEventTouchUpInside` and `UIControlEventValueChanged` callbacks
- no `UIControlEventTouchDown`. This was verified on standalone, native
iOS app in `SwiftUI`.
To fix that, I've moved Gesture Handler events flow to
`UIControlEventValueChanged`.
### web
On web there's a problem with hierarchy - current implementation assumes
that our `view` is `Switch`. In reality it is `div` wrapper that
contains `input` and other stuff that build this component.
## Test plan
<details>
<summary>Tested on the following example</summary>
```tsx
import { useState } from 'react';
import { StyleSheet, Switch } from 'react-native';
import {
GestureDetector,
GestureHandlerRootView,
Switch as GestureSwitch,
useNativeGesture,
} from 'react-native-gesture-handler';
export default function App() {
const g = useNativeGesture({
onBegin: () => {
console.log('gesture begin');
},
onActivate: () => {
console.log('gesture activate');
},
onUpdate: () => {
console.log('gesture update');
},
onDeactivate: () => {
console.log('gesture deactivate');
},
onFinalize: () => {
console.log('gesture finalize');
},
});
const [enabled, setEnabled] = useState(false);
return (
<GestureHandlerRootView style={styles.container}>
<Switch
value={enabled}
onValueChange={setEnabled}
style={{ backgroundColor: 'red' }}
/>
<GestureDetector gesture={g}>
<Switch
value={enabled}
onValueChange={setEnabled}
style={{ backgroundColor: 'green' }}
/>
</GestureDetector>
<GestureSwitch
onBegin={() => {
console.log('gesture begin');
}}
onActivate={() => {
console.log('gesture activate');
}}
onUpdate={() => {
console.log('gesture update');
}}
onDeactivate={() => {
console.log('gesture deactivate');
}}
onFinalize={() => {
console.log('gesture finalize');
}}
value={enabled}
onValueChange={setEnabled}
style={{ backgroundColor: 'blue' }}
/>
</GestureHandlerRootView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
});
```
</details>Switch component (#4112)1 parent b18dbd1 commit ddf9ffa
2 files changed
Lines changed: 60 additions & 22 deletions
File tree
- packages/react-native-gesture-handler
- apple/Handlers
- src/web/handlers
Lines changed: 48 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
| 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 | + | |
158 | 169 | | |
159 | 170 | | |
160 | 171 | | |
| |||
191 | 202 | | |
192 | 203 | | |
193 | 204 | | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
194 | 226 | | |
195 | 227 | | |
196 | 228 | | |
| |||
Lines changed: 12 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
| 53 | + | |
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
| |||
101 | 103 | | |
102 | 104 | | |
103 | 105 | | |
104 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
105 | 111 | | |
106 | 112 | | |
107 | 113 | | |
| |||
114 | 120 | | |
115 | 121 | | |
116 | 122 | | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
122 | 128 | | |
123 | 129 | | |
124 | 130 | | |
| |||
0 commit comments