Animate your React App with Pose

How to bring your React app to life with animation using Pose

Animate your React App with Pose

Animation on the web can be challenging because there are many ways to achieve the same goal. You could use CSS to achieve transitions or more complex animations or you could try one of the many JS libraries available for animation.

Have you ever asked yourself one of the following:

Why is animation so difficult in React?

How do I animate a component in on mount?

Do I use CSS or JS to animate my components?

I use CSS animations whenever possible but they can quickly become unwieldy beyond basic :hover and :focus states. It's possible to achieve a lot by toggling classes to your React components but the most flexible and easy to use method I've found is to use the React animation library Pose, from the fine people over at Popmotion. The techniques I use here can be used in React, React Native and Vue. It's easy to use, very powerful and produces great looking animations.

Why should I add animations to my web app?

If you aren't fully convinced why you should use animations in your React app, allow me to champion some reasons.

  1. You use React/Vue already
    If you already use a modern component based JS framework like React or Vue, you already know the benefits. React allows for interfaces that are dynamic and responsive for users. Animation is made easier than ever with React and doesn't have to be a nightmare.
  2. Delight your users
    A user doesn't click a button and wait for an entire page to be returned from the server, user's expectations are for rich experiences which animation enhances. It's icing on the cake that elevates your app.
  3. Contextualize actions
    A static webpage tells the user that nothing is happening. Animations can help contextualize what the user is doing and how it impacts change in your app.
    A loading indicator that animates, tells the user your app is alive and is (hopefully) doing something in the background.
    Clicking a delete button and seeing the item fade out or slide away, gives the user trust that the action has taken place. It hasn't just popped out of existence.

Know that you know why, let's take a look at how I use Pose to animate a simple UI.

A Chat App Example

Animate your React App with Pose The animations in action

See the code in action or take a look at the repo on GitHub:

How the message bubble animation works

const ChatWindow = posed.ol({
  visible: {
    staggerChildren: 120
  },
  hidden: {
    staggerChildren: 100
  }
});

const MessageItem = posed.li({
  visible: {
    x: 0,
    opacity: 1
  },
  hidden: {
    x: ({ fromDirection }) => (fromDirection === "left" ? -350 : 350),
    opacity: 0
  }
});

function Chat({ messages, visible }) {
    return(
        <ChatWindow
          className="chat"
          pose={visible ? "visible" : "hidden"}
        >
          {messages.map(m => (
            <MessageItem
              className={`container ${m.isOwner ? "sent" : "received"}`}
              fromDirection={m.isOwner ? "right" : "left"}
              key={m.id}
            />)
          }
    </ChatWindow>);
}

Simplified view of Chat, focused on the animation aspect

The two main components that make this animation work are the ChatWindow and MessageItem. We use posed.ol to create a component that will stagger the animations on child components by 120ms when it becomes visible and 100ms when the component hides. We can control whether the Chat is visible with a simple prop. We then use posed.li for the child components. They start in a hidden state where they are off the screen by 350px (either to the left or right depending on whether we are the sender or recipient of the message), so when they are told to become visible, they animate towards an opacity of 1 and an x coordinate of 0. That's it.

Pose handles the animation for us, we simply define the possible states we want to animate between and pose does the rest. This is a very flexible system that's easy to understand and produces really nice results with minimal effort.

How the person badges animate in and out

import React from "react";
import posed, { PoseGroup } from "react-pose";
import Person from "./Person";

const PersonItem = posed.li({
  enter: {
    opacity: 1,
    scale: 1,
    delay: ({i}) => i * 100
  },
  exit: {
    opacity: 0,
    scale: 0,
    delay: ({i}) => i * 20
  }
});

function PersonSelector({ people }) {
  return (
    <ul className="person-selector">
      <PoseGroup animateOnMount>
        {people.map((p, i) => (
          <PersonItem className="item" key={p.id} i={i}>
            <Person {...p} />
          </PersonItem>
        ))}
      </PoseGroup>
    </ul>
  );
}

export default PersonSelector;

The PersonSelector component that sits at the top of the chat window

We make use of the built in enter and exit states for our PersonItem. We want the animation to happen on mount so we wrap the children in a PoseGroup with the animateOnMount. When the component mounts or the people prop changes this will trigger the animation. We also make use of the delay property in a similar way to the staggerChildren property in our ChatWindow component. This gives a staggered animation as each Person fades and scales in.

How do I add animations to my React app?

To get started today, take a look at the getting started guide for pose, it takes you through the fundamentals so you can start breathing life into your React apps. (as well as React Native or Vue). Pose is a great library that delivers good looking animations without having to spend hours tweaking CSS animations or dealing with some other complex animation libraries. Set up your states or poses and let Pose take care of the rest.

Comments (4)

Add a comment
Serdar Sanri's photo

Hi Seth,

Thanks for the article. Something got my attention here,

#Why should I add animations to my web app? If you aren't fully convinced why you should use animations in your React app, allow me to champion some reasons.

You use React/Vue already If you already use a modern component based JS framework like React or Vue, you already know the benefits. React allows for interfaces that are dynamic and responsive for users. Animation is made easier than ever with React and doesn't have to be a nightmare.

To be honest, this doesn't make any sense. Animations are flavor to your application and makes user experience delightful and better. But that doesn't mean if you are using React or Vue you should use animations.

One thing to keep in mind is animations comes with its own cost. Extra code, extra storage, extra cpu. So if your goal is performance, every single bit counts. One thing to keep in mind, and I think most developers nowadays miss this point, not everybody has your super power boosted machine.

Seth Corker's photo

Full Stack Developer, specializing in React

Thanks Serdar,

You're right, if performance is a concern then it is important to accommodate users on lower powered devices. I'm not advocating you add animations every time but I think animations when used correctly can enrich the user experience.

What I was trying to allude to is the use of animation can help contextualize actions for the user and communicate that your app is doing something.

  • A modal with a slight fade-in or slide-in may be less jarring than simply appearing.
  • An spinning activity indicator says to the user that the UI is not unresponsive and performing a task
  • A subtle glow on a button can grab the user's attention for an on-boarding experience

As with anything, you don't have to add animations to everything and you shouldn't all the time but if you do, I think Pose is a good way to do it for developers. It's easy to use and much more flexible than other approaches I've come across. The mantra of the web is progressive enhancement, if you can achieve a performant experience on lower-end devices but cater to powerful devices too, it's a win-win.

Charis Harry's photo

Nice post

Rafael Morais's photo

Wow, I needed this!