Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ export type ReanimatedHandler<THandlerData> = {
context: ReanimatedContext<THandlerData>;
};

export type NativeEventsManager = new (component: {
props: Record<string, unknown>;
_componentRef: React.Ref<unknown>;
// Removed in https://github.com/software-mansion/react-native-reanimated/pull/6736
// but we likely want to keep it for compatibility with older Reanimated versions
_componentViewTag: number;
getComponentViewTag: () => number;
}) => {
attachEvents: () => void;
detachEvents: () => void;
updateEvents: (prevProps: Record<string, unknown>) => void;
};

let Reanimated:
| {
default: {
Expand All @@ -38,6 +51,7 @@ let Reanimated:
options?: unknown
): ComponentClass<P>;
};
NativeEventsManager: NativeEventsManager;
useHandler: <THandlerData>(
handlers: GestureCallbacks<THandlerData>
) => ReanimatedHandler<THandlerData>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import RNGestureHandlerDetectorNativeComponent from '../../specs/RNGestureHandlerDetectorNativeComponent';
export type { NativeProps as RNGestureHandlerDetectorNativeComponentProps } from '../../specs/RNGestureHandlerDetectorNativeComponent';
const HostGestureDetector = RNGestureHandlerDetectorNativeComponent;
export default HostGestureDetector;
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
AnimatedNativeDetector,
NativeDetectorProps,
nativeDetectorStyles,
ReanimatedNativeDetector,
} from './common';
import { ReanimatedNativeDetector } from './ReanimatedNativeDetector';

export function NativeDetector<THandlerData, TConfig>({
gesture,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { useEffect, useMemo, useRef } from 'react';
import {
NativeEventsManager,
Reanimated,
} from '../../handlers/gestures/reanimatedWrapper';
import HostGestureDetector, {
type RNGestureHandlerDetectorNativeComponentProps,
} from './HostGestureDetector';
import { findNodeHandle } from 'react-native';

let NativeEventsManagerImpl = Reanimated?.NativeEventsManager;

if (!NativeEventsManagerImpl) {
// When Reanimated.NativeEventsManager is undefined, it may be that an older
// Reanimated version is used which doesn't export NativeEventsManager, or
// Reanimated is not installed at all. For the older versions, try to import
// NativeEventsManager by a subpath. Otherwise, it will stay undefined.
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
NativeEventsManagerImpl =
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-member-access
require('react-native-reanimated/src/createAnimatedComponent/NativeEventsManager').NativeEventsManager;
} catch {
// fail silently
}
}

type WorkletProps = Pick<
RNGestureHandlerDetectorNativeComponentProps,
| 'onGestureHandlerReanimatedStateChange'
| 'onGestureHandlerReanimatedEvent'
| 'onGestureHandlerReanimatedTouchEvent'
>;

function LeanReanimatedNativeDetector(
props: RNGestureHandlerDetectorNativeComponentProps
) {
const prevProps = useRef<WorkletProps>(null);
const eventManager = useRef<InstanceType<NativeEventsManager> | null>(null);
const viewRef = useRef<any>(null);

const {
onGestureHandlerReanimatedStateChange,
onGestureHandlerReanimatedEvent,
onGestureHandlerReanimatedTouchEvent,
...restProps
} = props;

const reaProps: WorkletProps = useMemo(
() => ({
onGestureHandlerReanimatedStateChange,
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
onGestureHandlerReanimatedEvent,
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
onGestureHandlerReanimatedTouchEvent,
}),
[
onGestureHandlerReanimatedEvent,
onGestureHandlerReanimatedStateChange,
onGestureHandlerReanimatedTouchEvent,
]
);

useEffect(() => {
const nativeTag = findNodeHandle(viewRef.current) ?? -1;
// @ts-expect-error Reanimated expects __nativeTag to be present on the ref
viewRef.__nativeTag = nativeTag;
// @ts-expect-error NativeEventsManager should be defined here, if it isn't, we should
// go the fallback way and use Reanimated's createAnimatedComponent
eventManager.current = new NativeEventsManagerImpl({
props: reaProps,
_componentRef: viewRef,
_componentViewTag: nativeTag,
getComponentViewTag: () => nativeTag,
});
eventManager.current.attachEvents();

return () => {
eventManager.current?.detachEvents();
};
}, []);

useEffect(() => {
if (prevProps.current) {
eventManager.current?.updateEvents(prevProps.current);
}
prevProps.current = reaProps;
}, [reaProps]);

return <HostGestureDetector ref={viewRef} {...restProps} />;
}

export const ReanimatedNativeDetector = NativeEventsManagerImpl
? LeanReanimatedNativeDetector
: Reanimated?.default.createAnimatedComponent(HostGestureDetector);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Reanimated } from '../../handlers/gestures/reanimatedWrapper';
import HostGestureDetector from './HostGestureDetector';

export const ReanimatedNativeDetector =
Reanimated?.default.createAnimatedComponent(HostGestureDetector);
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
AnimatedNativeDetector,
InterceptingGestureDetectorProps,
nativeDetectorStyles,
ReanimatedNativeDetector,
} from '../common';
import { tagMessage } from '../../../utils';
import { useEnsureGestureHandlerRootView } from '../useEnsureGestureHandlerRootView';
import { ReanimatedNativeDetector } from '../ReanimatedNativeDetector';

interface VirtualChildrenForNative {
viewTag: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import { Gesture } from '../types';
import { Reanimated } from '../../handlers/gestures/reanimatedWrapper';
import { Animated, StyleSheet } from 'react-native';
import HostGestureDetector from './HostGestureDetector';
import { GestureDetectorProps as LegacyDetectorProps } from '../../handlers/gestures/GestureDetector';
Expand All @@ -23,9 +22,6 @@ export type GestureDetectorProps<THandlerData, TConfig> =
export const AnimatedNativeDetector =
Animated.createAnimatedComponent(HostGestureDetector);

export const ReanimatedNativeDetector =
Reanimated?.default.createAnimatedComponent(HostGestureDetector);

export const nativeDetectorStyles = StyleSheet.create({
detector: {
display: 'contents',
Expand Down
Loading