4.2.8 • Published 3 months ago

react-awesome-reveal v4.2.8

Weekly downloads
10,227
License
MIT
Repository
github
Last release
3 months ago

React Awesome Reveal

Version Last Commit Downloads Size License Rate on Openbase

!TIP If you like this library, please consider supporting its creator.

React Awesome Reveal is a library for React apps written in TypeScript that adds reveal animations using the Intersection Observer API to detect when the elements appear in the viewport. Animations are internally provided by Emotion and implemented as CSS Animations to benefit from hardware acceleration.

Table Of Contents

Features

  • 🎁 Modern stack – It is built for modern React and supports React 18
  • 🏷 TypeScript support – It is written in TypeScript to improve the DX
  • 🍃 Lightweight – Very little footprint on your project
  • ⚙️ Uses native APIs – Intersection Observer and CSS Animations are now supported by all browsers
  • 🚀 Fast – Buttery smooth experience thanks to the use of native asynchronous APIs and hardware acceleration
  • 💅 Fully customizable – Define custom animations and let the library render them
  • 💻 SSR support – Server Side Rendering works out-of-the-box
  • 🌳 Tree-shakeable – Only the parts you use will be included in your final bundle

Installation

To add this package as a dependency to your app, simply run

npm install react-awesome-reveal @emotion/react --save

or, if you are using Yarn:

yarn add react-awesome-reveal @emotion/react

or, if you are using PNPM (as I strongly suggest):

pnpm add react-awesome-reveal @emotion/react

Quick Start

Import effects from React Awesome Reveal to your React component, for example the Fade effect:

import { Fade } from "react-awesome-reveal";

Then simply wrap the components you want to animate:

<Fade>
  <p>I will gently appear as I enter the viewport</p>
</Fade>

Supported Effects

The effects currently supported are Bounce, Fade, Flip, Hinge, JackInTheBox, Roll, Rotate, Slide and Zoom. Refer to the Animate.css documentation for the details.

Attention Seekers

Since version 3, attention seeker animations are wrapped by the AttentionSeeker component, which accepts a prop called effect that specifies the animation to render (defaults to "bounce”). The supported effects are: ”bounce", "flash", "headShake”, "heartBeat", "jello”, "pulse", "rubberBand", “shake”, “shakeX", "shakeY”, "swing”, "tada" and “wobble”.

Again, refer to the Animate.css documentation for the details.

Props

You can pass the following props to the animation components to customize the behavior:

PropDescriptionValuesDefault
cascadeIf set, each child of a reveal animation automatically get assigned a delay that takes into account their predecessor (child i enters the viewport after i * delay * damping milliseconds) – useful for animating list items.true or falsefalse
dampingFactor that affects the delay that each animated component in a cascade animation will be assigned. If damping = 1 then the delay will be equal to the animation duration; if damping < 1 then the delay will be lower than the animation duration; if damping > 1 then the delay will be greater than the animation duration.number0.5 (meaning that the delay will be half of the animation duration)
directionOrigin of the animation (where applicable).Usually "down", "left", "right" or "up", with some exceptions documented in the codeundefined
delayTime to wait before the animation starts (in milliseconds).number0
durationThe animation duration (milliseconds).number1000
fractionHow much an element should be in viewport before the animation is triggered.number between 0 and 10
triggerOnceSpecifies if the animation should run only once or everytime an element enters/exits/re-enters the viewport.true or falsefalse
classNameThe class names to add to the container element.stringundefined
styleThe inline styles to add to the container element.React.CSSPropertiesundefined
childClassNameThe class names to add to the child element.stringundefined
childStyleThe inline styles to add to the child element.React.CSSPropertiesundefined
onVisibilityChangeCallback executed when the element enters or leaves the viewport. If more than one element is being animated, this function is called on each element.(inView: boolean, entry: IntersectionObserverEntry) => voidundefined

Example

To trigger the animation only the first time an element enters the viewport:

<Slide triggerOnce>
  <p>I will animate only the first time you see me</p>
</Slide>

Chaining Multiple Animations

To chain together multiple animations, set the cascade prop to true:

<Fade cascade>
  <p>I enter first...</p>
  <p>...then comes my turn...</p>
  <p>...and finally you see me!</p>
</Fade>

Play with the damping prop to alter the delay by which each child will progressively appear:

<Fade cascade damping={0.1}>
  <p>I enter first...</p>
  <p>...then comes my turn...</p>
  <p>...and finally you see me!</p>
</Fade>

Custom Animations

Starting from version 3.2.0, you can define custom animations! Simply import the Reveal component (which is the default export of the library – give it the name you want) and pass it a keyframes prop:

import React from "react";
import Reveal from "react-awesome-reveal";
import { keyframes } from "@emotion/react";

const customAnimation = keyframes`
  from {
    opacity: 0;
    transform: translate3d(-200px, -100px, 0);
  }

  to {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }
`;

function CustomAnimation({ children }) {
  return <Reveal keyframes={customAnimation}>{children}</Reveal>;
}

If no keyframes prop is passed, the default rendered animation is a fading entrance from the left (equivalent to <Fade direction="left">...</Fade>).

Other Props

You can also pass these props to Reveal:

  • cascade
  • damping
  • delay
  • duration
  • fraction
  • triggerOnce
  • className and childClassName
  • style and childStyle
  • onVisibilityChange

Intersection Observer

Intersection Observer is the API used to determine if an element is inside the viewport or not. Browser support is really good – with Safari adding support in 12.1, all major browsers now support Intersection Observers natively.

If you need to support old browsers, add the polyfill for the Intersection Observer API.

Polyfill

You can add the polyfill directly or use a service like polyfill.io to add it when needed.

yarn add intersection-observer

Then import it in your app:

import "intersection-observer";

If you are using Webpack (or similar) you could use dynamic imports to load the polyfill only if needed. A basic implementation could look something like this:

/**
 * Do feature detection, to figure out which polyfills needs to be imported.
 **/
async function loadPolyfills() {
  if (typeof window.IntersectionObserver === "undefined") {
    await import("intersection-observer");
  }
}

Past Releases

To see the documentation for previous versions, navigate through past tags in the GitHub's project repository and read the README for that specific version.

License

Project source code is licensed under the MIT license. You are free to fork this repository, edit the code, share and use it both for non-commercial and commercial purposes.

4.2.8

3 months ago

4.2.7

6 months ago

4.2.6

6 months ago

4.2.5

10 months ago

4.2.4

10 months ago

4.2.3

1 year ago

4.2.2

1 year ago

4.2.1

1 year ago

4.2.0

1 year ago

4.1.0

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.0-beta.2

2 years ago

4.0.0-beta.1

2 years ago

4.0.0-beta.0

2 years ago

3.7.2

3 years ago

3.8.1

3 years ago

3.7.1

3 years ago

3.7.0

3 years ago

3.6.0

3 years ago

3.5.2

3 years ago

3.5.1

3 years ago

3.5.0

3 years ago

3.4.0

3 years ago

3.3.3

3 years ago

3.3.2

3 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.2

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.0-beta.0

4 years ago

3.0.0

4 years ago

3.0.0-alpha.2

4 years ago

3.0.0-alpha.1

4 years ago

3.0.0-alpha.0

4 years ago

2.4.2

4 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.4.0-beta.0

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.3.0-beta.2

4 years ago

2.3.0-beta.1

4 years ago

2.3.0-beta.0

4 years ago

2.2.0-1

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.2.2

4 years ago

2.1.0

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

1.6.5

4 years ago

2.0.0-beta.0

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.6.3

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.5.3

4 years ago

1.6.0

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago