Introducing AutoAnimate — add motion to your apps with a single line of code.
Tags: react, vue, javascript, webdev
Published: 2022-05-19 16:30:33

When it comes to building user interfaces, a little motion goes a long way. Whether you are adding a task to your todo list, deleting a contact, or sorting a playlist, interfaces with subtle motion _unquestionably_ provide a superior user experience. If we know some motion is better — why do we so seldom add it?

![Motion can be hard to visualize](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9oog9eisxg95infw45wu.gif)

The answer is as simple — *it just takes too much time*. Time is money after all, and optimizing for "minor" UX details often falls outside the budget of all but the biggest brands.

To be fair, adding UI animations can be painful — especially for new, removed, and moved DOM elements. For example, when animating the addition of a new item to a list an experienced dev might do the following:

  - Measure the parent element and set its `max-height` explicitly, and add a class with css transitions for `max-height`.
  - Add a class to the new list item before its added to the DOM that sets its initial opacity, and transform state (perhaps a slight scale down).
  - Inject the element, and set a very short timeout that removes the initial class.
  - Calculate the height of the element being added, and increase the parent’s `max-height` by that amount.
  - Wait for the height to be fully transition, and remove the explicit max-height properties.

This is not fun! Enter AutoAnimate.


![AutoAnimation Logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8w3ctibxsz96ricw10ov.png)

AutoAnimate is a lightweight (1.9Kb), zero-config, drop-in, animation library that automatically applies transition animations to elements being added, removed, or moved in the DOM. It literally takes one line of code to implement, and it works with React, Vue, and any other JavaScript framework you want.

> AutoAnimate’s goal is to substantially improve an application’s user-experience without impacting the developer’s implementation time or performance budget.

Let’s take a look at two identical lists written in React — one with AutoAnimate and one without.

{% embed https://stackblitz.com/edit/auto-animate-react?ctl=1&embed=1&file=src/App.jsx&hideExplorer=1&hideNavigation=1&theme=dark&view=preview %}

The details of the list are just standard React code, but lets take a look at how animations were added to the second list:

```jsx
import React from 'react';
import FrameworkList from './FrameworkList';
import { useAutoAnimate } from '@formkit/auto-animate/react';

export default function App() {
  const [animationParent] = useAutoAnimate();
  return (
    <section className="comparison">
      <FrameworkList />
      <FrameworkList ref={animationParent} />
    </section>
  );
}
```

That’s it? Yep. And it might even be easier if you use Vue!

```vue
<script setup>
import FrameworkList from './FrameworkList.vue'
</script>

<template>
  <section class="comparison">
    <FrameworkList />
    <FrameworkList v-auto-animate />
  </section>
</template>
```

Of course AutoAnimate works great with plain ol' native JavaScript too! All you need to do is pass the parent DOM element into the autoAnimate function:

```js
const el = document.getElementById('#my-el')
autoAnimate(el)
```

AutoAnimate is made by myself ([Justin Schroeder](https://twitter.com/jpschroeder)) and the team from [FormKit](https://formkit.com/), and as of today the beta is publicly available!

<a href="https://auto-animate.formkit.com">
![Examples & Docs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gnerj0t6ggczhbbrjfze.png)
</a>

If you find AutoAnimate is helping you out, consider supporting us. You can:

- [Sponsor the FormKit organization](https://github.com/sponsors/formkit).
- [Star the AutoAnimate repository](https://github.com/formkit/auto-animate)
- [Follow FormKit](https://twitter.com/useformkit) and [myself](https://twitter.com/jpschroeder) on Twitter.