Skip to content

Commit 48cb50c

Browse files
committed
useReanimatedEventsManager
1 parent 48cb976 commit 48cb50c

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,31 @@ import {
66
AnimatedNativeDetector,
77
NativeDetectorProps,
88
nativeDetectorStyles,
9-
ReanimatedNativeDetector,
109
} from './common';
10+
import { useReanimatedEventsManager } from './useReanimatedEventsManager';
1111

1212
export function NativeDetector<THandlerData, TConfig>({
1313
gesture,
1414
children,
1515
}: NativeDetectorProps<THandlerData, TConfig>) {
16+
// TODO: is it possible with useReanimatedEventsManager to handle both Animated and Reanimated at the same time?
17+
1618
const NativeDetectorComponent = gesture.config.dispatchesAnimatedEvents
1719
? AnimatedNativeDetector
18-
: gesture.config.shouldUseReanimatedDetector
19-
? ReanimatedNativeDetector
20-
: HostGestureDetector;
20+
: HostGestureDetector;
2121

2222
ensureNativeDetectorComponent(NativeDetectorComponent);
2323
configureRelations(gesture);
2424

25+
// TODO: call useReanimatedEventsManager only when using Reanimated
26+
const viewRef = useReanimatedEventsManager(gesture);
2527
const handlerTags = useMemo(() => {
2628
return isComposedGesture(gesture) ? gesture.tags : [gesture.tag];
2729
}, [gesture]);
2830

2931
return (
3032
<NativeDetectorComponent
33+
ref={viewRef}
3134
pointerEvents={'box-none'}
3235
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
3336
onGestureHandlerStateChange={
@@ -40,18 +43,6 @@ export function NativeDetector<THandlerData, TConfig>({
4043
gesture.detectorCallbacks.onGestureHandlerTouchEvent
4144
}
4245
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
43-
onGestureHandlerReanimatedStateChange={
44-
gesture.detectorCallbacks.onReanimatedStateChange
45-
}
46-
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
47-
onGestureHandlerReanimatedEvent={
48-
gesture.detectorCallbacks.onReanimatedUpdateEvent
49-
}
50-
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
51-
onGestureHandlerReanimatedTouchEvent={
52-
gesture.detectorCallbacks.onReanimatedTouchEvent
53-
}
54-
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
5546
onGestureHandlerAnimatedEvent={
5647
gesture.detectorCallbacks.onGestureHandlerAnimatedEvent
5748
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { useEffect, useMemo, useRef } from 'react';
2+
import { Gesture } from '../types';
3+
import { findNodeHandle } from 'react-native';
4+
// TODO: import from reanimatedWrapper
5+
import { NativeEventsManager } from 'react-native-reanimated/src/createAnimatedComponent/NativeEventsManager';
6+
7+
// TODO: web
8+
export function useReanimatedEventsManager<THandlerData, TConfig>(
9+
gesture: Gesture<THandlerData, TConfig>
10+
) {
11+
const prevProps = useRef<any>(null);
12+
const eventManager = useRef<any>(null);
13+
const viewRef = useRef<any>(null);
14+
15+
const reaProps = useMemo(
16+
() => ({
17+
onGestureHandlerReanimatedStateChange:
18+
gesture.detectorCallbacks.onReanimatedStateChange,
19+
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
20+
onGestureHandlerReanimatedEvent:
21+
gesture.detectorCallbacks.onReanimatedUpdateEvent,
22+
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
23+
onGestureHandlerReanimatedTouchEvent:
24+
gesture.detectorCallbacks.onReanimatedTouchEvent,
25+
}),
26+
[
27+
gesture.detectorCallbacks.onReanimatedStateChange,
28+
gesture.detectorCallbacks.onReanimatedTouchEvent,
29+
gesture.detectorCallbacks.onReanimatedUpdateEvent,
30+
]
31+
);
32+
33+
useEffect(() => {
34+
const nativeTag = findNodeHandle(viewRef.current) ?? -1;
35+
viewRef.__nativeTag = nativeTag;
36+
eventManager.current = new NativeEventsManager({
37+
props: reaProps,
38+
_componentRef: viewRef,
39+
getComponentViewTag: () => {
40+
return nativeTag;
41+
},
42+
});
43+
eventManager.current.attachEvents();
44+
45+
return () => {
46+
eventManager.current.detachEvents();
47+
};
48+
}, []);
49+
50+
useEffect(() => {
51+
if (prevProps.current) {
52+
eventManager.current.updateEvents(prevProps.current);
53+
}
54+
prevProps.current = reaProps;
55+
}, [reaProps]);
56+
57+
return viewRef;
58+
}

0 commit comments

Comments
 (0)