Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Vue Transitions Animations Transition Component: Tutorial, Examples, FAQs & Interview Tips

The <Transition> Component

Vue's built-in <Transition> component applies enter/leave animations to a single element or component. It automatically adds CSS classes at the right moments, or you can use JavaScript hooks for full control.

ClassWhen applied
v-enter-fromStart state of enter - added before element is inserted
v-enter-activeActive state of enter - applied during entire enter phase
v-enter-toEnd state of enter - added after element is inserted
v-leave-fromStart state of leave
v-leave-activeActive state of leave
v-leave-toEnd state of leave
Transition, TransitionGroup, CSS Animations
<template>
  <div>
    <button @click="show = !show">Toggle</button>

    <!-- Basic fade transition -->
    <Transition name="fade">
      <p v-if="show">Hello, I fade in and out!</p>
    </Transition>

    <!-- Slide transition -->
    <Transition name="slide">
      <div v-if="show" class="panel">Sliding panel</div>
    </Transition>

    <!-- Mode: out-in (leave first, then enter) -->
    <Transition name="fade" mode="out-in">
      <component :is="currentView" :key="currentView" />
    </Transition>

    <!-- appear: animate on initial render -->
    <Transition name="fade" appear>
      <p>I animate when the page loads</p>
    </Transition>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(true)
const currentView = ref('HomeView')
</script>

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

/* Slide transition */
.slide-enter-active {
  transition: all 0.3s ease-out;
}
.slide-leave-active {
  transition: all 0.3s ease-in;
}
.slide-enter-from {
  transform: translateX(-100%);
  opacity: 0;
}
.slide-leave-to {
  transform: translateX(100%);
  opacity: 0;
}

/* Scale + fade */
.scale-enter-active,
.scale-leave-active {
  transition: all 0.25s ease;
}
.scale-enter-from,
.scale-leave-to {
  transform: scale(0.9);
  opacity: 0;
}
</style>
<!-- TransitionGroup - animate lists -->
<template>
  <div>
    <input v-model="newItem" @keyup.enter="addItem" placeholder="Add item" />

    <TransitionGroup name="list" tag="ul">
      <li v-for="item in items" :key="item.id">
        {{ item.text }}
        <button @click="removeItem(item.id)">x</button>
      </li>
    </TransitionGroup>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const newItem = ref('')
const items = ref([
  { id: 1, text: 'Learn Vue' },
  { id: 2, text: 'Build something' },
])

function addItem() {
  if (!newItem.value.trim()) return
  items.value.push({ id: Date.now(), text: newItem.value })
  newItem.value = ''
}

function removeItem(id) {
  items.value = items.value.filter(i => i.id !== id)
}
</script>

<style>
/* List item enter/leave */
.list-enter-active,
.list-leave-active {
  transition: all 0.4s ease;
}
.list-enter-from {
  opacity: 0;
  transform: translateX(-30px);
}
.list-leave-to {
  opacity: 0;
  transform: translateX(30px);
}

/* Smooth repositioning of remaining items */
.list-move {
  transition: transform 0.4s ease;
}

/* Prevent layout shift during leave */
.list-leave-active {
  position: absolute;
}
</style>
<!-- JavaScript hooks - full control with GSAP or Web Animations API -->
<template>
  <Transition
    @before-enter="onBeforeEnter"
    @enter="onEnter"
    @after-enter="onAfterEnter"
    @enter-cancelled="onEnterCancelled"
    @before-leave="onBeforeLeave"
    @leave="onLeave"
    @after-leave="onAfterLeave"
    :css="false"
  >
    <div v-if="show" class="box">Animated box</div>
  </Transition>
</template>

<script setup>
import { ref } from 'vue'

const show = ref(true)

// :css="false" - disable CSS transitions, use JS only

function onBeforeEnter(el) {
  el.style.opacity = '0'
  el.style.transform = 'scale(0.8)'
}

function onEnter(el, done) {
  // Use Web Animations API
  el.animate([
    { opacity: 0, transform: 'scale(0.8)' },
    { opacity: 1, transform: 'scale(1)' }
  ], {
    duration: 400,
    easing: 'ease-out'
  }).onfinish = done  // call done() when animation completes
}

function onLeave(el, done) {
  el.animate([
    { opacity: 1, transform: 'scale(1)' },
    { opacity: 0, transform: 'scale(0.8)' }
  ], {
    duration: 300,
    easing: 'ease-in'
  }).onfinish = done
}

function onAfterEnter(el) { console.log('Enter complete') }
function onAfterLeave(el) { console.log('Leave complete') }
function onBeforeLeave(el) { /* ... */ }
function onEnterCancelled(el) { /* ... */ }
</script>

Deep Dive: Transitions in Real Projects

Understanding Transitions is not just about syntax. In production applications, this topic directly affects maintainability, debugging speed, and team collaboration. Focus on readability, small reusable patterns, and predictable state flow when implementing Transitions.

A practical approach is to first implement the simplest working version, then refactor into reusable pieces (components/composables/stores) only when duplication appears. This helps keep your Vue codebase clean while avoiding over-engineering.

Common Mistakes to Avoid

  • Mixing too many responsibilities in one component instead of separating logic by concern.
  • Skipping meaningful naming for variables, emits, and component props.
  • Ignoring edge cases like empty data, loading states, and error handling.
  • Optimizing too early before measuring real bottlenecks in browser devtools.
  • Not creating small test scenarios to validate behavior after each change.

Mini Practice Checklist

  1. Build a small demo focused only on Transitions.
  2. Add one edge case (empty/loading/error) and handle it cleanly.
  3. Refactor repeated logic into a reusable function/composable.
  4. Add clear comments only where logic is non-obvious.
  5. Verify behavior with manual testing and Vue Devtools.
Key Takeaways
  • This chapter on Transitions focuses on practical Vue 3 patterns used in real projects.
  • Prefer the Composition API with script setup for cleaner and more scalable component logic.
  • Keep components focused and move reusable logic into composables when complexity grows.
  • Use Vue Devtools to inspect component state, props, emits, and performance during development.
  • Write small experiments for each concept before applying it in production code.
  • After finishing this chapter, continue to the next related topic in the Vue roadmap.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.