Animation in React

Animating your React apps doesn’t have to be a hassle. With these helpful components and prebuilt animation keyframes, you’ll be adding animation to your apps in no time.

Animating is difficult

We often forget about how animation is going to feature into our web projects. It’s easy to let this happen – designs tend to take shape in the form of flat images, and when we’re coding we’re thinking about browser compatibility, screen sizes, and the implementation of features. How UI moves often finds itself thrown in as an afterthought.

This can cause issues. Sometimes parts of our UI need to move. They need movement to smooth the transition between states or to draw attention to the most important action or newest content.

For example, we might want to add a little animation when a price changes so that people don’t miss it, as in this little example:


See the example on CodePen
1

 

It can become a problem when we leave this to last and put together quick solutions as needed, we can create situations where our animations aren’t consistent, applied to the wrong things, or even forgotten entirely.

Introducing React Animation

We’ve been working on ways to make adding UI animation to React projects quicker and easier, including releasing a new package called React Animation. React Animation is a helpful package of wrapper components along with pre-built animations you can apply to projects easily.

Why not just use something else? That’s a fair question. There are lots of good ways of building animations into our React UI. React Transition Group offers a handy approach to adding and removing classes so that we can then apply animations to it. Unfortunately though, React Transition Group doesn’t bring any animations, you still need to handle that part yourself. On the other hand, projects like React Spring offer advanced animation tools for handling large sets of animated elements.

What we needed was something in between – something that made it easy to apply animation to elements and offered a consistent set of pre-built animations we could apply to our projects knowing that the results would be reliable and consistent. It had to be easy to install and use, and offer enough flexibility that we could customise the animations to suit the project. It also gave us an opportunity to build using React hooks!

Let’s take a look at what React Animation can do, starting with the helper components.

AnimateOnChange

The repo includes components you can wrap around your content and they’ll apply animation as needed. The first is AnimateOnChange.

Sometimes you want to have a little movement on screen to let people know when some content has changed. Writing this functionality for lots of small pieces of UI can quickly become a hassle, so instead you can simply wrap your content in the AnimateOnChange component.

We can get started by running npm install react-animation. Then the component is used as:

import { AnimateOnChange } from ‘react-animation’
<AnimateOnChange>
  Your content, components etc here
</AnimateOnChange>

This component will respond to any change in the “children” content by applying a simple fade-out then fade-in animation with the new content. We can see the effect on this example:


See the example on CodePen
1

 

There’s more we can do with this component. We can pass in properties to control how the animation works. One option is to supply a durationOut value (a number of milliseconds). This “durationOut” is the time the “out” animation will take (when the old content is animated away). We can set this to a larger or smaller number to change the feel of the fade animation. Here’s a slower example with a durationOut value of 1,500:


See the example on CodePen
1

 

By passing in animationIn and animationOut properties, we can have other animations apply when the content changes. These take the name of one of the included animations in the package. You can find a full list of these animations at the end of this post.

<AnimateOnChange animationIn="bounceIn" animationOut="bounceOut">
  Your content, components etc here
</AnimateOnChange>

This makes use of the built-in bounce animations. Here it is in action:


See the example on CodePen
1

 

If we need even more control, we can set any properties we like on the component using the style property. This is just like the “style” property in React, and accepts an object containing style rules in the normal way.

Custom animations

If you want to create your own custom animations for the AnimateOnChange component, there are two ways do to so. First, you can create your own keyframes and pass in your own animation string to both the animationIn and animationOut properties.

<AnimateOnChange
  animationOut="custom-animation-out 500ms ease-out forwards"
  animationIn="custom-animation-in 500ms ease-out forwards"
  durationOut={500}
>
  Your content
</AnimateOnChange>

This will run the animation keyframes “my-anim-out” when hiding the content, and “my-anim-in” on the when showing the new content. We could define these keyframes like so:

@keyframes custom-animation-in {
  from {
    transform: rotateX(140deg) scale(32);
  }
  to {
    transform: none;
  }
}

@keyframes custom-animation-out {
  from {
    transform: none;
  }
  to {
    transform: rotateX(-270deg) scale(0);
  }
}

This would result in a 3D rotating zoom effect, as seen here:


See the example on CodePen
1

 

A second approach is to use classes. If you have a situation where you need to animate an element’s pseudo-element, or some element’s children, you might want to have a top left class applied for when the animation is in the “in” or “out” state. You can do this by specifying className.

<AnimateOnChange
  className="foo"
  durationOut={500}
>
  Your content
</AnimateOnChange>

This will apply the classes “foo-in” and “foo-out” for each stage. Here’s an example of it in action.


See the example on CodePen
1

 

Having parts of your UI animate can be very useful for making sure people notice when information changes. You could use it to update a value in a live-streamed data set such as stock prices, or to call attention to when an item has been added to a shopping basket. The possibilities are endless.

HideUntilLoaded

Some components make use of a background image, or some other featured image that can be dynamic and so potentially a large file size. We wouldn’t want the component to display before we know that the image has loaded as this could result in showing partially-loaded images. Sometimes, instead, you want a component to stay hidden until an image has completed loading. This is where HideUntilLoaded helps.

You can use this in a similar way, wrapping your content and in this case telling it which image to wait for.

<HideUntilLoaded imageToLoad="[image_url]">
  Your content here
</HideUntilLoaded>

You can see this in action here:


See the example on CodePen
1

 

We can even go further and pass in our own Spinner component that will show when loading is taking place.

<HideUntilLoaded imageToLoad="[image_url]" Spinner={Your Spinner Component}>
  Your content here
</HideUntilLoaded>


See the example on CodePen
1

 

Finally, we can also specify an animationIn for when the content has loaded. In this case we’ll use the popIn animation (find these and more example animations on this demo page).

<HideUntilLoaded imageToLoad="[image_url]" animationIn=”popIn”>
  Your content here
</HideUntilLoaded>


See the example on CodePen
1

 

The HideUntilLoaded component could be useful if you have a component that relies on a large background image – this could be product images with drop shadows and text overlays, or media components with photos and text information. These can be held back until ready and then animated in together.

Prebuilt keyframes

The package also includes some helpful pre-built animations you can use. You can even bring them into your project without using either of the above helper components. To import them, you’ll also need the associated keyframes, like so:

import 'react-animation/dist/keyframes.css'
import { animations } from 'react-animation'

With the keyframes imported, we can use the animations object in our component styles. An example might be when you want a component to pop in on first load. We could make use of the animation like so:

const style = {
  animation: animations.popIn
}

const MyComponent = () => 
My Component

In this example, animations.popIn evaluates to pop-in 500ms cubic-bezier(0.19, 1, 0.22, 1) forwards. You can use any of the built-in animations:

  • fadeIn
  • fadeOut
  • fadeInUp
  • popIn
  • popOut
  • bounceIn
  • bounceOut
  • slideIn
  • slideOut

Give it a go

Can you think of situations where animation could help your UI? Maybe you want to draw attention to an important button, or fade-in new items in a list. If so, feel free to try React Animation. You can get started using:

npm install react-animation 

From there you can import the components:

import { AnimateOnChange, HideUntilLoaded } from 'react-animation'

They will automatically bring in the needed keyframes definitions, so you can then use the components in your app!

I hope you find this a helpful and easy way to add animation to your projects.

At NearForm, we have vast experience in building solutions across a broad tech stack to deliver reduced complexities and overcome common hurdles. If you are creating modern applications and leveraging web technologies, contact us to learn more about how we can help.

 

You might also like some of our previous blog posts on React:

Forget everything you learned about React – Hooks rocks! 

Managing React state with Render Props

Exploring React Portals

Sharing React components with Lerna

nearform-news

Top