npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-animate

v3.0.0

Published

react-animate ============= Allows to animate parts of a React components programmatically, without bypassing React internals and without altering the DOM directly.

Downloads

312

Readme

react-animate

Allows to animate parts of a React components programmatically, without bypassing React internals and without altering the DOM directly.

It works by interpolating intermediate styles values and applying them to special state keys containing the styles, on each animation frame using requestAnimationFrame.

Multiples animations can be run concurrently since each animation is identified by a name. Different names target different animations.

Usage

Fully embracing ES6 classes, react-animate allows you to extend a React.Component and add new methods without collision using ES6 Symbol. Such an extended class can be created by using Animate.extend(Component). The new methods are accessible using either method call delegation or Symbol dereferencing:

this[Animate['@animate']](...);
// or equivalently
Animate.animate.call(this, ...);

The extended component gets three new methods: Animate['@animate'], Animate['@getAnimatedStyle'], and Animate['@abortAnimation'] (the latter being of limited use under normal circomstances - because Animate takes care of aborting running animations before unmounting - but can prove useful sometimes to actually interrupt a running animation).

Trigger an animation with animate, and inject the associated style in the render function using getAnimatedStyle:

Animate.extend(class MyComponent extends React.Component {
  fadeIn() {
    // syntax using call delegation
    Animate.animate.call(this,
      'my-custom-animation', // animation name
      { opacity: 0 }, // initial style
      { opacity: 1 }, // final style
      1000, // animation duration (in ms)
      { easing: 'linear' } // other options
    );
    // alternate syntax using Symbol
    this[Animate['@animate']](
      'my-custom-animation', // animation name
      { opacity: 0 }, // initial style
      { opacity: 1 }, // final style
      1000, // animation duration (in ms)
      { easing: 'linear' } // other options
    );
  }

  render() {
    return <div>
      <button onClick={this.fadeIn}>Click to fade in</button>
      <div style={Animate.getAnimatedStyle.call(this, 'my-custom-animation')}>
        This text will appear soon after the click.
      </div>
    </div>;
  }
});

This module is written in ES6/7. You will need babel to run it.

API

animate(name, initialStyle, finalStyle, duration, opts)

Start an animation. Returns this for chaining.

  • name can be any string. If you restart an animation with the same name, the previous animation with the same name will be cancelled and replaced by this one.

  • initialProperties and finalProperties are styles hashes, like the ones used by React, eg. { fontSize: '12px' } means font-size: 12px when translated to CSS by React. If a property is specified in one of the hashed by not the other, it is assumed to remain constant over the duration of the animation.

  • duration is a number of milliseconds representing the total duration of the animation.

  • options is an optional hash of optionals parameters :

    • easing: the easing of the animation timing. Can be either a string or a { type, arguments } object. In both case, it uses d3 under the hood, refer to their docs. Defaults to cubic-in-out.

    • onTick, onAbort, onComplete: optional callbacks functions that will be invoked respectively on each tick, on animation abort, or on animation complete. They will be called with (currentStyle, progress, easedProgress). Defaults to no-op.

    • disableMobileHA: flag to prevent the heuristic addition of dummy properties to attempt to force hardware acceleration on mobiles. Defaults to false.

getAnimatedStyle(name)

Get the current value of the animated style. You can call it from render() and pass it directly as the style prop of a React DOM element such as <div>. If no animation with this name exists, it will fail silently and return an empty hash ({}).

abortAnimation(name)

Abort the animation with the given name, if it exists. Returns true if an animation with this name existed. Returns false otherwise.

Note that you don't have to call this function in componentWillUnmount. react-animate will take care of that for you.