Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 87 ยท Lifecycle and Composition API

Transition Component

Learn Vue's Transition component - the six CSS classes for enter and leave, named transitions, appear, out-in mode, duration and JavaScript hooks - with runnable animation examples.

Elements that pop in and out of the page feel abrupt. When a v-if removes a panel, the panel is gone in one frame, and the user has to work out what changed. A short fade or slide tells the eye where something went. Doing that by hand is awkward, because an element that Vue removes from the DOM cannot be animated anymore: it is already gone.

Vue's built-in <Transition> component solves exactly that. It wraps a single element or component and, when that child enters or leaves the DOM, adds and removes CSS classes at the right moments, and waits for your animation to finish before it removes the element. You supply the CSS (or JavaScript); Vue supplies the timing. The same component works with v-if, v-show, dynamic components and changing keys.

The six transition classes

When the child enters or leaves, Vue applies six classes in a fixed order. The names start with v by default.

Class When it is applied
v-enter-from Before the element is inserted, removed one frame after insertion. The starting state of entering.
v-enter-active During the whole enter phase. Put transition or animation rules here.
v-enter-to Added one frame after insertion, removed when the transition ends. The final state of entering.
v-leave-from As soon as a leave starts, removed one frame later. The starting state of leaving.
v-leave-active During the whole leave phase.
v-leave-to Added one frame after the leave starts, removed when it ends. The final state of leaving.
flowchart LR
    subgraph Enter
      direction LR
      E1["v-enter-from<br/>v-enter-active"] --> E2["v-enter-to<br/>v-enter-active"] --> E3["classes removed"]
    end
    subgraph Leave
      direction LR
      L1["v-leave-from<br/>v-leave-active"] --> L2["v-leave-to<br/>v-leave-active"] --> L3["element removed"]
    end

One frame after the start state, Vue swaps the "from" class for the "to" class so the CSS transition has something to animate.

The pattern in CSS is always the same: describe the start/end look in the -from and -to classes and the how fast in the -active class. The browser does the in-between.

A first fade

Fade an element in and out

Press the button and the box fades over 0.4 seconds, then really leaves the DOM. Look at the CSS: opacity is 0 in -from (entering) and in -leave-to (leaving), and the default 1 everywhere else. The -active rule makes the property change take 0.4 seconds. Because the leaving element stays in the DOM until its transition ends, Vue needs no help from you to measure; it reads the transition or animation duration from the computed style.

<Transition> accepts one child element or component. To animate several items in a list, use TransitionGroup. A component inside <Transition> must also have a single root element; otherwise Vue cannot attach the classes and warns in development.

Names, appear and v-show

The default v- prefix is fine for one transition, but a real app has several: fades, slides, grows. Give each one a name and the classes change to match: name="slide" produces slide-enter-from, slide-enter-active and so on.

Two more props are common. appear runs the enter transition on the first render too, instead of showing the element immediately. And v-show works as well as v-if: the element stays in the DOM and Vue animates its display change, which is cheaper for things that toggle often.

Named transitions, appear and v-show

When the preview loads, the yellow banner fades in by itself because of appear. The "Toggle slide" button hides and shows the green note with a slide, and Vue only toggles its display (the element stays in the DOM). The last button shows a trick: changing the key makes Vue treat the paragraph as a new element, so the old one leaves and the new one enters. That is the simplest way to animate a value that changes in place.

Modes: what happens when one element replaces another

When two elements swap inside one <Transition> (a v-if/v-else pair or a dynamic component), by default the leaving and the entering transitions run at the same time. Both are in the DOM for a moment, and in normal flow they stack on top of each other, so the layout jumps. The mode prop controls the order:

  • out-in: the current element leaves first, then the new one enters. This is the one you want most of the time.
  • in-out: the new element enters first, then the current one leaves. Rare.
Switching tabs with mode out-in

Click through the tabs: the old panel slides out to the left, then the new one slides in from the right, one after the other and without the layout jumping. Remove mode="out-in" and compare: both panels are momentarily on screen at once.

Notice that the two directions use different "to/from" rules. The enter transition starts from page-enter-from (right) and ends at the natural position, and the leave transition ends at page-leave-to (left). The result looks like one motion in one direction.

Duration, types and custom classes

Vue works out how long to wait by reading your CSS. A few props refine this:

  • duration sets an explicit time in milliseconds when Vue's guess is wrong: :duration="550" or :duration="{ enter: 800, leave: 400 }".
  • type ("transition" or "animation") tells Vue which one to wait for when the element has both.
  • enter-active-class, leave-active-class and the other four props let you give your own class names, which is how you connect an animation library that ships ready-made classes.
  • :css="false" tells Vue not to look at CSS at all; you will use it with JavaScript hooks below.

You may also use CSS keyframe animations instead of transitions: put animation: bounce 0.5s on .v-enter-active and the reverse on .v-leave-active. The -from and -to classes are optional in that case.

JavaScript hooks

Sometimes CSS is not enough: you want to drive an animation with the Web Animations API, a library, or a computed value. <Transition> emits events at each stage. The enter and leave hooks receive a done callback; with :css="false" Vue waits for you to call it.

flowchart LR
    BE["before-enter"] --> EN["enter(el, done)"] --> AE["after-enter"]
    BL["before-leave"] --> LV["leave(el, done)"] --> AL["after-leave"]

Each phase has a "before", an action hook that signals completion with done, and an "after".

Transition hooks with the Web Animations API

Press "Toggle" twice. The log shows before-enter, enter, after-enter after the first press and before-leave, leave, after-leave after the second. The after- hook only fires once done() has been called, which happens when the browser reports the animation as finished. If you forget to call done in a hook with :css="false", the transition never completes and a leaving element would stay in the DOM forever.

Respect users who prefer less motion. Wrap decorative transitions in a media query such as @media (prefers-reduced-motion: reduce) { .fade-enter-active { transition: none; } }. The same idea applies to hook-driven animations: check window.matchMedia("(prefers-reduced-motion: reduce)") first.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the v-enter-active class represent?

    v-enter-active is applied for the whole enter phase. The -from and -to classes describe the start and end look, and -active says how the change is animated.
  2. 2. You set name="slide" on a Transition. Which class does Vue apply while entering?

    The name replaces the default v prefix: slide-enter-from, slide-enter-active, slide-enter-to, slide-leave-from, slide-leave-active and slide-leave-to.
  3. 3. Two tabs swap inside one Transition and the layout jumps because both are visible at once. What fixes it?

    By default leave and enter run simultaneously. mode="out-in" lets the old element leave first and then the new one enter.
  4. 4. With :css="false" and an enter hook that takes (el, done), what must the hook do?

    With CSS detection turned off, Vue relies on you to call done(). Without it the transition never completes.
  5. 5. How can you replay an enter/leave transition for a value that changes in place, like a counter?

    A new key makes Vue treat the element as a different one, so the old element leaves and the new one enters.

Summary

  • <Transition> wraps a single element or component and applies six CSS classes (-enter-from, -enter-active, -enter-to and the leave equivalents) while it enters or leaves the DOM.
  • You write the CSS: -from/-to describe the look, -active carries the transition or animation; Vue waits for it to end before removing the element.
  • name changes the class prefix, appear animates the first render, and v-show, dynamic components and changing key values all trigger transitions.
  • mode="out-in" runs the leave before the enter and avoids layout jumps when swapping content.
  • duration, type and the custom class props fine-tune timing and let you use animation libraries.
  • With :css="false", hooks such as @enter and @leave drive animations from JavaScript and must call done().

Next, animate whole lists as items are added, removed and reordered: TransitionGroup and list animation.