React higher order component method that transforms your favorite components to animate their props on change.
npm install react-animate-propsReact HOC (higher order component) method, and React Hook for transforming
your favorite components to animate prop values on change.
This package uses Tweenkle for handling
the tweening of prop values. It’s not as full-featured as GSAP,
but it works pretty well for basic value and object tweening.
Via npm
``sh`
npm install --save react-animate-props
Via Yarn
`sh`
yarn add react-animate-props
react-animate-props now offers two(!) ways for you to animate the props in both
your class-based, and functional, React components.
useAnimateProps
#### Parameters
* prop : Number - Value to animateoptions : Object
* - Options to define the tween properties to use.
__Default options:__
`js`
{
delay: 0, // Delay to apply before the tween starts
duration: 1000, // Duration of the tween in milliseconds
ease: Easing.Quad.Out, // Ease to use for the tween, @see Tweenkle for options
onAnimateProgress: value => value, // Callback to use during the tweening process, as well as being able to manipulate the value during the tween
onAnimateComplete: value => value, // Callback for when the tween has completed, as well as being able to manipulate the final value of the tween
}
#### Example
`js
import React from 'react';
import { Easing } from 'tweenkle';
import { useAnimateProps } from 'react-animate-props';
const AnimatedNumberLabel = ({ number }) => {
const animatedNumber = useAnimateProps(number, {
ease: Easing.Quad.In,
delay: 500,
duration: 1500,
onAnimateProgress: value => {
return Math.round(value);
},
onAnimateComplete: value => {
return Math.round(value);
},
});
return {animatedNumber};
};
export default AnimatedNumberLabel;
`
animateProps is a higher order component
that allows you to easily create components who’s props animate when changed.
Whether you’re writing a new component, or would like to make an animated version
of an existing component, just export your component and pass it through, animateProps.
#### Parameters
* component:Class - Class to apply animateProps logic to.
* defaultProps:Object - Default props declared for the component being animated. (Default: {})
#### Properties
* animatedProps:Object - Object defining which props to animate, and the tweenanimateProps
settings for each. uses the TweenkleTween
tweening library, specifically a instance, and you can pass whatever props that
library supports via the tween settings. You can find out more by reading the
Tweenkle README.
* onAnimateProgress:Function - Callback available to manipulate the prop before(prop, value) => { return { [prop]: value }; }
it is applied to the state. (Example: )
* onAnimateComplete:Function - Callback fired when the animation for a prop completes.(prop, value, tweensActive) => {}
(Example: )
#### Example
`js
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import animateProps from 'react-animate-props';
import { Easing } from 'tweenkle';
class AnimatedNumberLabel extends Component {
render() {
const {
number,
} = this.props;
return (
{number}
);
}
}
AnimatedNumberLabel.propTypes = {
animatedProps: PropTypes.object,
number: PropTypes.number,
onAnimateProgress: PropTypes.func,
};
AnimatedNumberLabel.defaultProps = {
animatedProps: {
number: {
ease: Easing.Quad.In,
delay: 500,
duration: 1500,
},
},
number: 0,
onAnimateProgress: (prop, value) => {
return {
[prop]: Math.round(value),
};
},
onAnimateComplete: (prop, value, tweensActive) => {
return {
[prop]: Math.round(value),
};
},
};
export default animateProps(
AnimatedNumberLabel,
AnimatedNumberLabel.defaultProps
);
``