How to animate a list-to-grid layout change in React
November 14, 2026 · 7 min read
Many apps let you flip between a list and a grid. Most implement it by swapping a class name, which makes every item jump to its new position at once. It works, but it throws away a chance to show the user how the two views relate. When items glide from their list position to their grid position, people can follow where each one went, and the change feels intentional.
Why this is hard with CSS
CSS cannot animate between a flex column and a grid, because layout properties like display, grid-template-columns and flex-direction are not animatable. The browser moves items instantly. The trick is to measure where each element was, apply the new layout, measure where it is now, and animate the difference using a transform. This technique is called FLIP: First, Last, Invert, Play.
Motion does the whole FLIP dance for you when you add the layout prop to an element.
The basic version
"use client"
import { useState } from "react"
import { motion } from "motion/react"
const items = ["Spring", "Press", "Slide", "Pop"]
export function Gallery() {
const [view, setView] = useState<"list" | "grid">("grid")
return (
<>
<button onClick={() => setView(view === "grid" ? "list" : "grid")}>
Toggle
</button>
<motion.div
layout
className={view === "grid" ? "grid grid-cols-2 gap-2" : "flex flex-col gap-2"}
>
{items.map((item) => (
<motion.div
key={item}
layout
transition={{ type: "spring", stiffness: 420, damping: 34 }}
className="rounded-xl bg-white p-4 shadow"
>
{item}
</motion.div>
))}
</motion.div>
</>
)
}Two details matter here. The parent has the layout prop so its own size change is animated, and every child has it too so each one moves. The keys must be stable, because Motion matches elements between renders by key.
Avoiding distortion
Layout animations are performed with transforms, and when an element changes size, Motion scales it. That can stretch the text inside and squash rounded corners while the animation runs. There are three fixes:
- Set borderRadius and boxShadow through the style prop, not through classes. Motion corrects these values during the animation so corners stay round.
- Use layout="position" on text elements that should only move, not resize. The text slides but never stretches.
- For child elements that change size noticeably, give them their own layout prop so Motion can counter-scale them.
Items that enter and leave
If your list is filtered or sorted, items will be added and removed too. Wrap the list in AnimatePresence so removed items can play an exit animation, and give each item an exit prop such as an opacity fade. The remaining items will slide into the gaps because they have the layout prop.
When several independent components share layout changes, wrap them in a LayoutGroup so Motion treats a change in one as a trigger for the others.
Performance and accessibility
Layout animations measure the DOM, so keep the number of animated items reasonable. A few dozen is fine, hundreds will cost frames. If you have a very long list, only animate what is visible.
The toggle itself should be a proper control. Use role="radiogroup" with two radio buttons labelled "List view" and "Grid view", and keep aria-checked in sync. As with any motion, respect prefers-reduced-motion: with MotionConfig set to reducedMotion="user", layout animations are skipped and the view simply changes.
The Layout Toggle component on makeui gives you the switch with its sliding highlight, ready to pair with the pattern above.