Vue.js course ยท Module 13: Transitions & Animations
TransitionGroup - Animating Lists
In this lesson5
The NOVA LAB station module list is constantly changing - we add new life support systems, remove damaged sensors, sort alerts by priority. Each time, we want the list changes to be visually smooth. <TransitionGroup> is a Vue component designed specifically for animating elements rendered with v-for.
Basic Usage
<TransitionGroup name="list" tag="ul">
<li v-for="item in items" :key="item.id">
{{ item.name }}
</li>
</TransitionGroup>Each element added to the list animates on entry, and removed ones animate on exit. This automatic behavior is based on the same CSS classes as <Transition>.
Key Differences from <Transition>
<TransitionGroup> differs from <Transition> in several important ways:
It renders a DOM element - unlike
<Transition>,<TransitionGroup>renders an HTML element by default. You control it with thetagattribute:tag="ul",tag="div",tag="section". If you don't want an extra element, you can settag="template"(Vue 3.5+).Each element must have a unique
:key- Vue needs to track the identity of individual elements to know which ones to add, remove, and move. Never use the array index as a key!It supports multiple elements simultaneously - it's not limited to a single element like
<Transition>. Multiple elements can enter and leave at the same time.Additional
-moveclass - animates the movement of elements when the list order changes. This is a unique feature of<TransitionGroup>.The
modeattribute is not supported - because we're not switching between one element and another.
CSS Classes for Lists
TransitionGroup uses the same classes as Transition plus an additional -move class:
/* New element entering */
.list-enter-active {
transition: all 0.5s ease;
}
.list-enter-from {
opacity: 0;
transform: translateX(30px);
}
/* Element being removed */
.list-leave-active {
transition: all 0.5s ease;
position: absolute; /* IMPORTANT! */
}
.list-leave-to {
opacity: 0;
transform: translateX(-30px);
}
/* Remaining elements moving */
.list-move {
transition: transform 0.5s ease;
}Move Transitions - The Key Mechanic
This is the most interesting feature of <TransitionGroup>. When you add or remove an element from a list, the remaining elements need to shift to new positions. Vue automatically adds the -move class to elements that change position, and animates their movement using CSS transforms.
For move transitions to work correctly, leaving elements must have position: absolute in the -leave-active class. This takes the leaving element "out of flow" so the rest can smoothly shift:
.list-leave-active {
position: absolute;
/* Optionally: preserve width */
width: 100%;
}
.list-move {
transition: transform 0.5s ease;
}Practical Example - Station Alert List
<template>
<div class="alert-panel">
<button @click="addAlert">Add alert</button>
<TransitionGroup name="alert" tag="div" class="alert-list">
<div
v-for="alert in alerts"
:key="alert.id"
class="alert-item"
:class="alert.level"
>
{{ alert.message }}
<button @click="removeAlert(alert.id)">x</button>
</div>
</TransitionGroup>
</div>
</template>
<script setup>
import { ref } from 'vue'
const alerts = ref([])
let nextId = 1
function addAlert() {
alerts.value.unshift({
id: nextId++,
message: `Alert #${nextId - 1}: Oxygen level check`,
level: Math.random() > 0.5 ? 'warning' : 'critical'
})
}
function removeAlert(id) {
alerts.value = alerts.value.filter(a => a.id !== id)
}
</script>.alert-enter-active, .alert-leave-active {
transition: all 0.4s ease;
}
.alert-enter-from {
opacity: 0;
transform: translateX(-30px);
}
.alert-leave-to {
opacity: 0;
transform: translateX(30px);
}
.alert-leave-active {
position: absolute;
}
.alert-move {
transition: transform 0.4s ease;
}The effect: new alerts enter from the left, removed ones exit to the right, and the rest smoothly shift into place. Exactly like on a space station alert panel!
Code for this lesson: App.vue
<script setup>
import { ref } from 'vue'
const modules = ref([
{ id: 1, name: 'Oxygen Generator', status: 'active' },
{ id: 2, name: 'Water Recycler', status: 'active' },
{ id: 3, name: 'Solar Panel Array', status: 'active' }
])
let nextId = 4
function addModule() {
const names = ['Thermal Shield', 'Nav Computer', 'Comm Relay', 'Greenhouse']
const name = names[Math.floor(Math.random() * names.length)]
modules.value.splice(
Math.floor(Math.random() * (modules.value.length + 1)),
0,
{ id: nextId++, name, status: 'active' }
)
}
function removeModule(id) {
const idx = modules.value.findIndex(m => m.id === id)
if (idx > -1) modules.value.splice(idx, 1)
}
function shuffle() {
modules.value = [...modules.value].sort(() => Math.random() - 0.5)
}
</script>
<template>
<div class="nova-lab">
<h1>TransitionGroup Demo</h1>
<div class="controls">
<button @click="addModule" class="btn add">+ Add module</button>
<button @click="shuffle" class="btn shuffle">Shuffle</button>
</div>
<TransitionGroup name="list" tag="ul" class="module-list">
<li v-for="mod in modules" :key="mod.id" class="module-item">
<span>{{ mod.name }}</span>
<button @click="removeModule(mod.id)" class="remove">ร</button>
</li>
</TransitionGroup>
</div>
</template>
<style scoped>
.nova-lab {
background: #0a0e27;
color: #00ff88;
min-height: 100vh;
padding: 20px;
font-family: monospace;
}
h1 { color: #00ff88; }
.controls { display: flex; gap: 10px; margin: 15px 0; }
.btn {
border: none;
padding: 10px 16px;
border-radius: 8px;
cursor: pointer;
color: white;
}
.add { background: #00b4d8; }
.shuffle { background: #9b59b6; }
.module-list {
list-style: none;
padding: 0;
}
.module-item {
display: flex;
justify-content: space-between;
align-items: center;
background: rgba(0, 180, 216, 0.1);
border: 1px solid #00b4d8;
padding: 12px 16px;
margin: 8px 0;
border-radius: 8px;
}
.remove {
background: #e74c3c;
color: white;
border: none;
width: 28px;
height: 28px;
border-radius: 50%;
cursor: pointer;
}
/* TransitionGroup classes */
.list-enter-active,
.list-leave-active {
transition: all 0.4s ease;
}
.list-enter-from,
.list-leave-to {
opacity: 0;
transform: translateX(30px);
}
/* Move animation */
.list-move {
transition: transform 0.4s ease;
}
.list-leave-active {
position: absolute;
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. How does <TransitionGroup> differ from <Transition>?
2. What does the tag='ul' attribute do in <TransitionGroup>?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Use <TransitionGroup> with v-for and name='list' to animate the list.
- Click in order
Arrange the correct TransitionGroup syntax:
- Code editor
Use the .list-move class with a transition on transform to animate movement.