@jeongshin/react-native-animateable-text
v0.18.7
Published
A fork of React Native's `<Text/> component that supports Reanimated Shared Values as text!
Maintainers
Readme
@jeongshin/react-native-animateable-text
A fork of react-native-animateable-text with Android fixes and improvements.
This package is based on the excellent work by Jonny Burger and Axelra. The original library provides a React Native <Text/> component that supports Reanimated Shared Values as text.
Changes from upstream
- Fixed
allowFontScalingnot working on Android - Fixed multiline text clipping on Android
- Fixed
lineHeightnot matching React Native's<Text/>on Android (usesLineHeightSpanlike RN) - Added support for numeric
fontWeightvalues (500,600, etc.) on Android - Fixed
fontWeightcrash when passed as a number from JS - Fixed
includeFontPaddingand gravity alignment on Android - Fixed podspec
source_filesto only include Fabric component view files
Compatibility
(🚨 Make sure you use the correct version for your RN project)
Installation
First make sure you have reanimated already installed and linked from here then run
yarn add @jeongshin/react-native-animateable-textthen for Expo managed projects you need to prebuild your project, and for ReactNative bare projects you should run
npx pod-installSupported Android Props
The following style props are supported on Android (Old Architecture):
| Prop | Type | Default | Description |
|------|------|---------|-------------|
| text | string | "" | The text content to display |
| color | Color | theme default | Text color |
| fontSize | number | 14 | Font size in dp |
| fontFamily | string | system default | Font family name |
| fontWeight | string \| number | "normal" | Font weight ("normal", "bold", 100-900) |
| fontStyle | string | "normal" | "normal" or "italic" |
| textAlign | string | "start" | "left", "right", "center", "justify" |
| numberOfLines | number | 0 (unlimited) | Maximum number of lines |
| lineHeight | number | auto | Line height in dp |
| letterSpacing | number | 0 | Letter spacing in dp |
| textDecorationLine | string | none | "underline", "line-through", or both |
| allowFontScaling | boolean | true | Whether to respect system font size settings |
Usage (Reanimated 2/3)
Note about Reanimated 2: The library does not work with Alpha 9 until RC1. Make sure to update to RC2 or later!
Use it the same as a <Text/> component, except instead of passing the text as a child node, pass it using the text props.
import AnimateableText from '@jeongshin/react-native-animateable-text';
const Example: React.FC = () => {
const reanimatedText = useSharedValue('World');
const animatedProps = useAnimatedProps(() => {
return {
text: reanimatedText.value,
};
});
return (
<AnimateableText
animatedProps={animatedProps}
// all other <Text /> props are also available
/>;
};OMG, why would you build this?
We want to animate numbers based on gestures as fast as possible, for example for charts displaying financial data. Updating native state is too slow and not feasible for a smooth experience. Using createAnimatedComponent doesn't allow you to animate the text since the children of Text are separate nodes rather than just props.
The best way so far has been to use the <ReText> component from react-native-redash, which allows you to render a string from a Reanimated Text node. However, under the hood, it uses a <TextInput/> and animates it's value prop.
This naturally comes with a few edge cases, for example:
Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
Credits
Written by Jonny Burger for our needs at Axelra.
Thanks to Axelra for sponsoring my time to turn this into an open source project!
License
MIT
