Newts Parading Majestically
    Wondering what’s next for npm?Check out our public roadmap! »

    react-animate-props

    0.2.4 • Public • Published

    react-animate-props

    React 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.

    Install

    Via npm

    npm install --save react-animate-props

    Via Yarn

    yarn add react-animate-props

    How to use

    react-animate-props now offers two(!) ways for you to animate the props in both your class-based, and functional, React components.

    Hook

    useAnimateProps

    Parameters

    • prop : Number - Value to animate
    • options : Object - Options to define the tween properties to use.

    Default options:

    {
      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](https://github.com/ryanhefner/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

    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 <span>{animatedNumber}</span>;
    };
     
    export default AnimatedNumberLabel;

    HOC (Higher Order Component)

    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 tween settings for each. animateProps uses the Tweenkle tweening library, specifically a Tween 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 it is applied to the state. (Example: (prop, value) => { return { [prop]: value }; })

    • onAnimateComplete:Function - Callback fired when the animation for a prop completes. (Example: (prop, value, tweensActive) => {})

    Example

    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 (
          <span>
            {number}
          </span>
        );
      }
    }
     
    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
    );

    License

    MIT © Ryan Hefner

    Install

    npm i react-animate-props

    DownloadsWeekly Downloads

    20

    Version

    0.2.4

    License

    MIT

    Unpacked Size

    66.2 kB

    Total Files

    18

    Last publish

    Collaborators

    • avatar