Bird
Raised Fist0
Vueframework~20 mins

Dynamic transitions in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Dynamic Transitions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when the transition name changes dynamically?

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?

Vue
<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>
AThe element will not animate at all because dynamic transition names are not supported in Vue.
BThe element will always use the initial transition name and ignore changes to the transitionName variable.
CThe element will animate with the current transition name each time it appears or disappears, switching between fade and slide animations.
DThe element will animate with both fade and slide transitions simultaneously, causing a mixed effect.
Attempts:
2 left
💡 Hint

Think about how Vue applies transition classes based on the name attribute binding.

📝 Syntax
intermediate
1:30remaining
Which option correctly binds a dynamic transition name in Vue 3?

Given a Vue 3 template, which syntax correctly binds a dynamic transition name stored in a variable currentTransition?

Vue
<template>
  <transition ???>
    <div v-if="visible">Content</div>
  </transition>
</template>
A:name="currentTransition"
Bname="{{ currentTransition }}"
Cv-bind:name="currentTransition"
Dname="currentTransition"
Attempts:
2 left
💡 Hint

Remember how to bind props dynamically in Vue templates.

🔧 Debug
advanced
2:30remaining
Why does the transition not animate when toggling visibility?

Given this Vue 3 component, the element appears and disappears instantly without animation. What is the cause?

Vue
<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>
AThe transition name 'fade' is not defined in the CSS, so no animation occurs.
BThe button click does not update the visible variable correctly, so no animation triggers.
CUsing v-show instead of v-if prevents the transition from triggering enter and leave animations.
DThe transition component must wrap a single root element, but <p> is not allowed.
Attempts:
2 left
💡 Hint

Check how refs are updated from template event handlers in Vue 3 <script setup>.

❓ state_output
advanced
2:00remaining
What is the final class list on the element after transition enter finishes?

In Vue 3, when a transition with name 'slide' finishes entering, which classes remain on the element?

Vue
<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>
ANo transition classes remain after enter finishes
Bslide-enter-active and slide-enter-to
Cslide-enter-from only
Dslide-enter-active only
Attempts:
2 left
💡 Hint

Recall the sequence of classes Vue applies during enter transitions.

🧠 Conceptual
expert
3:00remaining
How does Vue 3 handle dynamic transition components with multiple nested transitions?

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?

AVue requires manual event listeners to synchronize nested transitions when using dynamic names.
BVue merges all nested transitions into a single transition using the outermost transition name, ignoring inner dynamic names.
CVue only applies transitions to the outermost component and disables inner nested transitions to avoid conflicts.
DVue tracks each transition instance separately and applies the correct CSS classes based on the current dynamic name for each nested transition.
Attempts:
2 left
💡 Hint

Think about how Vue manages multiple transition components in the virtual DOM.

Practice

(1/5)
1. What does the :name attribute in Vue's <transition> component control?
easy
A. The CSS class name used for the transition animation
B. The HTML tag used for the transition wrapper
C. The JavaScript function called during transition
D. The duration of the transition in milliseconds

Solution

  1. Step 1: Understand the role of :name in <transition>

    The :name attribute sets the base CSS class name for the transition states like enter and leave.
  2. Step 2: Connect :name to CSS animation classes

    Vue uses this name to apply classes such as {name}-enter-active and {name}-leave-active to trigger CSS animations.
  3. Final Answer:

    The CSS class name used for the transition animation -> Option A
  4. Quick Check:

    Transition :name = CSS class base name [OK]
Hint: Remember :name sets CSS classes for animations [OK]
Common Mistakes:
  • Confusing :name with HTML tag or JS function
  • Thinking :name controls timing instead of class names
  • Assuming :name is optional for dynamic transitions
2. Which of the following is the correct syntax to bind a dynamic transition name in Vue?
easy
A. <transition v-bind:name>
B. <transition :name="fade">
C. <transition name="fade">
D. <transition :name="transitionName">

Solution

  1. Step 1: Identify dynamic binding syntax in Vue

    Dynamic binding uses : or v-bind: with a JavaScript expression inside quotes.
  2. Step 2: Check which option uses a variable for dynamic name

    <transition :name="transitionName"> uses :name="transitionName", correctly binding a variable named transitionName.
  3. Final Answer:

    <transition :name="transitionName"> -> Option D
  4. Quick Check:

    Dynamic binding = :name="variable" [OK]
Hint: Use :name="variable" for dynamic transition names [OK]
Common Mistakes:
  • Using static string without binding for dynamic names
  • Missing quotes around variable name
  • Using :name without a value
3. Given this Vue template snippet:
<template>
  <transition :name="animation">
    <div v-if="show">Hello</div>
  </transition>
</template>

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

What happens if you change animation.value from 'fade' to 'slide' while show is true?
medium
A. The div will animate with the 'slide' transition on next toggle
B. The div immediately animates with the 'slide' transition without toggling
C. No animation occurs until show changes
D. Vue throws an error because transition name changed dynamically

Solution

  1. Step 1: Understand when transitions trigger

    Transitions animate when elements enter or leave the DOM, triggered by v-if changes.
  2. Step 2: Changing transition name while element is visible

    Changing animation.value alone does not trigger enter/leave, so no animation runs immediately.
  3. Final Answer:

    No animation occurs until show changes -> Option C
  4. Quick Check:

    Transition triggers on DOM changes, not name changes [OK]
Hint: Transitions run on element enter/leave, not on name change alone [OK]
Common Mistakes:
  • Thinking transition changes animate immediately
  • Assuming Vue errors on dynamic name changes
  • Confusing v-if toggling with reactive variable changes
4. Consider this Vue code snippet:
<transition :name="currentTransition">
  <div v-if="visible">Content</div>
</transition>

<script setup>
import { ref } from 'vue'
const visible = ref(true)
const currentTransition = ref('fade')

// Later in code
currentTransition = 'slide'
</script>

What is the main problem in this code?
medium
A. Directly assigning a new string to a ref variable instead of using .value
B. Using v-if inside <transition> is not allowed
C. Transition names cannot be changed dynamically
D. Missing quotes around transition names

Solution

  1. Step 1: Identify how to update Vue refs

    Refs are reactive objects; to update their value, use .value, not direct assignment.
  2. Step 2: Spot incorrect assignment

    The code does currentTransition = 'slide' which replaces the ref instead of updating its value.
  3. Final Answer:

    Directly assigning a new string to a ref variable instead of using .value -> Option A
  4. Quick Check:

    Update ref with .value, not direct assignment [OK]
Hint: Update ref variables with .value, not direct assignment [OK]
Common Mistakes:
  • Assigning new value directly to ref variable
  • Thinking v-if cannot be used inside transition
  • Believing transition names are static only
5. You want to create a Vue component that switches between 'fade' and 'slide' transitions dynamically based on a button click. Which approach correctly updates the transition and triggers the animation each time the button toggles visibility?
hard
A. Change the transition name ref only without toggling visibility
B. Use a ref for transition name and toggle show to trigger enter/leave animations
C. Use a computed property for transition name but keep show always true
D. Apply both transitions simultaneously by listing names in :name

Solution

  1. Step 1: Understand how transitions trigger in Vue

    Transitions animate when elements enter or leave the DOM, controlled by v-if or similar directives.
  2. Step 2: Combine dynamic transition name with toggling visibility

    Changing the transition name alone won't animate; toggling show triggers the animation with the current transition name.
  3. Step 3: Confirm correct approach

    Using a ref for transition name and toggling show ensures the element animates with the chosen transition each toggle.
  4. Final Answer:

    Use a ref for transition name and toggle show to trigger enter/leave animations -> Option B
  5. Quick Check:

    Toggle visibility + dynamic name = dynamic transitions [OK]
Hint: Toggle element visibility to trigger dynamic transitions [OK]
Common Mistakes:
  • Changing transition name without toggling visibility
  • Trying to apply multiple transitions at once
  • Keeping element always visible expecting animation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard