react-native-gesture-handler
Version:
Declarative API exposing native platform touch and gesture system to React Native
71 lines (68 loc) • 2.61 kB
JavaScript
"use strict";
import React from 'react';
import { assignRef, isHostInstance, preferHostInstance } from '../../../hostInstance';
import { tagMessage } from '../../../utils';
import { Reanimated } from '../reanimatedWrapper';
export class Wrap extends React.Component {
childInstance = null;
hostInstance = null;
childRef = undefined;
attachedChildRef = undefined;
childRefCleanup = undefined;
// eslint-disable-next-line @eslint-react/no-unused-class-component-members
getHostInstance() {
return this.hostInstance;
}
detachChildRef() {
if (this.childRefCleanup !== undefined) {
this.childRefCleanup();
} else if (this.attachedChildRef) {
assignRef(this.attachedChildRef, null);
}
this.childRefCleanup = undefined;
this.attachedChildRef = undefined;
}
attachChildRef(instance) {
this.attachedChildRef = this.childRef;
this.childRefCleanup = assignRef(this.attachedChildRef, instance);
}
handleChildRef = instance => {
this.childInstance = instance;
const resolved = preferHostInstance(instance);
this.hostInstance = isHostInstance(resolved) ? resolved : null;
this.detachChildRef();
if (instance !== null && instance !== undefined) {
this.attachChildRef(instance);
}
};
componentDidUpdate() {
if (this.childRef === this.attachedChildRef || this.childInstance === null) {
return;
}
this.detachChildRef();
this.attachChildRef(this.childInstance);
}
render() {
// I don't think that fighting with types over such a simple function is worth it
// The only thing it does is add 'collapsable: false' to the child component
// to make sure it is in the native view hierarchy so the detector can find
// correct viewTag to attach to.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let child;
try {
child = React.Children.only(this.props.children);
} catch (e) {
throw new Error(tagMessage(`GestureDetector got more than one view as a child. If you want the gesture to work on multiple views, wrap them with a common parent and attach the gesture to that view.`));
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
this.childRef = child.props.ref;
return /*#__PURE__*/React.cloneElement(child, {
collapsable: false,
ref: this.handleChildRef
},
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
child.props.children);
}
}
export const AnimatedWrap = Reanimated?.default?.createAnimatedComponent(Wrap) ?? Wrap;
//# sourceMappingURL=Wrap.js.map