@tamagui/react-native-web-lite
Version:
React Native for Web
104 lines (103 loc) • 3.79 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var useAnimatedProps_exports = {};
__export(useAnimatedProps_exports, {
default: () => useAnimatedProps_default,
useAnimatedProps: () => useAnimatedProps
});
module.exports = __toCommonJS(useAnimatedProps_exports);
var import_AnimatedProps = require("./nodes/AnimatedProps.cjs");
var import_AnimatedEvent = require("./AnimatedEvent.cjs");
var import_useRefEffect = require("../Utilities/useRefEffect.cjs");
var import_NativeAnimatedHelper = require("./NativeAnimatedHelper.cjs");
var import_react = require("react");
var import_react_native_web_internals = require("@tamagui/react-native-web-internals");
function useAnimatedProps(props) {
const [, scheduleUpdate] = (0, import_react.useReducer)(count => count + 1, 0);
const onUpdateRef = (0, import_react.useRef)(null);
const node = (0, import_react.useMemo)(() => new import_AnimatedProps.AnimatedProps(props, () => onUpdateRef.current?.()), [props]);
useAnimatedPropsLifecycle(node);
const refEffect = (0, import_react.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 import_AnimatedEvent.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 = (0, import_useRefEffect.useRefEffect)(refEffect);
return [reduceAnimatedProps(node), callbackRef];
}
function reduceAnimatedProps(node) {
return {
...node.__getValue(),
collapsable: false
};
}
function useAnimatedPropsLifecycle(node) {
const prevNodeRef = (0, import_react.useRef)(null);
const isUnmountingRef = (0, import_react.useRef)(false);
(0, import_react.useEffect)(() => {
import_NativeAnimatedHelper.NativeAnimatedHelper.API.flushQueue();
});
(0, import_react_native_web_internals.useLayoutEffect)(() => {
isUnmountingRef.current = false;
return () => {
isUnmountingRef.current = true;
};
}, []);
(0, import_react_native_web_internals.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;