React-Text-transition
Animate your text changes
Installation
npm install react-text-transition
Using the demo
npm run dev
How to use
Example
import React, { Component } from "react";
import TextTransition from "react-text-transition";
class App extends Component {
static texts = [
"Forest",
"Building",
"Tree",
"Color"
];
state = { active : 0 };
componentDidMount() {
setInterval(() => {
this.setState({ active : this.state.active + 1 });
}, 5000);
}
render() {
return (
<h1>
<TextTransition
text={ App.texts[this.state.active % App.texts.length] }
/>
</h1>
);
}
}
Props
Prop | Type | Default | Definition |
---|---|---|---|
text | String | REQUIRED | The text you want to show. |
order | Number | 0 | Used to determine the direction of the transition. |
inline | Boolean | false | Makes the wrapper inline (will auto resize based on contents). |
delay | Number | 0 | Delay the transition of the text (in milliseconds). |
spring | Object | { stiffness : 170, damping : 26 } | React-Motion's spring configuration. |
overflow | Boolean | false | Setting this to false will make the transitioning text appear clipped (Will simply set overflow : hidden on the wrapper). |
className | String | "" | Any css classes that you might want to send to the wrapper. |
style | Object | {} | Any styles that you might want to send to the wrapper. |
Detailed Props
String
text Changing this prop triggers the transition.
Number
order Used to determine the direction from which the new text is shown.
if the order of the new text is bigger than the one before it, the transition will be bottom-to-top,
this is the default transition it'll be used if no order
prop was provided,
and the opposite happens if the order of the new text is less than the one before it, aka top-to-bottom.
Boolean
inline Will simply make the wrapper an inline element and animate its width based on currently showing text, this is useful if you want to show some other static text on the same line.
Number
delay The amount of miliseconds to wait before transitioning.
Object
spring React-Motion's Spring configuration, you can also use the Spring Parameters Chooser to help you pick your preferred spring config.
React-Motion's spring presets for {stiffness, damping}
are provided with the plugin.
import TextTransition, { presets } from "react-text-transition";
// in your render method
<TextTransition
text={ this.state.text }
spring={ presets.wobbly }
/>
There're 4 presets
noWobble
The default.gentle
wobbly
stiff
Boolean
overflow The default value false
will make the text appear clipped while the transition happens (takes less area), it will simply set overflow to hidden on the animation wrapper, set to true
if you want the text to overflow.
String
className Any css classes that you might want to provide to the wrapper.
Object
style Any css styles that you might want to provide to the wrapper.
NOTE
Feel free to ask any questions about using this plugin. This plugin requires react.