Mrs. Sri has a small sign that says "Added to cart!". If the sign appears and disappears right away, customers never notice it. So Mrs. Sri lifts it slowly, holds it a moment, then lowers it slowly. She stores the sign only once it's all the way down.
In Vue, slow movement like the sign's is handled by <Transition> and <TransitionGroup>. <Transition> animates one element as it enters or leaves the page. <TransitionGroup> does the same for every item of a list. Vue doesn't draw the animation itself. It adds CSS classes at each stage, and your CSS moves the element. When an element leaves, Vue waits for the movement to finish before removing that element from the DOM, like Mrs. Sri, who stores the sign only once it's all the way down.
Transition: one element entering and leaving
An example is an "Added to cart" toast. A toast is a small message that appears for a moment, then disappears by itself. This toast shows each time Add is clicked, then disappears after 2 seconds:
<script setup>
import { ref } from "vue"
const toast = ref("")
let timer
function add(name) {
toast.value = "Added to cart: " + name
clearTimeout(timer)
timer = setTimeout(() => (toast.value = ""), 2000)
}
</script>
<template>
<button @click="add('Fried Rice')">Add Fried Rice</button>
<Transition name="fade">
<p v-if="toast" class="toast">{{ toast }}</p>
</Transition>
</template>
<style>
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style>
<Transition>is a built-in Vue component, so there's nothing to import.- It holds one element or component that appears and disappears through
v-if,v-show, or a changing:key. name="fade"sets the class prefix:fade-enter-from,fade-leave-to, and so on. Withoutname, the prefix isv-:v-enter-from.
Classes at each stage
This is the order of classes on the <p>, recorded in Chrome with the CSS above. One frame is one time the browser draws the screen:
| Time | What happens | Classes on <p> |
|---|---|---|
| 0 ms | toast is filled, <p> goes into the DOM |
fade-enter-from fade-enter-active |
| 1 frame later | fade-enter-active fade-enter-to |
|
| 300 ms | the transition ends | transition classes removed |
| 2 seconds | toast is emptied |
fade-leave-from fade-leave-active |
| 1 frame later | fade-leave-active fade-leave-to |
|
| 300 ms later | <p> is removed from the DOM |
enter-fromis the starting state when entering (hereopacity: 0), andenter-toits end state (the element's normal styles).enter-activestays for the whole process, and in the CSS above, it is the class that holds thetransition.leave-from,leave-activeandleave-toare their partners for leaving.- Vue reads the
transitionduration from the CSS and waits. So aftertoastis emptied, the<p>is still in the DOM for about 300 ms, fading out. - Without CSS, those classes come and go within one or two frames. The element appears and disappears instantly, with no error.
mode="out-in": swapping one element for another
If the <p> is written <p :key="toast"> and the toast's text changes from Fried Rice to Iced Tea, Vue treats that <p> as a new element: the old one leaves, the new one enters. Without mode, both run at the same time. So for 300 ms there are two <p> in the DOM, and the layout jumps. <Transition name="fade" mode="out-in"> makes the old one finish leaving first, then the new one enters.
v-if and v-else inside one <Transition> are fine too, because only one of them renders.
TransitionGroup: the cart list
This is the cart list with <TransitionGroup>, then its CSS:
<TransitionGroup name="list" tag="ul">
<li v-for="item in cart" :key="item.id">{{ item.name }}</li>
</TransitionGroup>
.list-enter-active,
.list-leave-active {
transition: all 0.3s ease;
}
.list-enter-from,
.list-leave-to {
opacity: 0;
transform: translateX(30px);
}
.list-move {
transition: transform 0.3s ease;
}
.list-leave-active {
position: absolute;
}
tag="ul"renders a real<ul>around the items. Withouttag, there's no wrapper element. Attributes such asidon<TransitionGroup>fall through to that<ul>.- Every item needs a unique
:key. Without keys, in development mode the console writes[Vue warn]: <TransitionGroup> children must be keyed. - The
list-moveclass goes on items whose position shifts.position: absoluteonlist-leave-activelifts the leaving item out of the page's normal flow, so the other items can move up smoothly right away.
For example, the cart holds Fried Rice, Chicken Noodles and Iced Tea, then Fried Rice is removed. This table shows what you see, with and without keys:
:key |
What you see during those 300 ms |
|---|---|
item.id |
Fried Rice fades out to the right, Chicken Noodles and Iced Tea slide up with list-move |
| none | the first row turns into Chicken Noodles right away, the second into Iced Tea, and the third row fades out |
Without keys, Vue reuses elements by position, swaps their text, then removes the last element. The animation runs on the wrong row.
In the exercise editor
The first exercise is written as a whole .vue file, as in a Vite project: <script setup>, <template> and <style>. The CSS in <style> is applied to the preview too. The ---template--- format the Lab's Playground uses has no <style> block: a <style> in the template is silently dropped. That's why the Lab's Playground code adds its CSS with a script:
document.getElementById("transition-style")?.remove()
const style = document.createElement("style")
style.id = "transition-style"
style.textContent = ".fade-enter-active, .fade-leave-active { transition: opacity 0.3s } .fade-enter-from, .fade-leave-to { opacity: 0 }"
document.head.appendChild(style)
- In the first exercise,
<button id="toggle">Toggle</button>flipsshow(truebecomesfalse, and back), and<p id="animated">Animated!</p>shows whileshowistrue. That paragraph is wrapped in<Transition name="fade">, with a 0.3 second fade in<style>. Right after the click, the checker sees the paragraph still there with thefade-leave-activeclass, which means it's fading. To make sure the paragraph is gone in the end, the checker waits until the animations started by that click finish (2 seconds at most). - In the second exercise, the list starts with
"Item 1"and"Item 2", and<span id="total">holds2. Clicking#add-itemadds"Item 3"to the list, and the total becomes3. The list is written<TransitionGroup tag="ul" id="anim-list">with:key="item"on each<li>.
⚠️ Why does the element vanish right away, with no animation?
v-ifon the<Transition>, not on its element.<Transition v-if="show">removes the whole Transition, so nothing runs the leave animation, and the element vanishes right away. Putv-ifon the element inside.- Two elements in one
<Transition>. Only the first one renders. In development mode, the console writes[Vue warn]: <transition> can only be used on a single element or component. Use <transition-group> for lists.Usev-if/v-else, or<TransitionGroup>. - Class names that don't match.
name="fade"looks for.fade-enter-active. CSS named.v-enter-activeisn't used, and the element shows with no animation. - Texts and numbers in the exercises. In the first exercise, the button has to use
@click, and the element inside<Transition>appears and disappears throughv-iforv-showreading aref. In the second exercise, the new item is calledItem 3inside<TransitionGroup>, and#totalgoes from2to3.
TL;DR: <Transition name="fade"> wraps one element that appears and disappears through v-if, v-show or a changing :key. Vue adds the classes fade-enter-from, fade-enter-active, fade-enter-to when the element enters, and fade-leave-from, fade-leave-active, fade-leave-to when it leaves. Your CSS moves the element, and a leaving element is removed from the DOM only after its transition ends. mode="out-in" makes the old element leave before the new element enters. <TransitionGroup tag="ul"> animates every item of a list, always with a unique :key. The -move class (such as list-move) goes on items that change place, so that the move can be animated. Put v-if on the element, not on the Transition.
⚡ Transition: one element entering and leaving
Write:
<Transition name="fade">
<p v-if="toast" class="toast">{{ toast }}</p>
</Transition>
<style>
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
</style>
Meaning: <Transition> is a built-in Vue component that animates one element as it enters or leaves the page. Here the element is the <p>, a toast (a small message) that shows while toast holds text. Vue doesn't draw the animation itself. It adds a class at each stage, and your CSS does the moving. name="fade" sets the class prefix: fade-enter-from, fade-leave-to, and so on.
⚡ A leaving element is waited for
Guess first: toast is emptied. Is the <p> removed from the DOM right away?
Not yet. For about 300 ms, the <p> is still in the DOM with the class fade-leave-active, fading out. Vue reads the transition duration from the CSS, then waits.
Meaning: enter-from is the starting state when entering, enter-to its end state, and enter-active stays for the whole process. leave-from, leave-active and leave-to are their partners for leaving. Without CSS, the element appears and disappears instantly, with no error.
⚡ TransitionGroup for a list
Write:
<TransitionGroup name="list" tag="ul">
<li v-for="item in cart" :key="item.id">{{ item.name }}</li>
</TransitionGroup>
Meaning: <Transition> is for one element, and <TransitionGroup> for every item of a list, here the cart list. Every item needs a unique :key, and tag="ul" renders a real <ul> around the items.