UNPKG

react-native-reanimated

Version:

More powerful alternative to Animated library for React Native.

136 lines (109 loc) 4.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dispatchCommand = dispatchCommand; exports.getTag = getTag; exports.scrollTo = exports.measure = void 0; exports.setGestureState = setGestureState; var _reactNative = require("react-native"); var _PlatformChecker = require("./PlatformChecker"); /* global _WORKLET _measure _scrollTo _dispatchCommand _setGestureState */ function getTag(view) { return (0, _reactNative.findNodeHandle)(view); } const isNative = !(0, _PlatformChecker.shouldBeUseWeb)(); let measure; exports.measure = measure; if ((0, _PlatformChecker.isWeb)()) { exports.measure = measure = animatedRef => { const element = animatedRef(); // TODO: fix typing of animated refs on web const viewportOffset = element.getBoundingClientRect(); return { width: element.offsetWidth, height: element.offsetHeight, x: element.offsetLeft, y: element.offsetTop, pageX: viewportOffset.left, pageY: viewportOffset.top }; }; } else if ((0, _PlatformChecker.isChromeDebugger)()) { exports.measure = measure = _animatedRef => { console.warn('[Reanimated] measure() cannot be used with Chrome Debugger.'); return null; }; } else { exports.measure = measure = animatedRef => { 'worklet'; if (!_WORKLET) { console.warn('[Reanimated] measure() was called from the main JS context. Measure is ' + 'only available in the UI runtime. This may also happen if measure() ' + 'was called by a worklet in the useAnimatedStyle hook, because useAnimatedStyle ' + 'calls the given worklet on the JS runtime during render. If you want to ' + 'prevent this warning then wrap the call with `if (_WORKLET)`. Then it will ' + 'only be called on the UI runtime after the render has been completed.'); return null; } const viewTag = animatedRef(); if (viewTag === -1) { console.warn(`[Reanimated] The view with tag ${viewTag} is not a valid argument for measure(). This may be because the view is not currently rendered, which may not be a bug (e.g. an off-screen FlatList item).`); return null; } const measured = _measure(viewTag); if (measured === null) { console.warn(`[Reanimated] The view with tag ${viewTag} has some undefined, not-yet-computed or meaningless value of \`LayoutMetrics\` type. This may be because the view is not currently rendered, which may not be a bug (e.g. an off-screen FlatList item).`); return null; } else if (measured.x === -1234567) { console.warn(`[Reanimated] The view with tag ${viewTag} returned an invalid measurement response.`); return null; } else if (isNaN(measured.x)) { console.warn(`[Reanimated] The view with tag ${viewTag} gets view-flattened on Android. To disable view-flattening, set \`collapsable={false}\` on this component.`); return null; } else { return measured; } }; } function dispatchCommand(animatedRef, commandName, args) { 'worklet'; if (!_WORKLET || !isNative) { return; } const shadowNodeWrapper = animatedRef(); _dispatchCommand(shadowNodeWrapper, commandName, args); } let scrollTo; exports.scrollTo = scrollTo; if ((0, _PlatformChecker.isWeb)()) { exports.scrollTo = scrollTo = (animatedRef, x, y, animated) => { 'worklet'; const element = animatedRef(); // @ts-ignore same call as in react-native-web element.scrollTo({ x, y, animated }); }; } else if (isNative && global._IS_FABRIC) { exports.scrollTo = scrollTo = (animatedRef, x, y, animated) => { 'worklet'; dispatchCommand(animatedRef, 'scrollTo', [x, y, animated]); }; } else if (isNative) { exports.scrollTo = scrollTo = (animatedRef, x, y, animated) => { 'worklet'; if (!_WORKLET) { return; } const viewTag = animatedRef(); _scrollTo(viewTag, x, y, animated); }; } else { exports.scrollTo = scrollTo = (_animatedRef, _x, _y) => {// no-op }; } function setGestureState(handlerTag, newState) { 'worklet'; if (!_WORKLET || !isNative) { console.warn('[Reanimated] You can not use setGestureState in non-worklet function.'); return; } _setGestureState(handlerTag, newState); } //# sourceMappingURL=NativeMethods.js.map