1
- import { useEffect , useRef } from 'react' ;
2
- import { View } from 'react-native' ;
1
+ import { Ref , useEffect , useRef } from 'react' ;
3
2
import RNGestureHandlerModule from '../RNGestureHandlerModule.web' ;
4
3
import { ActionType } from '../ActionType' ;
5
4
import { PropsRef } from '../web/interfaces' ;
5
+ import { View } from 'react-native' ;
6
+ import { tagMessage } from '../utils' ;
6
7
7
8
export interface GestureHandlerDetectorProps extends PropsRef {
8
9
handlerTags : number [ ] ;
@@ -13,13 +14,16 @@ export interface GestureHandlerDetectorProps extends PropsRef {
13
14
const HostGestureDetector = ( props : GestureHandlerDetectorProps ) => {
14
15
const { handlerTags, children } = props ;
15
16
16
- const viewRef = useRef ( null ) ;
17
+ const viewRef = useRef < Element > ( null ) ;
17
18
const propsRef = useRef < PropsRef > ( props ) ;
18
19
const attachedHandlerTags = useRef < Set < number > > ( new Set < number > ( ) ) ;
20
+ const attachedNativeHandlerTags = useRef < Set < number > > ( new Set < number > ( ) ) ;
19
21
20
22
const detachHandlers = ( oldHandlerTags : Set < number > ) => {
21
23
oldHandlerTags . forEach ( ( tag ) => {
22
24
RNGestureHandlerModule . detachGestureHandler ( tag ) ;
25
+ attachedNativeHandlerTags . current . delete ( tag ) ;
26
+ attachedHandlerTags . current . delete ( tag ) ;
23
27
} ) ;
24
28
} ;
25
29
@@ -33,19 +37,42 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
33
37
detachHandlers ( oldHandlerTags ) ;
34
38
35
39
newHandlerTags . forEach ( ( tag ) => {
36
- RNGestureHandlerModule . attachGestureHandler (
37
- tag ,
38
- viewRef . current ,
39
- ActionType . NATIVE_DETECTOR ,
40
- propsRef
41
- ) ;
40
+ if (
41
+ RNGestureHandlerModule . getGestureHandlerNode (
42
+ tag
43
+ ) . shouldAttachGestureToChildView ( )
44
+ ) {
45
+ if ( ! viewRef . current ?. firstChild ) {
46
+ throw new Error (
47
+ tagMessage ( 'Detector expected to have a child element' )
48
+ ) ;
49
+ }
50
+ RNGestureHandlerModule . attachGestureHandler (
51
+ tag ,
52
+ viewRef . current . firstChild ,
53
+ ActionType . NATIVE_DETECTOR ,
54
+ propsRef
55
+ ) ;
56
+ attachedNativeHandlerTags . current . add ( tag ) ;
57
+ } else {
58
+ RNGestureHandlerModule . attachGestureHandler (
59
+ tag ,
60
+ viewRef . current ,
61
+ ActionType . NATIVE_DETECTOR ,
62
+ propsRef
63
+ ) ;
64
+ }
42
65
} ) ;
43
66
attachedHandlerTags . current = currentHandlerTags ;
44
67
} ;
45
68
69
+ useEffect ( ( ) => {
70
+ detachHandlers ( attachedNativeHandlerTags . current ) ;
71
+ } , [ children ] ) ;
72
+
46
73
useEffect ( ( ) => {
47
74
attachHandlers ( new Set ( handlerTags ) ) ;
48
- } , [ handlerTags ] ) ;
75
+ } , [ handlerTags , children ] ) ;
49
76
50
77
useEffect ( ( ) => {
51
78
return ( ) => {
@@ -54,7 +81,7 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
54
81
} , [ ] ) ;
55
82
56
83
return (
57
- < View style = { { display : 'contents' } } ref = { viewRef } >
84
+ < View style = { { display : 'contents' } } ref = { viewRef as Ref < View > } >
58
85
{ children }
59
86
</ View >
60
87
) ;
0 commit comments