react-native-gesture-handler
Version:
Declarative API exposing native platform touch and gesture system to React Native
59 lines (56 loc) • 2.27 kB
JavaScript
;
import React, { useCallback } from 'react';
import { assignRef, preferHostInstance } from '../../../hostInstance';
import { tagMessage } from '../../../utils';
export const Wrap = ({
ref,
children,
onStartShouldSetResponderCapture
}) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let child;
try {
child = React.Children.only(children);
} catch (e) {
throw new Error(tagMessage(`VirtualGestureDetector expects exactly one React element as its child. To use a gesture with multiple views, wrap them in a single parent view and attach the gesture to that.`));
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const childRef = child.props.ref;
const attachRef = useCallback(instance => {
const childCleanup = assignRef(childRef, instance);
const forwardedCleanup = assignRef(ref, preferHostInstance(instance));
const hasCleanup = typeof childCleanup === 'function' || typeof forwardedCleanup === 'function';
if (!hasCleanup) {
return undefined;
}
return () => {
if (typeof childCleanup === 'function') {
childCleanup();
} else {
assignRef(childRef, null);
}
if (typeof forwardedCleanup === 'function') {
forwardedCleanup();
} else {
assignRef(ref, null);
}
};
}, [childRef, ref]);
const childOnStartShouldSetResponderCapture =
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
child.props.onStartShouldSetResponderCapture;
// Marks in the capture phase: RN Text replaces its own press handling with
// any passed `onStartShouldSetResponder`, which would kill its `onPress`.
const handleStartShouldSetResponderCapture = useCallback(event => {
onStartShouldSetResponderCapture?.(event);
return childOnStartShouldSetResponderCapture?.(event) ?? false;
}, [onStartShouldSetResponderCapture, childOnStartShouldSetResponderCapture]);
return /*#__PURE__*/React.cloneElement(child, {
collapsable: false,
ref: attachRef,
onStartShouldSetResponderCapture: handleStartShouldSetResponderCapture
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
child.props.children);
};
//# sourceMappingURL=Wrap.js.map