UNPKG

react-native-reanimated

Version:

More powerful alternative to Animated library for React Native.

48 lines (47 loc) 1.84 kB
import { useRef } from 'react'; import { useSharedValue } from './useSharedValue'; import { getShadowNodeWrapperFromRef } from '../fabricUtils'; import { makeShareableCloneRecursive, registerShareableMapping } from '../shareables'; import { Platform, findNodeHandle } from 'react-native'; function getComponentOrScrollable(component) { if (global._IS_FABRIC && component.getNativeScrollRef) { return component.getNativeScrollRef(); } else if (!global._IS_FABRIC && component.getScrollableNode) { return component.getScrollableNode(); } return component; } const getTagValueFunction = global._IS_FABRIC ? getShadowNodeWrapperFromRef : findNodeHandle; export function useAnimatedRef() { const tag = useSharedValue(-1); const viewName = useSharedValue(null); const ref = useRef(); if (!ref.current) { const fun = component => { // enters when ref is set by attaching to a component if (component) { tag.value = getTagValueFunction(getComponentOrScrollable(component)); fun.current = component; // viewName is required only on iOS with Paper if (Platform.OS === 'ios' && !global._IS_FABRIC) { var _component$viewConfig; viewName.value = (component === null || component === void 0 ? void 0 : (_component$viewConfig = component.viewConfig) === null || _component$viewConfig === void 0 ? void 0 : _component$viewConfig.uiViewClassName) || 'RCTView'; } } return tag.value; }; fun.current = null; const remoteRef = makeShareableCloneRecursive({ __init: () => { 'worklet'; const f = () => tag.value; f.viewName = viewName; return f; } }); registerShareableMapping(fun, remoteRef); ref.current = fun; } return ref.current; } //# sourceMappingURL=useAnimatedRef.js.map