react-native-reanimated
Version:
More powerful alternative to Animated library for React Native.
68 lines (63 loc) • 2.32 kB
JavaScript
;
import { createElement, forwardRef } from 'react';
import { Text } from 'react-native';
import { logger } from "../common/index.js";
import { createAnimatedComponent } from "../createAnimatedComponent/index.js";
import { getStaticFeatureFlag } from "../featureFlags/index.js";
import { isSharedValue } from "../isSharedValue.js";
// Since createAnimatedComponent return type is ComponentClass that has the props of the argument,
// but not things like NativeMethods, etc. we need to add them manually by extending the type.
// is-tree-shakable-suppress
const AnimatedTextBase = createAnimatedComponent(Text);
const sharedValueIds = new WeakMap();
let nextSharedValueId = 0;
function getSharedValueId(sharedValue) {
let id = sharedValueIds.get(sharedValue);
if (id === undefined) {
id = nextSharedValueId++;
sharedValueIds.set(sharedValue, id);
}
return id;
}
// is-tree-shakable-suppress
const AnimatedTextWithRef = /*#__PURE__*/forwardRef(({
children,
...props
}, ref) => {
const content = (Array.isArray(children) ? children : [children]).map((child, index) => {
if (!isSharedValue(child)) {
return child;
}
if (__DEV__ && getStaticFeatureFlag('USE_ANIMATION_BACKEND')) {
logger.warnOnce('Animated.Text does not update shared value children when the `USE_ANIMATION_BACKEND` static feature flag is enabled.', 0);
}
return /*#__PURE__*/createElement(AnimatedTextBase, {
key: `${getSharedValueId(child)}:${index}`
}, child);
});
return /*#__PURE__*/createElement(AnimatedTextBase, {
...props,
ref
}, ...content);
});
AnimatedTextWithRef.displayName = 'AnimatedText';
/**
* Animated version of the React Native `Text`. Its children can be shared
* values, and the text follows them on the UI thread without re-rendering.
*
* Pass the shared value itself as a child. Don't read its `value` during
* render: it is a side effect that blocks the JS thread until the value is
* fetched from the UI thread, and the text becomes a static snapshot that never
* updates.
*
* ```tsx
* // Antipattern
* <Animated.Text>{sv.value}</Animated.Text>;
*
* // Correct
* <Animated.Text>{sv}</Animated.Text>;
* ```
*/
// is-tree-shakable-suppress
export const AnimatedText = AnimatedTextWithRef;
//# sourceMappingURL=Text.js.map