Article

Make awesome animated interactions with two lines of code

/img/articles/view-transitions-api-animated-interactions-hero.png

You might have heard about the View Transitions API in the context of animating between web pages. Whilst this is certainly awesome, you can apply this approach on a way smaller level to create awesome animated interactions in your web applications!

A few weeks ago I was lucky enough to visit the Google Campus in San Francisco through the Google Developer Expert program I am part of. Dave bitter on the Google Campus in San Francisco riding a Google bike There, Una Kravets showed a demo she built using the View Transitions API to animate the deletion of cards. This excited me to look at using the API for not just page transitions, but more for micro animations!

Give feedback to the user

As always, I made to see what we can do with this approach:

See the Pen View Transitions Animate Cards by Dave Bitter (@davebitter) on CodePen.

View on CodePen

First, I created a layout like I would normally do. Next, I wrote some JavaScript to remove a list item with a small delay to see it get the checked state first. Removing an item looked like this:

list.addEventListener('change', (e) => {
  setTimeout(() => {
    removeListItemFromInputChangeEvent(e)
  }, 300)
})

Screen recording showing an item being removed and the grid of cards immediately snap into place The user experience of removing an item is not great. Because the item is removed, the item next to it now takes it place. Suddenly, this item is in the place of the removed one which makes it look like it was always there. What if we can remove it, have all the items neatly animate to their new position and by doing that give the user the proper feedback they need? In the past, this took quite a bit of JavaScript to do and even then it worked a bit janky. Now let’s (technically) add two lines of code. First, wrap the call of the remove function in a startViewTransition. For good measure, we’ll also feature check to progressively enhance the user experience. Meaning, if the View Transitions API is supported, we enhance the default behaviour with a nice animation:

list.addEventListener('change', (e) => {
  setTimeout(() => {
    if (!document.startViewTransition) {
      removeListItemFromInputChangeEvent(e)
    } else {
      document.startViewTransition(() => {
        removeListItemFromInputChangeEvent(e)
      })
    }
  }, 300)
})

Next, we add unique name to each of the list items:

<li style="view-transitions-name: card-1">
  <label>
    <input type="checkbox" />
    Check this card
  </label>
</li>

Now if we delete an item, we see this behaviour: Screen recording showing an item being removed and the grid of cards animate into place With just adding the document.startViewTransition and a unique view-transitions-name for each list item, the entire grid repositioning is animated providing better feedback to the user. Awesome, right? One of the biggest benefits I find of this approach is that the View Transitions API just takes a before and after state. It doesn’t matter how you build your layout, what properties change or how different the two views are. As long as before and after your change (the removal of the one list item node) items have matching view-transitions-name properties, the browser will animate between them.

Now, let’s add a button that allows you to add a new item:

addButton.addEventListener('click', () => {
  addListItem()
})

Screen recording showing an item being added and the grid of cards immediately snap into place We see the same unanimated behaviour as previously when deleting an item. Now let’s add the document.startViewTransition again:

addButton.addEventListener('click', () => {
  if (!document.startViewTransition) {
    addListItem()
  } else {
    document.startViewTransition(() => {
      addListItem()
    })
  }
})

Screen recording showing an item being added and the grid of cards animate into place As you can see, now when an item is added, the rest of the items animate to their new position. We can even add a bit of JavaScript that will randomly reorder the list and see what this technique will do: Screen recording showing an items being randomly reordered and the grid of cards animate into place

Some other use cases

While having a grid of cards that may be filtered is a pretty common use case, you can naturally take this approach to other interactions as well. More than likely, you are working/have worked with tables. When adding to, removing from, sorting or filtering these tables the rows may change position. When calling the startViewTransition you can animate the changing of the rows like so: Screen recording showing an items being reordered, removed and added and the grid of cards aniamte into place You may have noticed I added a toggle to this demo for showing the items in a grid or underneath each other. When switching, that looks like this: Screen recording showing the grid of cards toggle between grid and table view and immediately snap into place I though to myself, why not use this technique when switching between the two views? Screen recording showing the grid of cards toggle between grid and table view and animating into place

Start using this now

It has never been easier to animate between different views on the page. Whilst animating between pages might be a bit much to start with, try implementing the View Transitions API on a smaller part of a page to make smoother experiences in your web application whilst simultaneously providing feedback for a user’s interactions through animation. It only takes a line or two!

See the Pen View Transitions Animate Cards by Dave Bitter (@davebitter) on CodePen.

View on CodePen

Read on