Consider a Vue 3 component that toggles between two transition names based on a button click. What will be the visible effect on the element's animation when the transition name changes?
<template> <button @click="toggle">Toggle Transition</button> <transition :name="transitionName"> <p v-if="show">Hello Vue!</p> </transition> </template> <script setup> import { ref } from 'vue' const show = ref(true) const transitionName = ref('fade') function toggle() { show.value = !show.value transitionName.value = transitionName.value === 'fade' ? 'slide' : 'fade' } </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter-from, .fade-leave-to { opacity: 0; } .slide-enter-active, .slide-leave-active { transition: transform 0.5s; } .slide-enter-from { transform: translateX(-100%); } .slide-leave-to { transform: translateX(100%); } </style>
Think about how Vue applies transition classes based on the name attribute binding.
Vue applies transition classes dynamically based on the current value of the name prop. Changing the transition name causes Vue to apply different CSS classes for enter and leave animations, so the element animates with the new transition style each time it appears or disappears.
Given a Vue 3 template, which syntax correctly binds a dynamic transition name stored in a variable currentTransition?
<template>
<transition ???>
<div v-if="visible">Content</div>
</transition>
</template>Remember how to bind props dynamically in Vue templates.
In Vue templates, dynamic attribute binding uses the : shorthand (C) or full v-bind:name="currentTransition" (B). Option A incorrectly uses text interpolation {{}} for an attribute, and D binds the literal string "currentTransition".
Given this Vue 3 component, the element appears and disappears instantly without animation. What is the cause?
<template> <transition name="fade"> <p v-show="visible">Fade me</p> </transition> <button @click="visible = !visible">Toggle</button> </template> <script setup> import { ref } from 'vue' const visible = ref(true) </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter-from, .fade-leave-to { opacity: 0; } </style>
Check how refs are updated from template event handlers in Vue 3 <script setup>.
In Vue 3 <script setup>, you cannot mutate a ref directly in a template expression like @click="visible = !visible" because the left-hand visible does not refer to visible.value. The ref.value never changes, so visibility does not toggle and no transition triggers. Fix by defining const toggle = () => { visible.value = !visible.value } and using @click="toggle".
In Vue 3, when a transition with name 'slide' finishes entering, which classes remain on the element?
<style>
.slide-enter-active { transition: transform 0.3s; }
.slide-enter-from { transform: translateX(-100%); }
.slide-enter-to { transform: translateX(0); }
</style>
<template>
<transition name="slide">
<div v-if="show">Slide me</div>
</transition>
</template>
<script setup>
import { ref, onMounted } from 'vue'
const show = ref(false)
onMounted(() => { show.value = true })
</script>Recall the sequence of classes Vue applies during enter transitions.
During enter: Vue adds slide-enter-active and slide-enter-from, then replaces slide-enter-from with slide-enter-to. When the transition finishes (transitionend event), Vue removes slide-enter-active and slide-enter-to, leaving no transition classes.
In a Vue 3 app, you have nested <transition> components with dynamic names that change based on user interaction. What is the key behavior Vue uses to manage these nested dynamic transitions correctly?
Think about how Vue manages multiple transition components in the virtual DOM.
Vue treats each <transition> component as an independent instance. It applies CSS classes based on each transition's current name prop, even if nested. This allows nested dynamic transitions to work correctly and independently.