# @cher-ami/react-transition

> A zero dependency React transition component who allows to manage play-in & play-out transitions of any DOM element.

Latest version **1.0.0** (published 2021-05-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @cher-ami/react-transition
pnpm add @cher-ami/react-transition
yarn add @cher-ami/react-transition
bun add @cher-ami/react-transition
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2021-05-12 |
| First published | 2021-05-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 149.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Willy Brauner |
| Maintainers | willybe, risq |
| Keywords | react, component, transition, animation, helper |

## Links

- npm: https://www.npmjs.com/package/@cher-ami/react-transition
- Repository: https://github.com/willybrauner/react-transition
- Homepage: https://github.com/willybrauner/react-transition#readme
- Issues: https://github.com/willybrauner/react-transition/issues
- npm.io page: https://npm.io/package/@cher-ami/react-transition

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2021-05-12

## README

<h1 align="center" style="text-align:center">react-transition</h1>

<p align="center">
A zero dependency React transition component who allows to manage play-in & play-out transitions of any DOM element.
<br>
<br>
<img alt="npm" src="https://img.shields.io/npm/v/@wbe/react-transition">
<img alt="build" src="https://github.com/cher-ami/router/workflows/CI/badge.svg">
</p>

<br>
<p align="center">
<img alt="demo" src="./assets/demo.gif">
</p>
<br>

## About

React-transition responds to the simple request to have control over the play-in
and play-out transition of any component or DOM element.

React does not by default provide the ability to execute an exit transition
function before a DOM element is destroyed.
[Alexey Taktarov](https://github.com/molefrog) explains this "limitation" with
example in [this publication](https://molefrog.github.io/stateful-animations/#27).

React-transition a "Vuejs
like" [transition API](https://vuejs.org/v2/guide/transitions.html). It's
intended to partially meet the same needs of Vue transition component.

## Install

```shell
$ npm i @cher-ami/react-transition
```

## Usage

```tsx
import React, {useState} from "react"
import {Transition} from "@cher-ami/react-transition"
import {gsap} from "gsap/all"

const App = () => {
  const [isToggle, setIsToggle] = useState(true)

  // Example with GSAP
  const playInTransition = (el, done) => {
    gsap.fromTo(el, {autoAlpha: 0}, {autoAlpha: 1, onComplete: done})
  }

  const playOutTransition = (el, done) => {
    gsap.fromTo(el, {autoAlpha: 1}, {autoAlpha: 0, onComplete: done})
  }

  return (
    <div className={"app"}>
      <button onClick={() => setIsToggle(!isToggle)}>Toggle</button>
      <Transition
        if={isToggle}
        playIn={playInTransition}
        playOut={playOutTransition}
      >
        <div className={"element"} />
      </Transition>
    </div>
  )
}
```

## Props

### if

`boolean` _optional_ - default: `true`  
Toggle start play-in / play-out children with transition

### children

`ReactElement`  
React children element to transit

### playIn

`(el: HTMLElement, done: () => any) => void`  _optional_  
Play-in transition function

### onPlayInComplete

`() => void` _optional_  
Function calls when play-in transition function is completed

### playOut

`(el: HTMLElement, done: () => any) => void`  _optional_     
Play-out transition function

### onPlayOutComplete

`() => void` _optional_   
Function calls when play-out transition function is completed

### appear

`boolean` _optional_ - default: `false`    
Start transition on first mount. If false, children is visible but transition
start only when "if" props change

### unmountAfterPlayOut

`boolean` _optional_ - default: `true`   
Unmount children React element after playOut transition

### dispatchPlayState

`(play: TPlay) => void` _optional_   
Dispatch current TPlay string type

<details>
  <summary>Type TPlay</summary>

```ts
type TPlay = "hidden" | "play-out" | "play-in" | "visible";
```

</details>

```tsx
import React, {useEffect, useState} from "react"
import {Transition} from "@cher-ami/react-transition"

const App = () => {
  // ...
  const [elementPlayState, setElementPlayState] = useState(null);
  useEffect(()=> {
    if (elementPlayState === "play-in") {
      // ...  
    }
  }, [elementPlayState])

  return (
    <>
      <Transition
        // ...
        // Everytime transition playState change, elementPlayState will be updated
        dispatchPlayState={(playState) => setElementPlayState(playState)}
      >
        <div className={"element"} />
      </Transition>
    </>
  )
}
```

## <a name="Example"></a>Example

A use case example is available on this repos.

Install dependencies

```shell
$ npm i
```

Start dev server

```shell
$ npm run dev
```

## License

[MIT](./LICENSE)

## Credits

[Willy Brauner](https://willybrauner.com)

---
_Source: https://npm.io/package/@cher-ami/react-transition · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
