Skip to content

Commit 8b43cf5

Browse files
authored
[Android][iOS] Don't handle events on the detector surface (#3823)
## Description Prevents `NativeDetector` from reacting to events that happen on its surface but not on any of its children. This should match the behavior from the V2 API, and feels more like an "expected" result. This doesn't affect the web since `display: contents` there isn't subject to our hacks and the detector doesn't create a box. The behavior after this PR should be consistent across the platforms. Since codegen is not perfect, to put it lightly, I had to redeclare `pointerEvents` in the spec and handle the setter on Android. The only base class that does it automatically is `ReactViewManager`, but we need to create a `ViewGroup` to accept children. ## Test plan |-|Android|iOS| |-|-|-| |Before|<video src='https://github.com/user-attachments/assets/d936d480-ab94-4d6c-803a-1ea06c7b3fa0' />|<video src='https://github.com/user-attachments/assets/e7bf9560-1a6e-4c66-8528-523108655ea4' />| |After|<video src='https://github.com/user-attachments/assets/cfa6aa95-2392-4d1c-a0c5-75d3c751b2b7' />|<video src='https://github.com/user-attachments/assets/4f45bc6d-3699-4293-8ad1-c8b8426d28a5' />|
1 parent 2ebe247 commit 8b43cf5

4 files changed

Lines changed: 15 additions & 0 deletions

File tree

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerDetectorViewManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.swmansion.gesturehandler.react
22

33
import com.facebook.react.bridge.ReadableArray
44
import com.facebook.react.module.annotations.ReactModule
5+
import com.facebook.react.uimanager.PointerEvents.Companion.parsePointerEvents
56
import com.facebook.react.uimanager.ThemedReactContext
67
import com.facebook.react.uimanager.ViewGroupManager
78
import com.facebook.react.uimanager.ViewManagerDelegate
@@ -45,4 +46,8 @@ class RNGestureHandlerDetectorViewManager :
4546
view.onViewDrop()
4647
super.onDropViewInstance(view)
4748
}
49+
50+
override fun setPointerEvents(view: RNGestureHandlerDetectorView, pointerEventsStr: String?) {
51+
view.pointerEvents = parsePointerEvents(pointerEventsStr)
52+
}
4853
}

packages/react-native-gesture-handler/src/specs/RNGestureHandlerDetectorNativeComponent.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
DirectEventHandler,
55
UnsafeMixed,
66
Double,
7+
WithDefault,
78
} from 'react-native/Libraries/Types/CodegenTypes';
89
import type { ViewProps } from 'react-native';
910

@@ -47,6 +48,8 @@ export interface VirtualChildrenProps {
4748
viewTag: Int32;
4849
}
4950

51+
// @ts-expect-error WithDefault adds `| null` to the type, which doesn't align with ViewProps.pointerEvents
52+
// Using Exclude to remove null from the type makes the error go away, but breaks codegen.
5053
export interface NativeProps extends ViewProps {
5154
onGestureHandlerEvent?: DirectEventHandler<GestureHandlerEvent>;
5255
onGestureHandlerStateChange?: DirectEventHandler<GestureHandlerStateChangeEvent>;
@@ -59,6 +62,11 @@ export interface NativeProps extends ViewProps {
5962
handlerTags: Int32[];
6063
moduleId: Int32;
6164
virtualChildren: VirtualChildrenProps[];
65+
66+
pointerEvents?: WithDefault<
67+
'box-none' | 'none' | 'box-only' | 'auto',
68+
'auto'
69+
>;
6270
}
6371

6472
export default codegenNativeComponent<NativeProps>('RNGestureHandlerDetector', {

packages/react-native-gesture-handler/src/v3/detectors/NativeDetector.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export function NativeDetector<THandlerData, TConfig>({
2828

2929
return (
3030
<NativeDetectorComponent
31+
pointerEvents={'box-none'}
3132
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
3233
onGestureHandlerStateChange={
3334
gesture.detectorCallbacks.onGestureHandlerStateChange

packages/react-native-gesture-handler/src/v3/detectors/VirtualDetector/InterceptingGestureDetector.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({
207207
return (
208208
<InterceptingDetectorContext value={contextValue}>
209209
<NativeDetectorComponent
210+
pointerEvents={'box-none'}
210211
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
211212
onGestureHandlerStateChange={useMemo(
212213
() => createGestureEventHandler('onGestureHandlerStateChange'),

0 commit comments

Comments
 (0)