Tutorials Logic, IN info@tutorialslogic.com

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

Vue Transitions Animations Transition Component

Vue in Vue is best learned by connecting the rule to a small admin screen. Start with the smallest component or composable, observe the output, and then add one realistic constraint so the concept becomes practical.

The key habit for this lesson is to watch ref, prop, slot, or emitted event as it changes. That makes the topic easier to debug, easier to explain in interviews, and easier to use in real code without memorizing isolated syntax.

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.

Class When applied
v-enter-from Start state of enter - added before element is inserted
v-enter-active Active state of enter - applied during entire enter phase
v-enter-to End state of enter - added after element is inserted
v-leave-from Start state of leave
v-leave-active Active state of leave
v-leave-to End state of leave

Transition, TransitionGroup, CSS Animations

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>

The <Transition> Component

The <Transition> Component
<!-- 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>

The <Transition> Component

The <Transition> Component
<!-- 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

  • Build a small demo focused only on Transitions.
  • Add one edge case (empty/loading/error) and handle it cleanly.
  • Refactor repeated logic into a reusable function/composable.
  • Add clear comments only where logic is non-obvious.
  • Verify behavior with manual testing and Vue Devtools.

Applied guide for Vue

Use Vue when the program needs a clear answer to a specific problem, not because the keyword looks familiar. In a real Vue task, first name the input, then name the transformation, then name the output. This small discipline shows whether the topic is being used correctly or only copied from an example.

A reliable practice flow is: create the smallest working component or composable, add one normal case, add one edge case such as missing, repeated, empty, or boundary input, and then confirm the result with Vue Devtools and template warnings. If the result surprises you, reduce the code until the behavior is visible again.

The most common trap here is copying the syntax before understanding the behavior. Avoid it by writing one sentence before the code that explains why Vue is the right choice. After the code runs, verify the lesson by doing this: change one input and explain the changed output.

  • Identify the exact problem solved by Vue.
  • Trace ref, prop, slot, or emitted event before and after the main operation.
  • Keep one intentionally broken version and explain the fix.
  • Connect the example to a small admin screen so the idea feels concrete.
Key Takeaways
  • I can explain where Vue fits inside a small admin screen.
  • I can point to the exact ref, prop, slot, or emitted event affected by this topic.
  • I tested a normal case and an edge case involving missing, repeated, empty, or boundary input.
  • I verified the result with Vue Devtools and template warnings instead of assuming it worked.
  • I can describe the main mistake: copying the syntax before understanding the behavior.
Common Mistakes to Avoid
WRONG Copying the syntax before understanding the behavior.
RIGHT Write the expected behavior first, then make the example prove it.
A one-line expectation turns the code from copied syntax into a testable idea.
WRONG Practicing only the perfect input.
RIGHT Also test missing, repeated, empty, or boundary input before considering the lesson complete.
The edge case is where most interview follow-up questions begin.
WRONG Looking only at the final output.
RIGHT Trace ref, prop, slot, or emitted event through each important step.
Tracing makes debugging faster because you can see the first incorrect state.

Practice Tasks

  • Build one small component or composable that demonstrates Vue in a small admin screen.
  • Change the example to include missing, repeated, empty, or boundary input and record the difference.
  • Break the example by deliberately copying the syntax before understanding the behavior, then write the corrected version.
  • Explain the finished example in five bullet points: input, operation, output, failure case, and verification.

Frequently Asked Questions

Use it when the problem matches the behavior shown in the example and when the result can be verified through Vue Devtools and template warnings.

Start with a tiny case, then test missing, repeated, empty, or boundary input. The main warning sign is copying the syntax before understanding the behavior.

Trace ref, prop, slot, or emitted event, predict the result, run the example, and compare your prediction with the actual output.

Ready to Level Up Your Skills?

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