react-native-animatable
Standard set of easy to use animations and declarative transitions for React Native
Top Related Projects
React Native's Animated library reimplemented
Experimental implementation of high performance interactable views in React Native
Declarative API exposing platform native touch and gesture system to React Native.
Lottie wrapper for React Native.
Fluid Transitions for React Navigation
Render After Effects animations natively on Android and iOS, Web, and React Native
Quick Overview
React Native Animatable is a declarative animation library for React Native. It provides a set of pre-defined animations and allows developers to create custom animations with ease. The library aims to simplify the process of adding animations to React Native applications.
Pros
- Easy to use with pre-defined animations
- Supports custom animations and interpolations
- Compatible with both iOS and Android platforms
- Lightweight and performant
Cons
- Limited to React Native applications
- Some complex animations may require additional libraries
- Documentation could be more comprehensive
- Occasional issues with TypeScript definitions
Code Examples
- Basic animation:
import * as Animatable from 'react-native-animatable';
<Animatable.View animation="fadeIn" duration={1000}>
<Text>Fading in</Text>
</Animatable.View>
This example shows a view fading in over 1 second.
- Custom animation:
import * as Animatable from 'react-native-animatable';
Animatable.initializeRegistryWithDefinitions({
myCustomAnimation: {
from: { opacity: 0, scale: 0.5 },
to: { opacity: 1, scale: 1 },
},
});
<Animatable.Text animation="myCustomAnimation" duration={1500}>
Custom animation
</Animatable.Text>
This example demonstrates how to create and use a custom animation.
- Chaining animations:
import * as Animatable from 'react-native-animatable';
<Animatable.View
animation="bounceIn"
duration={1000}
onAnimationEnd={() => {
this.view.tada(800);
}}
ref={(ref) => (this.view = ref)}
>
<Text>Chained animations</Text>
</Animatable.View>
This example shows how to chain multiple animations together.
Getting Started
-
Install the library:
npm install react-native-animatable
-
Import and use in your React Native component:
import * as Animatable from 'react-native-animatable'; function MyComponent() { return ( <Animatable.View animation="fadeIn"> <Text>Hello, Animations!</Text> </Animatable.View> ); }
-
Customize animations as needed using props like
duration
,delay
, anditerationCount
.
Competitor Comparisons
React Native's Animated library reimplemented
Pros of react-native-reanimated
- More powerful and flexible animation system
- Better performance for complex animations
- Supports gesture-based animations
Cons of react-native-reanimated
- Steeper learning curve
- More complex setup and configuration
- Larger bundle size
Code Comparison
react-native-animatable:
import * as Animatable from 'react-native-animatable';
<Animatable.View animation="fadeIn" duration={1000}>
<Text>Fading in</Text>
</Animatable.View>
react-native-reanimated:
import Animated, { useAnimatedStyle, withTiming } from 'react-native-reanimated';
const animatedStyle = useAnimatedStyle(() => {
return { opacity: withTiming(1, { duration: 1000 }) };
});
<Animated.View style={animatedStyle}>
<Text>Fading in</Text>
</Animated.View>
react-native-animatable offers a simpler API for basic animations, making it easier to use for beginners and quick prototyping. It provides pre-defined animations that can be applied with minimal code.
react-native-reanimated, on the other hand, offers more control and flexibility for complex animations. It allows for more precise timing and interpolation, and can handle more advanced use cases like gesture-based animations.
The choice between the two libraries depends on the complexity of your animations and your project requirements. For simple animations, react-native-animatable might be sufficient, while react-native-reanimated is better suited for more advanced animation needs.
Experimental implementation of high performance interactable views in React Native
Pros of react-native-interactable
- More advanced physics-based animations and interactions
- Better performance for complex gestures and animations
- Supports real-time user interaction with animated elements
Cons of react-native-interactable
- Steeper learning curve due to more complex API
- Less actively maintained (last update was in 2021)
- Fewer pre-built animation types compared to react-native-animatable
Code Comparison
react-native-interactable:
<Interactable.View
snapPoints={[{x: 0}, {x: 200}]}
dragEnabled={true}
onSnap={this.onSnap}>
<View style={styles.box} />
</Interactable.View>
react-native-animatable:
<Animatable.View
animation="slideInRight"
iterationCount="infinite"
direction="alternate">
<View style={styles.box} />
</Animatable.View>
react-native-interactable offers more control over physics-based interactions, while react-native-animatable provides simpler, pre-defined animations. The former is better suited for complex, interactive animations, while the latter is ideal for quick, easy-to-implement animations without the need for intricate user interactions.
Declarative API exposing platform native touch and gesture system to React Native.
Pros of react-native-gesture-handler
- More comprehensive gesture handling capabilities, including complex gestures and multi-touch interactions
- Better performance due to native implementation of gesture recognition
- Seamless integration with React Native's Animated API for fluid animations
Cons of react-native-gesture-handler
- Steeper learning curve due to more complex API and concepts
- Requires additional setup and configuration compared to react-native-animatable
- May be overkill for simple animation needs
Code Comparison
react-native-gesture-handler:
import { PanGestureHandler, State } from 'react-native-gesture-handler';
<PanGestureHandler
onGestureEvent={this._onPanGestureEvent}
onHandlerStateChange={this._onPanHandlerStateChange}>
<Animated.View style={animatedStyles} />
</PanGestureHandler>
react-native-animatable:
import * as Animatable from 'react-native-animatable';
<Animatable.View animation="fadeIn" duration={1000}>
<Text>Fading in</Text>
</Animatable.View>
react-native-gesture-handler offers more control over complex gestures and interactions, while react-native-animatable provides a simpler API for basic animations. The choice between the two depends on the specific requirements of your project, with react-native-gesture-handler being more suitable for advanced gesture-based interactions and react-native-animatable for quick and easy animations.
Lottie wrapper for React Native.
Pros of Lottie React Native
- Supports complex, high-quality animations created in Adobe After Effects
- Provides a seamless workflow for designers and developers
- Offers precise control over animation playback and properties
Cons of Lottie React Native
- Larger file size and potentially higher performance overhead
- Steeper learning curve for creating animations (requires Adobe After Effects)
- Limited to animations created using the Lottie format
Code Comparison
React Native Animatable:
import * as Animatable from 'react-native-animatable';
<Animatable.View animation="fadeIn" duration={1000}>
<Text>Fading in</Text>
</Animatable.View>
Lottie React Native:
import LottieView from 'lottie-react-native';
<LottieView
source={require('./animation.json')}
autoPlay
loop
/>
React Native Animatable offers a simpler API for basic animations, while Lottie React Native provides more complex, pre-designed animations with greater control over playback. The choice between the two depends on the project's specific animation requirements and the team's workflow preferences.
Fluid Transitions for React Navigation
Pros of FluidTransitions
- Focuses on seamless transitions between screens and components
- Provides a more advanced and customizable animation system
- Offers better support for complex, multi-step animations
Cons of FluidTransitions
- Steeper learning curve due to its more complex API
- Less extensive documentation compared to React Native Animatable
- Smaller community and fewer resources available
Code Comparison
React Native Animatable:
import * as Animatable from 'react-native-animatable';
<Animatable.View animation="fadeIn" duration={1000}>
<Text>Fading in</Text>
</Animatable.View>
FluidTransitions:
import { Transition, FluidNavigator } from 'react-navigation-fluid-transitions';
<Transition shared="hero">
<Image source={require('./hero.jpg')} />
</Transition>
FluidTransitions focuses on screen-to-screen transitions, while React Native Animatable is more versatile for individual component animations. FluidTransitions requires more setup but offers more control over complex transitions. React Native Animatable provides a simpler API for quick, predefined animations on components.
Render After Effects animations natively on Android and iOS, Web, and React Native
Pros of Lottie-Android
- Supports complex, high-quality animations created in Adobe After Effects
- Offers a wide range of customization options for animations
- Provides better performance for complex animations due to vector-based rendering
Cons of Lottie-Android
- Requires animations to be created in Adobe After Effects and exported as JSON
- Limited to animations created using supported After Effects features
- Steeper learning curve for designers and developers unfamiliar with After Effects
Code Comparison
Lottie-Android:
val animationView = findViewById<LottieAnimationView>(R.id.animation_view)
animationView.setAnimation(R.raw.animation)
animationView.playAnimation()
React Native Animatable:
import * as Animatable from 'react-native-animatable';
<Animatable.View animation="fadeIn" duration={1000}>
<Text>Fading in</Text>
</Animatable.View>
Summary
Lottie-Android excels in complex, high-quality animations created in After Effects, offering extensive customization and better performance for intricate animations. However, it requires animations to be created in After Effects and exported as JSON, which may present a steeper learning curve.
React Native Animatable, on the other hand, provides a simpler API for basic animations directly in React Native code, making it more accessible for developers without After Effects experience. It's suitable for simpler animations but may not offer the same level of complexity and performance as Lottie-Android for advanced animations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
react-native-animatable
Declarative transitions and animations for React Native
Installation
$ npm install react-native-animatable --save
Usage
To animate things you must use the createAnimatableComponent
composer similar to the Animated.createAnimatedComponent
. The common components View
, Text
and Image
are precomposed and exposed under the Animatable
namespace. If you have your own component that you wish to animate, simply wrap it with a Animatable.View
or compose it with:
import * as Animatable from 'react-native-animatable';
MyCustomComponent = Animatable.createAnimatableComponent(MyCustomComponent);
Declarative Usage
Animations
<Animatable.Text animation="zoomInUp">Zoom me up, Scotty</Animatable.Text>
Looping
To make looping animations simply set the iterationCount
to infinite
. Most animations except the attention seekers work best when setting direction
to alternate
.
<Animatable.Text animation="slideInDown" iterationCount={5} direction="alternate">Up and down you go</Animatable.Text>
<Animatable.Text animation="pulse" easing="ease-out" iterationCount="infinite" style={{ textAlign: 'center' }}>â¤ï¸</Animatable.Text>
Generic transitions
You can create your own simple transitions of a style property of your own choosing. The following example will increase the font size by 5 for every tap â all animated, all declarative! If you don't supply a duration
property, a spring animation will be used.
Note: If you are using colors, please use rgba()
syntax.
Note: Transitions require StyleSheet.flatten
available in React Native 0.15 or later. If you are running on anything lower, please polyfill as described under imperative usage.
<TouchableOpacity onPress={() => this.setState({fontSize: (this.state.fontSize || 10) + 5 })}>
<Animatable.Text transition="fontSize" style={{fontSize: this.state.fontSize || 10}}>Size me up, Scotty</Animatable.Text>
</TouchableOpacity>
Properties
Note: Other properties will be passed down to underlying component.
Prop | Description | Default |
---|---|---|
animation | Name of the animation, see below for available animations. | None |
duration | For how long the animation will run (milliseconds). | 1000 |
delay | Optionally delay animation (milliseconds). | 0 |
direction | Direction of animation, especially useful for repeating animations. Valid values: normal , reverse , alternate , alternate-reverse . | normal |
easing | Timing function for the animation. Valid values: custom function or linear , ease , ease-in , ease-out , ease-in-out , ease-in-cubic , ease-out-cubic , ease-in-out-cubic , ease-in-circ , ease-out-circ , ease-in-out-circ , ease-in-expo , ease-out-expo , ease-in-out-expo , ease-in-quad , ease-out-quad , ease-in-out-quad , ease-in-quart , ease-out-quart , ease-in-out-quart , ease-in-quint , ease-out-quint , ease-in-out-quint , ease-in-sine , ease-out-sine , ease-in-out-sine , ease-in-back , ease-out-back , ease-in-out-back . | ease |
iterationCount | How many times to run the animation, use infinite for looped animations. | 1 |
iterationDelay | For how long to pause between animation iterations (milliseconds). | 0 |
transition | What style property to transition, for example opacity , rotate or fontSize . Use array for multiple properties. | None |
onAnimationBegin | A function that is called when the animation has been started. | None |
onAnimationEnd | A function that is called when the animation has been completed successfully or cancelled. Function is called with an endState argument, refer to endState.finished to see if the animation completed or not. | None |
onTransitionBegin | A function that is called when the transition of a style has been started. The function is called with a property argument to differentiate between styles. | None |
onTransitionEnd | A function that is called when the transition of a style has been completed successfully or cancelled. The function is called with a property argument to differentiate between styles. | None |
useNativeDriver | Whether to use native or JavaScript animation driver. Native driver can help with performance but cannot handle all types of styling. | false |
isInteraction | Whether or not this animation creates an "interaction handle" on the InteractionManager. | false if iterationCount is less than or equal to one |
Imperative Usage
Animations
All animations are exposed as functions on Animatable elements, they take an optional duration
argument. They return a promise that is resolved when animation completes successfully or is cancelled.
import * as Animatable from 'react-native-animatable';
class ExampleView extends Component {
handleViewRef = ref => this.view = ref;
bounce = () => this.view.bounce(800).then(endState => console.log(endState.finished ? 'bounce finished' : 'bounce cancelled'));
render() {
return (
<TouchableWithoutFeedback onPress={this.bounce}>
<Animatable.View ref={this.handleViewRef}>
<Text>Bounce me!</Text>
</Animatable.View>
</TouchableWithoutFeedback>
);
}
}
To stop any ongoing animations, just invoke stopAnimation()
on that element.
You can also animate imperatively by using the animate()
function on the element for custom animations, for example:
this.view.animate({ 0: { opacity: 0 }, 1: { opacity: 1 } });
Generic transitions
transition(fromValues, toValues[[, duration], easing])
Will transition between given styles. If no duration
or easing
is passed a spring animation will be used.
transitionTo(toValues[[, duration], easing])
This function will try to determine the current styles and pass it along to transition()
as fromValues
.
import * as Animatable from 'react-native-animatable';
class ExampleView extends Component {
handleTextRef = ref => this.text = ref;
render() {
return (
<TouchableWithoutFeedback onPress={() => this.text.transitionTo({ opacity: 0.2 })}>
<Animatable.Text ref={this.handleTextRef}>Fade me!</Animatable.Text>
</TouchableWithoutFeedback>
);
}
}
Custom Animations
Animations can be referred to by a global name or a definition object.
Animation Definition Schema
An animation definition is a plain object that contains an optional easing
property, an optional style
property for static non-animated styles (useful for perspective
, backfaceVisibility
, zIndex
etc) and a list of keyframes. The keyframes are refered to by a number between 0 to 1 or from
and to
. Inspect the source in the definitions
folder to see more in depth examples.
A simple fade in animation:
const fadeIn = {
from: {
opacity: 0,
},
to: {
opacity: 1,
},
};
<Animatable.Text animation={fadeIn} >Fade me in</Animatable.Text>
Combining multiple styles to create a zoom out animation:
const zoomOut = {
0: {
opacity: 1,
scale: 1,
},
0.5: {
opacity: 1,
scale: 0.3,
},
1: {
opacity: 0,
scale: 0,
},
};
<Animatable.Text animation={zoomOut} >Zoom me out</Animatable.Text>
To make your animations globally available by referring to them by a name, you can register them with initializeRegistryWithDefinitions
. This function can also be used to replace built in animations in case you want to tweak some value.
Animatable.initializeRegistryWithDefinitions({
myFancyAnimation: {
from: { ... },
to: { ... },
}
});
React Europe Talk
The talk A Novel Approach to Declarative Animations in React Native from React Europe 2017 about this library and animations/transitions in general is available on YouTube.
MakeItRain
example
See Examples/MakeItRain
folder for the example project from the talk.
AnimatableExplorer
example
See Examples/AnimatableExplorer
folder for an example project demoing animations available out of the box and more.
Animations
Animations are heavily inspired by Animated.css.
Attention Seekers
bounce
flash
jello
pulse
rotate
rubberBand
shake
swing
tada
wobble
Bouncing Entrances
bounceIn
bounceInDown
bounceInUp
bounceInLeft
bounceInRight
Bouncing Exits
bounceOut
bounceOutDown
bounceOutUp
bounceOutLeft
bounceOutRight
Fading Entrances
fadeIn
fadeInDown
fadeInDownBig
fadeInUp
fadeInUpBig
fadeInLeft
fadeInLeftBig
fadeInRight
fadeInRightBig
Fading Exits
fadeOut
fadeOutDown
fadeOutDownBig
fadeOutUp
fadeOutUpBig
fadeOutLeft
fadeOutLeftBig
fadeOutRight
fadeOutRightBig
Flippers
flipInX
flipInY
flipOutX
flipOutY
Lightspeed
lightSpeedIn
lightSpeedOut
Sliding Entrances
slideInDown
slideInUp
slideInLeft
slideInRight
Sliding Exits
slideOutDown
slideOutUp
slideOutLeft
slideOutRight
Zooming Entrances
zoomIn
zoomInDown
zoomInUp
zoomInLeft
zoomInRight
Zooming Exits
zoomOut
zoomOutDown
zoomOutUp
zoomOutLeft
zoomOutRight
Changelog
License
MIT License. © Joel Arvidsson 2015
Top Related Projects
React Native's Animated library reimplemented
Experimental implementation of high performance interactable views in React Native
Declarative API exposing platform native touch and gesture system to React Native.
Lottie wrapper for React Native.
Fluid Transitions for React Navigation
Render After Effects animations natively on Android and iOS, Web, and React Native
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot