UNPKG

@tamagui/react-native-web-lite

Version:
83 lines (82 loc) 3.3 kB
import AnimatedProps from "./nodes/AnimatedProps.native.js"; import { AnimatedEvent } from "./AnimatedEvent.native.js"; import useRefEffect from "../Utilities/useRefEffect.native.js"; import NativeAnimatedHelper from "./NativeAnimatedHelper.native.js"; import { useCallback, useEffect, useMemo, useReducer, useRef } from "react"; import { useLayoutEffect } from "@tamagui/react-native-web-internals"; function useAnimatedProps(props) { var [, scheduleUpdate] = useReducer(function (count) { return count + 1; }, 0), onUpdateRef = useRef(null), node = useMemo(function () { return new AnimatedProps(props, function () { var _onUpdateRef_current; return (_onUpdateRef_current = onUpdateRef.current) === null || _onUpdateRef_current === void 0 ? void 0 : _onUpdateRef_current.call(onUpdateRef); }); }, [props]); useAnimatedPropsLifecycle(node); var refEffect = useCallback(function (instance) { node.setNativeView(instance), onUpdateRef.current = function () { scheduleUpdate(); }; var target = getEventTarget(instance), events = []; for (var propName in props) { var propValue = props[propName]; propValue instanceof AnimatedEvent && propValue.__isNative && (propValue.__attach(target, propName), events.push([propName, propValue])); } return function () { onUpdateRef.current = null; var _iteratorNormalCompletion = !0, _didIteratorError = !1, _iteratorError = void 0; try { for (var _iterator = events[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = !0) { var [propName2, propValue2] = _step.value; propValue2.__detach(target, propName2); } } catch (err) { _didIteratorError = !0, _iteratorError = err; } finally { try { !_iteratorNormalCompletion && _iterator.return != null && _iterator.return(); } finally { if (_didIteratorError) throw _iteratorError; } } }; }, [props, node]), callbackRef = useRefEffect(refEffect); return [reduceAnimatedProps(node), callbackRef]; } function reduceAnimatedProps(node) { return { ...node.__getValue(), collapsable: !1 }; } function useAnimatedPropsLifecycle(node) { var prevNodeRef = useRef(null), isUnmountingRef = useRef(!1); useEffect(function () { NativeAnimatedHelper.API.flushQueue(); }), useLayoutEffect(function () { return isUnmountingRef.current = !1, function () { isUnmountingRef.current = !0; }; }, []), useLayoutEffect(function () { if (node.__attach(), prevNodeRef.current != null) { var prevNode = prevNodeRef.current; prevNode.__restoreDefaultValues(), prevNode.__detach(), prevNodeRef.current = null; } return function () { isUnmountingRef.current ? node.__detach() : prevNodeRef.current = node; }; }, [node]); } function getEventTarget(instance) { return typeof instance == "object" && typeof instance?.getScrollableNode == "function" ? instance.getScrollableNode() : instance; } export { useAnimatedProps as default }; //# sourceMappingURL=useAnimatedProps.native.js.map