Transition and TransitionGroup

Intermediate · 10 min

How Vue's <Transition> holds elements in the DOM and toggles enter/leave classes so CSS can animate v-if, plus out-in mode, JS hooks, and TransitionGroup.

Before thisConditional rendering with v-if and v-showList rendering with v-for

Net Ninja · 10 min

Transition and TransitionGroup

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Net NinjaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • CSS alone can't animate an element that `v-if` has just inserted or removed, because a removed element is gone before any transition can run. `<Transition>` fixes that by holding the element in the DOM and toggling classes at the right frames.
  • On enter Vue applies `v-enter-from`, `v-enter-active` and `v-enter-to`; on leave it applies `v-leave-from`, `v-leave-active` and `v-leave-to`. A `name` prop swaps the `v-` prefix, so `name="fade"` means `fade-enter-from` and so on.
  • `<Transition>` wraps exactly one element or single-root component, triggered by `v-if`, `v-show`, a dynamic `<component :is>`, or a changed `key`; `mode="out-in"` makes the old element finish leaving before the new one enters.
  • JavaScript hooks such as `@enter(el, done)` and `@leave(el, done)` let a library like GSAP drive the animation; with `:css="false"` Vue skips CSS detection and waits for you to call `done`.
  • `<TransitionGroup>` animates a `v-for` list: every child needs a unique `key`, classes apply to each item rather than the container, there are no modes, and a `*-move` class animates items sliding into their new positions.
  • Vue 3 renamed the starting classes from Vue 2's `v-enter` and `v-leave` to `v-enter-from` and `v-leave-from`, which is the quickest way to spot an outdated tutorial.

The problem: CSS can’t animate something that isn’t there

CSS transitions work by watching a property change on an element that stays in the document. If an element’s opacity goes from 1 to 0, the browser interpolates the frames in between. That covers hover effects and toggled classes well — the mechanics are in CSS animations and transitions.

v-if breaks that model in both directions. When the condition becomes true, the element is inserted already in its final state, so there is no “before” value to transition from. When it becomes false, the element is removed from the DOM in the same update, so there is nothing left to animate.

<script setup>
import { ref } from 'vue'
const open = ref(false)
</script>

<template>
  <button @click="open = !open">Toggle</button>
  <!-- appears and disappears instantly, no matter what CSS you add -->
  <p v-if="open" class="notice">Saved.</p>
</template>

What you need is someone to hold the element in the DOM a little longer on the way out, and to give it a starting state on the way in. That is the whole job of <Transition>.

What <Transition> actually does

<Transition> is built in, so there’s nothing to import or register. Wrap the conditional element in it:

<template>
  <button @click="open = !open">Toggle</button>
  <Transition name="fade">
    <p v-if="open" class="notice">Saved.</p>
  </Transition>
</template>

<style>
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.25s ease;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>

When the paragraph is inserted, Vue adds fade-enter-from and fade-enter-active before it hits the DOM, then one frame later swaps fade-enter-from for fade-enter-to. The browser sees opacity change from 0 to the default 1 while a transition is declared, and animates it. When the transition ends, Vue removes the classes.

Leaving runs the mirror image: fade-leave-from and fade-leave-active go on, one frame later fade-leave-from is swapped for fade-leave-to, and Vue waits for the transitionend (or animationend) event before it actually removes the element.

Think of it as a stage manager: the actor doesn’t vanish the instant the script says “exit”. The stage manager waits until they’ve walked off.

The six classes are:

Phase Start Whole phase End
Enter v-enter-from v-enter-active v-enter-to
Leave v-leave-from v-leave-active v-leave-to

Without a name, the prefix is v-. With name="fade", it’s fade-. The -active classes are where transition or animation goes, because they stay on for the entire phase. The -from/-to classes describe the hidden state. CSS @keyframes work too — put the animation on the -active class and Vue waits for animationend instead.

If you’ve read a Vue 2 tutorial, note that Vue 3 renamed v-enter to v-enter-from and v-leave to v-leave-from. A video using the old names is teaching Vue 2.

Rules and triggers

<Transition> wraps one element or component, and if it’s a component, that component must render a single root element. It reacts to four kinds of change:

  • v-if / v-else-if / v-else inserting or removing the child
  • v-show toggling display
  • a dynamic <component :is="..."> switching components
  • the child’s key changing, which forces Vue to treat it as a new element

The key trigger is handy for animating a value that changes in place, like a counter:

<Transition name="bump" mode="out-in">
  <span :key="count">{{ count }}</span>
</Transition>

Two props come up constantly:

  • mode="out-in" — when swapping between two elements, the old one finishes leaving before the new one enters. Without it both animate at once and briefly share the layout. in-out exists but is rarely what you want.
  • appear — also run the enter transition on the initial render, not only on later toggles.

JavaScript hooks

Sometimes CSS isn’t expressive enough — staggered sequences, physics, or a library like GSAP. <Transition> emits hooks at each stage:

<script setup>
import gsap from 'gsap'

function onBeforeEnter(el) {
  el.style.opacity = 0
  el.style.transform = 'translateY(12px)'
}
function onEnter(el, done) {
  gsap.to(el, { opacity: 1, y: 0, duration: 0.3, onComplete: done })
}
function onLeave(el, done) {
  gsap.to(el, { opacity: 0, duration: 0.2, onComplete: done })
}
</script>

<template>
  <Transition
    :css="false"
    @before-enter="onBeforeEnter"
    @enter="onEnter"
    @leave="onLeave"
  >
    <div v-if="open" class="card">...</div>
  </Transition>
</template>

The done callback is the contract: for enter and leave, Vue considers the transition finished when you call it. :css="false" tells Vue not to look for CSS transitions at all, which skips detection and stops any stray CSS rule from ending the transition early. The full set is before-enter, enter, after-enter, enter-cancelled, and the same four for leave.

To reuse an animation, wrap <Transition> in your own component that passes its slot through. Then <FadeIn> works anywhere without copying CSS.

Animating lists with <TransitionGroup>

<Transition> handles one child switching. A v-for list has many children entering, leaving and reordering independently — that’s <TransitionGroup>’s job:

<script setup>
import { ref } from 'vue'
const todos = ref([{ id: 1, text: 'Write lesson' }])
let nextId = 2
const add = () => todos.value.unshift({ id: nextId++, text: 'New task' })
const remove = (id) => (todos.value = todos.value.filter((t) => t.id !== id))
</script>

<template>
  <button @click="add">Add</button>
  <TransitionGroup name="list" tag="ul">
    <li v-for="todo in todos" :key="todo.id" @click="remove(todo.id)">
      {{ todo.text }}
    </li>
  </TransitionGroup>
</template>

<style>
.list-move,
.list-enter-active,
.list-leave-active {
  transition: all 0.3s ease;
}
.list-enter-from,
.list-leave-to {
  opacity: 0;
  transform: translateX(24px);
}
.list-leave-active {
  position: absolute;
}
</style>

The differences from <Transition> are short:

  • Every child needs a unique key — Vue has to know which item is which to animate it. This is the same key discipline as in list rendering with v-for.
  • Classes go on each item, not on the container. The tag prop picks a wrapper element if you want one; without it, no wrapper is rendered.
  • There are no modes, because items aren’t mutually exclusive.
  • The *-move class animates items that change position. Vue records each item’s position before the update, measures again after, applies an inverse transform, then lets the move class transition it back to zero — the FLIP technique. Setting position: absolute on leaving items takes them out of the flow so the others can start moving immediately.

What the video covers

The Net Ninja’s episode builds a toggle and walks through how <Transition> adds the enter and leave classes to an element, which is the core mechanism above, in Vue 3 class names. It is part of a series, so it assumes you know a basic CSS transition. It does not cover <TransitionGroup>; the LearnVue link below does that in five minutes, and the Vue School link shows wrapping a transition in a reusable component.

Where this fits

<Transition> turns up again around cached components and routed views: the next lesson, KeepAlive and cached components, is often nested inside one, and Teleport pairs with it for animated modals.

Check yourself

  1. 01Why does adding `transition: opacity 0.3s` to an element controlled by `v-if` not make it fade out when the condition turns false?

    When the condition turns false, `v-if` removes the element from the DOM in the same update, so there is no element left for the browser to transition. A CSS transition needs the element to exist while a property changes from one value to another. `<Transition>` solves this by keeping the element in the DOM, applying `v-leave-from` then `v-leave-to` so the property actually changes, and only removing the element once the transition has finished.

  2. 02You have `<Transition name="slide">` around a panel. Which class should hold the `transition` property and which should hold the hidden state?

    The `transition` declaration (duration, easing, which properties) goes on `slide-enter-active` and `slide-leave-active`, because those classes stay on the element for the whole phase. The hidden state, such as `opacity: 0` or `transform: translateX(100%)`, goes on `slide-enter-from` and `slide-leave-to`, since those describe where entering starts and where leaving ends.

  3. 03Two buttons swap with `v-if`/`v-else` inside a `<Transition>`, and for a moment both are visible and the layout jumps. What is the simplest fix?

    Add `mode="out-in"` to the `<Transition>`. By default the leaving and entering elements animate at the same time, so both sit in the layout together. With `out-in`, Vue waits for the old element's leave transition to finish before inserting the new one.

  4. 04In a `<TransitionGroup>` list, items fade in and out correctly but their neighbours snap into place instead of sliding. What is missing?

    A move transition. Add a `list-move` class (matching the group's `name`) with a `transition` on `transform`, and give `list-leave-active` `position: absolute` so leaving items drop out of the layout flow. Vue then measures each remaining item's old and new position and animates the difference.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark