Animating Layouts with the FLIP Technique

FLIP animates a layout change by measuring an element before and after the change and animating the difference.

What is the FLIP Technique?

FLIP stands for First, Last, Invert, Play. It is a technique for animating UI layouts that was introduced by Paul Lewis and Jonathan Neal in 2015. The FLIP technique works by first recording the starting and ending positions and dimensions of the elements that need to be animated, then inverting the starting position and dimensions to create a new “flipped” state, and finally animating the element from the flipped state to the final state.

Here are the steps involved in the FLIP technique:

  1. First: Record the current position and dimensions of the element that needs to be animated.
  2. Last: Record the final position and dimensions of the element that needs to be animated.
  3. Invert: Invert the starting position and dimensions to create a new “flipped” state.
  4. Play: Animate the element from the flipped state to the final state.

FLIP measures layout at the start and end, then uses transforms for the animation. Measuring layout can still trigger layout work.

How to Use the FLIP Technique

To use the FLIP technique, we need to perform the following steps:

  1. Record the First State: Before any changes are made to the layout, record the current position and dimensions of the element that needs to be animated.
  2. Make the Changes: Make the necessary changes to the layout that will affect the position and dimensions of the element.
  3. Record the Last State: After the changes have been made, record the final position and dimensions of the element.
  4. Invert the First State: Calculate the differences between the first and last state, and invert the values to create a new “flipped” state.
  5. Animate to the Last State: Animate the element from the flipped state to the final state.

Let’s take a look at an example to see how this works in practice.

Example: Animating a Card Layout

Suppose we have a layout that displays a list of cards. When a user clicks on a card, we want to expand it and show additional content. Here’s how we can use the FLIP technique to animate the card layout:

  1. Record the First State: Before any changes are made, record the current position and dimensions of the clicked card.
  2. Make the Changes: When the user clicks on the card, we need to expand it and show additional content. We can do this by adding a CSS class to the card that sets its height to a larger value.
  3. Record the Last State: After the changes have been made, record the final position and dimensions of the card.
  4. Invert the First State: Calculate the differences between the first and last state, and invert the values to create a new “flipped” state. In this case, we need to invert the height of the card.
  5. Animate to the Last State: Animate the card from the flipped state to the final state using a CSS transition or animation.

The example assumes .card exists, its dimensions are nonzero, and the .expanded class changes its layout. Run this when the card is clicked:

const card = document.querySelector('.card');

// Record the first state
const first = card.getBoundingClientRect();

// Make the changes
card.classList.toggle('expanded');

// Record the last state
const last = card.getBoundingClientRect();

// Invert the first state
// Calculate the differences between the first and last state
const invert = {
    x: first.left - last.left,
    y: first.top - last.top,
    width: first.width / last.width,
    height: first.height / last.height
};

// Show the element at its original position without a transition.
card.style.transformOrigin = 'top left';
card.style.transition = 'none';
card.style.transform = `translate(${invert.x}px, ${invert.y}px) scale(${invert.width}, ${invert.height})`;

// Commit the initial transform before animating to the final layout.
card.getBoundingClientRect();
requestAnimationFrame(() => {
    card.style.transition = 'transform 300ms ease';
    card.style.transform = 'none';
});

In this example, we first record the position and dimensions of the card before any changes are made. Then, we add a CSS class to the card that sets its height to a larger value. After the changes have been made, we record the final position and dimensions of the card. We then calculate the differences between the first and last state and invert them to create a new “flipped” state. Finally, we animate the card from the flipped state to the final state using a CSS transition, and remove the flipped state once the animation is complete.

Conclusion

FLIP moves layout work to the measurement steps and animates the resulting transform. Test the result with your layout and respect reduced-motion preferences.

References: