@tamagui/react-native-web-lite
Version:
React Native for Web
77 lines (76 loc) • 2.57 kB
JavaScript
import { AnimatedProps } from "./nodes/AnimatedProps.mjs";
import { AnimatedEvent } from "./AnimatedEvent.mjs";
import { useRefEffect } from "../Utilities/useRefEffect.mjs";
import { NativeAnimatedHelper } from "./NativeAnimatedHelper.mjs";
import { useCallback, useEffect, useMemo, useReducer, useRef } from "react";
import { useLayoutEffect } from "@tamagui/react-native-web-internals";
function useAnimatedProps(props) {
const [, scheduleUpdate] = useReducer(count => count + 1, 0);
const onUpdateRef = useRef(null);
const node = useMemo(() => new AnimatedProps(props, () => onUpdateRef.current?.()), [props]);
useAnimatedPropsLifecycle(node);
const refEffect = useCallback(instance => {
node.setNativeView(instance);
onUpdateRef.current = () => {
scheduleUpdate();
};
const target = getEventTarget(instance);
const events = [];
for (const propName in props) {
const propValue = props[propName];
if (propValue instanceof AnimatedEvent && propValue.__isNative) {
propValue.__attach(target, propName);
events.push([propName, propValue]);
}
}
return () => {
onUpdateRef.current = null;
for (const [propName, propValue] of events) {
propValue.__detach(target, propName);
}
};
}, [props, node]);
const callbackRef = useRefEffect(refEffect);
return [reduceAnimatedProps(node), callbackRef];
}
function reduceAnimatedProps(node) {
return {
...node.__getValue(),
collapsable: false
};
}
function useAnimatedPropsLifecycle(node) {
const prevNodeRef = useRef(null);
const isUnmountingRef = useRef(false);
useEffect(() => {
NativeAnimatedHelper.API.flushQueue();
});
useLayoutEffect(() => {
isUnmountingRef.current = false;
return () => {
isUnmountingRef.current = true;
};
}, []);
useLayoutEffect(() => {
node.__attach();
if (prevNodeRef.current != null) {
const prevNode = prevNodeRef.current;
prevNode.__restoreDefaultValues();
prevNode.__detach();
prevNodeRef.current = null;
}
return () => {
if (isUnmountingRef.current) {
node.__detach();
} else {
prevNodeRef.current = node;
}
};
}, [node]);
}
function getEventTarget(instance) {
return typeof instance === "object" && typeof instance?.getScrollableNode === "function" ? instance.getScrollableNode() : instance;
}
var useAnimatedProps_default = useAnimatedProps;
export { useAnimatedProps_default as default, useAnimatedProps };
//# sourceMappingURL=useAnimatedProps.mjs.map