11import React , { RefObject , useCallback , useRef , useState } from 'react' ;
2- import { Animated , StyleSheet } from 'react-native' ;
3- import HostGestureDetector from './HostGestureDetector' ;
4- import { tagMessage } from '../../utils' ;
2+ import HostGestureDetector from '../HostGestureDetector' ;
53import {
64 LogicChildren ,
7- Gesture ,
8- DetectorCallbacks ,
95 GestureHandlerEvent ,
10- } from '../types' ;
6+ DetectorCallbacks ,
7+ } from '../../types' ;
118import { DetectorContext } from './useDetectorContext' ;
12- import { Reanimated } from '../../handlers/gestures/reanimatedWrapper' ;
13- import { configureRelations } from './utils' ;
14- import { isComposedGesture } from '../hooks/utils/relationUtils' ;
15-
16- export interface NativeDetectorProps < THandlerData , TConfig > {
17- children ?: React . ReactNode ;
18- gesture : Gesture < THandlerData , TConfig > ;
19- }
20-
21- const AnimatedNativeDetector =
22- Animated . createAnimatedComponent ( HostGestureDetector ) ;
23-
24- const ReanimatedNativeDetector =
25- Reanimated ?. default . createAnimatedComponent ( HostGestureDetector ) ;
26-
27- export function NativeDetector < THandlerData , TConfig > ( {
9+ import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper' ;
10+ import { configureRelations , ensureNativeDetectorComponent } from '../utils' ;
11+ import { isComposedGesture } from '../../hooks/utils/relationUtils' ;
12+ import {
13+ AnimatedNativeDetector ,
14+ InterceptingGestureDetectorProps ,
15+ nativeDetectorStyles ,
16+ ReanimatedNativeDetector ,
17+ } from '../common' ;
18+ import { tagMessage } from '../../../utils' ;
19+
20+ export function InterceptingGestureDetector < THandlerData , TConfig > ( {
2821 gesture,
2922 children,
30- } : NativeDetectorProps < THandlerData , TConfig > ) {
23+ } : InterceptingGestureDetectorProps < THandlerData , TConfig > ) {
3124 const [ logicChildren , setLogicChildren ] = useState < LogicChildren [ ] > ( [ ] ) ;
25+
3226 const logicMethods = useRef <
3327 Map < number , RefObject < DetectorCallbacks < unknown > > >
3428 > ( new Map ( ) ) ;
3529
36- const NativeDetectorComponent = gesture . config . dispatchesAnimatedEvents
30+ const [ shouldUseReanimated , setShouldUseReanimated ] = useState (
31+ gesture ? gesture . config . shouldUseReanimatedDetector : false
32+ ) ;
33+ const [ dispatchesAnimatedEvents , setDispatchesAnimatedEvents ] = useState (
34+ gesture ? gesture . config . dispatchesAnimatedEvents : false
35+ ) ;
36+
37+ const NativeDetectorComponent = dispatchesAnimatedEvents
3738 ? AnimatedNativeDetector
38- : gesture . config . shouldUseReanimatedDetector
39+ : shouldUseReanimated
3940 ? ReanimatedNativeDetector
4041 : HostGestureDetector ;
4142
4243 const register = useCallback (
43- ( child : LogicChildren , methods : RefObject < DetectorCallbacks < unknown > > ) => {
44+ (
45+ child : LogicChildren ,
46+ methods : RefObject < DetectorCallbacks < unknown > > ,
47+ forReanimated : boolean | undefined ,
48+ forAnimated : boolean | undefined
49+ ) => {
50+ setShouldUseReanimated ( ! ! forReanimated ) ;
51+ setDispatchesAnimatedEvents ( ! ! forAnimated ) ;
52+
4453 setLogicChildren ( ( prev ) => {
4554 const index = prev . findIndex ( ( c ) => c . viewTag === child . viewTag ) ;
4655 if ( index !== - 1 ) {
@@ -76,11 +85,9 @@ export function NativeDetector<THandlerData, TConfig>({
7685 ) ;
7786 }
7887
79- configureRelations ( gesture ) ;
80-
8188 const handleGestureEvent = ( key : keyof DetectorCallbacks < THandlerData > ) => {
8289 return ( e : GestureHandlerEvent < THandlerData > ) => {
83- if ( gesture . detectorCallbacks [ key ] ) {
90+ if ( gesture ? .detectorCallbacks [ key ] ) {
8491 gesture . detectorCallbacks [ key ] ( e ) ;
8592 }
8693
@@ -97,7 +104,7 @@ export function NativeDetector<THandlerData, TConfig>({
97104 ( key : keyof DetectorCallbacks < unknown > ) => {
98105 const handlers : ( ( e : GestureHandlerEvent < THandlerData > ) => void ) [ ] = [ ] ;
99106
100- if ( gesture . detectorCallbacks [ key ] ) {
107+ if ( gesture ? .detectorCallbacks [ key ] ) {
101108 handlers . push (
102109 gesture . detectorCallbacks [ key ] as (
103110 e : GestureHandlerEvent < unknown >
@@ -116,21 +123,27 @@ export function NativeDetector<THandlerData, TConfig>({
116123
117124 return handlers ;
118125 } ,
119- [ logicChildren , gesture . detectorCallbacks ]
126+ [ logicChildren , gesture ? .detectorCallbacks ]
120127 ) ;
121128
122129 const reanimatedEventHandler = Reanimated ?. useComposedEventHandler (
123130 getHandlers ( 'onReanimatedUpdateEvent' )
124131 ) ;
125- const reanimedStateChangeHandler = Reanimated ?. useComposedEventHandler (
132+ const reanimatedStateChangeHandler = Reanimated ?. useComposedEventHandler (
126133 getHandlers ( 'onReanimatedStateChange' )
127134 ) ;
128135 const reanimatedTouchEventHandler = Reanimated ?. useComposedEventHandler (
129136 getHandlers ( 'onReanimatedTouchEvent' )
130137 ) ;
131138
139+ ensureNativeDetectorComponent ( NativeDetectorComponent ) ;
140+
141+ if ( gesture ) {
142+ configureRelations ( gesture ) ;
143+ }
144+
132145 return (
133- < DetectorContext . Provider value = { { register, unregister } } >
146+ < DetectorContext value = { { register, unregister } } >
134147 < NativeDetectorComponent
135148 // @ts -ignore This is a type mismatch between RNGH types and RN Codegen types
136149 onGestureHandlerStateChange = { handleGestureEvent (
@@ -140,32 +153,36 @@ export function NativeDetector<THandlerData, TConfig>({
140153 onGestureHandlerEvent = { handleGestureEvent ( 'onGestureHandlerEvent' ) }
141154 // @ts -ignore This is a type mismatch between RNGH types and RN Codegen types
142155 onGestureHandlerAnimatedEvent = {
143- gesture . detectorCallbacks . onGestureHandlerAnimatedEvent
156+ gesture ? .detectorCallbacks . onGestureHandlerAnimatedEvent
144157 }
145158 // @ts -ignore This is a type mismatch between RNGH types and RN Codegen types
146159 onGestureHandlerTouchEvent = { handleGestureEvent (
147160 'onGestureHandlerTouchEvent'
148161 ) }
149162 // @ts -ignore This is a type mismatch between RNGH types and RN Codegen types
150- onGestureHandlerReanimatedStateChange = { reanimatedEventHandler }
163+ onGestureHandlerReanimatedStateChange = {
164+ shouldUseReanimated ? reanimatedStateChangeHandler : undefined
165+ }
151166 // @ts -ignore This is a type mismatch between RNGH types and RN Codegen types
152- onGestureHandlerReanimatedEvent = { reanimedStateChangeHandler }
167+ onGestureHandlerReanimatedEvent = {
168+ shouldUseReanimated ? reanimatedEventHandler : undefined
169+ }
153170 // @ts -ignore This is a type mismatch between RNGH types and RN Codegen types
154- onGestureHandlerReanimatedTouchEvent = { reanimatedTouchEventHandler }
171+ onGestureHandlerReanimatedTouchEvent = {
172+ shouldUseReanimated ? reanimatedTouchEventHandler : undefined
173+ }
155174 moduleId = { globalThis . _RNGH_MODULE_ID }
156- handlerTags = { isComposedGesture ( gesture ) ? gesture . tags : [ gesture . tag ] }
157- style = { styles . detector }
175+ handlerTags = {
176+ gesture
177+ ? isComposedGesture ( gesture )
178+ ? gesture . tags
179+ : [ gesture . tag ]
180+ : [ ]
181+ }
182+ style = { nativeDetectorStyles . detector }
158183 logicChildren = { logicChildren } >
159184 { children }
160185 </ NativeDetectorComponent >
161- </ DetectorContext . Provider >
186+ </ DetectorContext >
162187 ) ;
163188}
164-
165- const styles = StyleSheet . create ( {
166- detector : {
167- display : 'contents' ,
168- // TODO: remove, debug info only
169- backgroundColor : 'red' ,
170- } ,
171- } ) ;
0 commit comments