Vue3 Fundamentals ยท Built-in Components

Animate an element with Transition

Animating enter and leave with Transition

Animate an element with Transition

When an element appears or disappears, it snaps in and out. A little motion makes the change easier to follow. Vue gives you <Transition> for exactly that.

Wrap a single element in <Transition> and Vue adds CSS classes around the moment it enters or leaves. You write the CSS; Vue times the classes.

<template>
  <Transition name="fade">
    <p v-if="visible">Now you see me</p>
  </Transition>
</template>

The name prop prefixes the classes: fade-enter-from, fade-enter-active, fade-enter-to, and the matching fade-leave-* set. Add appear and the entering element is animated on the first render too, not only on later toggles.

<Transition> takes exactly one child. That child is usually driven by v-if, v-show, or a dynamic component, so there is something to enter and leave.

The Test mounts your component and checks the element is wrapped in a fade transition. The starter renders the element on its own, so no transition wraps it and the Test fails.

Challenge

The element pops in with no motion. Wrap it in a transition: export an App component from App.vue whose <p> sits inside <Transition name="fade">.

The Test mounts your component and checks the element is wrapped in a fade transition. The starter renders the paragraph on its own, so the Test fails.

App.vue
<script setup>
import { ref } from 'vue';

const visible = ref(true);
</script>

<template>
  <p v-if="visible" data-test="box">Now you see me</p>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge โ€” you only see the result, never the test source.