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-easy-flip

v4.0.3

Published

A lightweight React library for smooth FLIP animations

Downloads

590

Readme

npm bundle size npm (tag)

React Easy Flip

⚛ A lightweight React library for smooth FLIP animations

Features

  • Animates the unanimatable (DOM positions, mounts/unmounts)

  • One hook for many usecases

  • Uses the Web Animations API (WAAPI)

  • Stable and smooth 60fps animations
  • SSR-friendly
  • Built-in easing functions
  • Lightweight

Previous README versions

This is a README for v4. The v3 README can be found here.

Demo

https://react-easy-flip-demo.now.sh/

Repository: react-easy-flip-demo

You can also read about how it works in detail here.

Install

npm install react-easy-flip@beta

Get started

  1. Import useFlip hook and FlipProvider:
import { useFlip, FlipProvider } from 'react-easy-flip'
  1. Wrap your app (or at least a component that contains animated children) with a FlipProvider
<FlipProvider>
  <MyApp />
</FlipProvider>
  1. Assign a data-flip-root-id to any parent of the element(s) you want to animate
<div data-flip-root-id="flip-root">
  <AnimatedChildren>
</div>
  1. Pick a unique data-flip-id and assign it to the element(s) you want to animate. It can be the same as a key prop
<img data-flip-id="animated-image" />
  1. Use the hook by passing it the root ID you picked in (3)
useFlip(rootId)

And that's it!

Usage details

useFlip

useFlip requires one argument, which is an ID of the root, i.e. any parent whose children you want to animate. You can optionally pass an options object with animation options (see details below) as a second argument. Third argument is the optional dependencies which you would normally pass to a useEffect hook: use it if you need to explicitly tell react-easy-flip that items you want to animate changed.

useFlip(rootId, animationOptions, deps)

Animation optons

Animation options is an object.

| Property | Default | Required | Type | Details | | :------------: | :------------: | :------: | :--------: | :-----------------------------------------------------------: | | duration | 400 | false | number | Animation duration (ms) | | easing | easeOutCubic | false | function | Easing function (that can be imported from react-easy-flip) | | delay | 0 | false | number | Animation delay | | animateColor | false | false | boolean | Animate background color of the animated element |

Example:

import { easeInOutQuint } from 'react-easy-flip`

const SomeReactComponent = () => {
  const animationOptions = {
    duration: 2000,
    easing: easeInOutQuint,
  }

  useFlip(rootId, animationOptions)

  return (
    <div data-flip-root-id="root">
      <div data-flip-id="flipped" />
    </div>
  )
}

Exported easings

react-easy-flip exports ready-to-use easing functions. You can see the examples here.

  • linear
  • easeInSine
  • easeOutSine
  • easeInOutSine
  • easeInCubic
  • easeOutCubic
  • easeInOutCubic
  • easeInQuint
  • easeOutQuint
  • easeInOutQuint
  • easeInBack
  • easeOutBack
  • easeInOutBack

AnimateInOut

While useFlip can animate all kinds of position changes, it does not animate mount/unmount animations (e.g. fade in/out). For this purpose the <AnimateInOut /> component is also exported. To use it, simple wrap with it the components/elements which you want to be animated. By default the initial render is not animated, but this can be changed with a prop.

Every element wrapped with a <AnimateInOut /> must have a unique key prop.

Example:

import { AnimateInOut } from 'react-easy-flip`

const SomeReactComponent = () => {
  return (
      <AnimateInOut>
        <div key="flipped-1" />
        <div key="flipped-2" />
        <div key="flipped-3" />
      </AnimateInOut>
  )
}

Here are all props that you can pass to <AnimateInOut />:

| Property | Default | Required | Type | Details | | :-----------------: | :---------: | :------: | :------------------: | :------------------------------------------------------------: | | in | fadeIn | false | AnimationKeyframes | Mount animation options | | out | fadeOut | false | AnimationKeyframes | Unmount animation options | | playOnFirstRender | false | false | boolean | Animate on first render | | itemAmount | undefined | false | number | An explicit amount of current children (see explanation below) |

What is itemAmount for? In most cases this is not needed. But if your element is animated with a shared layout transition (such as moving from one list to another), this means that it doesn't need an unmount animation. In order to avoid two animations being applied to one element, provide the amount. For example, if this is a todo-app-like application, keep the number of both todo and done items. Moving from todo to done doesn't change the total amount of items, but <AnimateInOut /> does not know that until you tell it. See the recipes below.

Comparison with other libraries

  • react-easy-flip uses Web Animations API (WAAPI) for animations. No other library based on a FLIP technique currently does that.

  • Similar to existing libraries such as react-overdrive, react-flip-move or react-flip-toolkit (although only the latter seems to be maintained).

  • Allows you to easily do so-called shared layout animations (e.g. smoothly move an element from one page/parent to another). Some examples are given below. This is what heavier libraries like framer-motion call Magic Motion.

  • Additionally, react-easy-flip is the only lightweight FLIP library for React that provides animation via a hook. Currently react-easy-flip has the smallest bundle size. It also does not use React class components and lifecycle methods that are considered unsafe in latest releases of React.

Recipes

List sort/shuffle animation

Go to code

Both x and y coordinate shuffle

Go to code

Shared layout animation

This is an todo-app-like example of shared layout animations. Click on any rectangle to move it to another parent. Note that on every click an item is actually unmounted from DOM and re-mounted in the other position, but having the same data-flip-id allows to be smoothly animated from one to another position.

Go to code

Shared layout animation (navigation)

One nice usecase for shared layout animation is navigation bars where we want to move the highlighting indicator smoothly between tabs.

Go to code

In/out (mount/unmount) animation (opacity)

The fade in and out keyframes are default and work out of box (= you do not need to explicitly pass them).

Go to code

In/out (mount/unmount) animation (translation)

An example of passing custom animation options to <AnimateInOut>. Here the images are moved in and out instead of simply fading in and out.

Go to code

Requirements

This library requires React version 16.8.0 or higher (the one with Hooks).

Contribution

Any kind of contribution is welcome!

  1. Open an issue or pick an existing one that you want to work on
  2. Fork this repository
  3. Clone your fork to work on it locally
  4. Make changes
  5. Run yarn build:dev and make sure that it builds without crash