# react-motion-flip-animated

> A simple library for animating positional transitions

Latest version **0.0.4** (published 2017-01-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-motion-flip-animated
pnpm add react-motion-flip-animated
yarn add react-motion-flip-animated
bun add react-motion-flip-animated
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2017-01-28 |
| First published | 2017-01-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Marc Porciuncula |
| Maintainers | marcotheporo |

## Links

- npm: https://www.npmjs.com/package/react-motion-flip-animated
- Repository: https://github.com/MarcoThePoro/react-motion-flip-animated
- Homepage: https://github.com/MarcoThePoro/react-motion-flip-animated#readme
- Issues: https://github.com/MarcoThePoro/react-motion-flip-animated/issues
- npm.io page: https://npm.io/package/react-motion-flip-animated

## Dependencies (1)

- [invariant](https://npm.io/package/invariant.md) ^2.2.2

## Recent versions

- 0.0.4 (latest) — 2017-01-28
- 0.0.3 — 2017-01-13
- 0.0.2 — 2017-01-13
- 0.0.1 — 2017-01-13

## README

# react-motion-flip-animated
Render here, render there, watch it animate, move on.

React Motion Flip Animated is a one component library that makes it dead simple to animate anything from here to there.

It makes use of the FLIP animation technique in combination with [react-motion](https://github.com/chenglou/react-motion) to automagically animate anything you throw at it, no measuring pixels required.

[![npm version](https://badge.fury.io/js/react-motion-flip-animated.svg)](https://badge.fury.io/js/react-motion-flip-animated)

```js
import { Animated } from 'react-motion-flip-animated';

// render this...
<div>
  <div className="here">
    <Animated id="animate-me">
      <MyCoolComponent />
    </Animated>
  </div>
  <div className="there">
    null
  </div>
</div>

// then render this...
<div>
  <div className="here">
    null
  </div>
  <div className="there">
    <Animated id="animate-me">
      <MyCoolComponent />
    </Animated>
  </div>
</div>
```

**gif here**

## Install
```
npm install --save react-motion react-motion-flip-animated
```

**Yes** there is a peer dependency on `react-motion` (and obviously `react`);

If you're allergic to module bundling:
```js
// unpkg build coming soon...
```

## Use it


`react-motion-flip-animated` exports a single component `Animated`.

### Make something move from here to there
- Wrap it inside an `Animated` with a unique id.
```js
<Animated id="animated-thingy">
  <MyCoolComponent />
</Animated>
```
- Render it somewhere "before"
```js
<div className="somewhere">
  <Animated id="animated-thingy">
    <MyCoolComponent />
  </Animated>
</div>
```
- Cause it to render elsewhere "after" (due to a state/props/context change).
```js
<div className="somewhere">
  null
</div>
<div className="elsewhere">
  <Animated id="animated-thingy">
    <MyCoolComponent />
  </Animated>
</div>
```
 ***Warning***: Your animated must remain mounted between your before and after renders. If the animated unmounts (is not rendered) for just a single update, it loses memory of its last position and assumes it is new.

### You can also make things swap positions:

- Render the before.
```js
<div className="somewhere">
  <Animated id="animated-thingy-1">
    <Component1 />
  </Animated>
</div>
<div className="elsewhere">
  <Animated id="animated-thingy-2">
    <Component2 />
  </Animated>
</div>
```
- Render the after (with your animateds swapped).
```js
<div className="somewhere">
  <Animated id="animated-thingy-2">
    <Component2 />
  </Animated>
</div>
<div className="elsewhere">
  <Animated id="animated-thingy-1">
    <Component1 />
  </Animated>
</div>
```

### And even reorder entire collections:

- Render (also supplying the `index` prop).
```js
<div>
  {this.state.items.map(({ id, ...etc }, i) => (
    <Animated id={id} key={id} index={i}>
      <Component {...etc} />
    </Animated>
  ))}
</div>
```
- Reorder the items.
- Render again.

**Remember**: the `key` prop goes on the root node of any element in an array, however, the `index` prop is for `<Animated>` only. For example:
```js
<div>
  {this.state.items.map(({ id, ...etc }, i) => (
    <div className="wrap-each-item-but-don't-animate-this-element" key={id}>
      <Animated id={id} index={i}>
        <Component {...etc} /> // I'll still be animated
      </Animated>
    </div>
  ))}
</div>
```

### API

#### Props
- `id`: string (required)

  An id for tracking this specific animated component. It must be unique from all other ids of `<Animated>`s currently being rendered. If you happen to use two of the same id, there will be no warning (currently), and the animation won't occur.

- `index`: number (optional)

  The index of this item, if this item was rendered in a collection. You **must** supply this prop when rendering collections of `<Animated>`s that you wish to reanimate when reordered. (If you for some reason don't want that, don't supply the prop).

- `children`: ReactElement | ManualRenderFunction (required)

  What will be rendered.

  - If ReactElement: the element will be wrapped in a `<div>`, and that div will receive ```style={{ transform: `translate(${translateX}px, ${translateY}px)` }}``` to perform the animation. If you want to avoid the extra div and apply the transformation directly to your component, use a ManualRenderFunction instead.

  - If ManualRenderFunction: this function will be called with the signature `(getRef: (node) => void, AnimationData { translateX: number, translateY: number, progress: number, isAnimating: boolean }) => ReactElement`. This will be called every frame with updated (tweened) animation data. You **must** supply `getRef` to the root element of your component as a ref callback so that its position can be measured, and you **must** apply the transformation yourself. `progress` is a number from 0 to (over) 100 (it may go over 100 if the animation overshoots the target. ie. `animation === 100` does not mean the animation has finished).

- `spring`: SpringConfig (optional)

  This is a react-motion [spring config](https://github.com/chenglou/react-motion#--spring-val-number-config-springhelperconfig--opaqueconfig), which has the shape `{ stiffness: number (optional), damping: number (optional), precision: number (optional) }`, you can use this to configure the physics of the animation.

- `transitionBegan`: function (optional)

  A callback which is triggered when the transition animation has begun.

- `transitionFinished`: function (optional)

  A callback which is triggered when the transition animation has come to a full stop.

## Run the demos
```bash
git clone https://github.com/MarcoThePoro/react-motion-flip-animated.git
cd react-motion-flip-animated
npm install
npm run storybook
```
Then navigate to [http://localhost:9001](http://localhost:9001)

## Test it
404 tests not found

## FAQ

- I'm trying to swap two `<Animated>`s but they're jumping to their new position without animating.

  Check if you're accidentally giving them the same `id`.

- I'm trying to reorder a collection of `<Animated>`s but they're jumping to their new position without animating.

  Ensure you're supplying correct values for `key` (trackable across renders) and `index` (position in collection).

- Help! Now that all the animation work is done by this library, I no longer have a job!

  Oh no!

---
_Source: https://npm.io/package/react-motion-flip-animated · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
