Challenge - 5 Problems
Vue Transition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when the
@before-enter hook returns a Promise?In Vue 3 transitions, if the
@before-enter hook returns a Promise, how does Vue handle the transition?Vue
<template>
<transition
@before-enter="beforeEnter"
@enter="enter"
@leave="leave"
>
<div v-if="show">Hello</div>
</transition>
</template>
<script setup>
import { ref } from 'vue'
const show = ref(true)
function beforeEnter(el) {
return new Promise(resolve => {
setTimeout(() => {
el.style.opacity = '0'
resolve()
}, 100)
})
}
function enter(el, done) {
el.style.transition = 'opacity 0.5s'
el.style.opacity = '1'
setTimeout(done, 500)
}
function leave(el, done) {
el.style.transition = 'opacity 0.5s'
el.style.opacity = '0'
setTimeout(done, 500)
}
</script>Attempts:
2 left
💡 Hint
Think about how Vue manages asynchronous hooks to control timing.
✗ Incorrect
Vue waits for the Promise returned by @before-enter to resolve before starting the enter transition. This allows asynchronous setup before the animation begins.
📝 Syntax
intermediate1:30remaining
Which option correctly defines a
@enter hook with a done callback?Select the correct syntax for an
@enter hook that uses the done callback to signal transition end.Attempts:
2 left
💡 Hint
The done callback is the second argument and must be called to finish the transition.
✗ Incorrect
The @enter hook receives the element as the first argument and the done callback as the second. Calling done() signals the end of the transition.
🔧 Debug
advanced2:00remaining
Why does the leave transition never finish in this code?
Given the following leave hook, why does the transition never complete?
Vue
<template> <transition @leave="leave"> <div v-if="show">Bye</div> </transition> </template> <script setup> import { ref } from 'vue' const show = ref(true) function leave(el, done) { el.style.transition = 'opacity 1s' el.style.opacity = '0' // done() is never called } </script>
Attempts:
2 left
💡 Hint
Check if the done callback is called to signal transition completion.
✗ Incorrect
Vue waits for the done() callback to be called to know when the leave transition finishes. If it is never called, Vue waits forever.
🧠 Conceptual
advanced1:30remaining
What is the purpose of the
@after-leave hook in Vue transitions?Choose the best description of what the
@after-leave hook does.Attempts:
2 left
💡 Hint
Think about when cleanup or final actions happen after an element disappears.
✗ Incorrect
The @after-leave hook runs after the leave transition completes and the element is removed from the DOM. It is useful for cleanup.
❓ state_output
expert2:30remaining
What is the final opacity of the element after this enter transition completes?
Given this enter hook, what will be the element's opacity after the transition ends?
Vue
<template> <transition @enter="enter"> <div v-if="show">Fade In</div> </transition> </template> <script setup> import { ref } from 'vue' const show = ref(true) function enter(el, done) { el.style.opacity = '0' el.style.transition = 'opacity 0.3s ease-in' requestAnimationFrame(() => { el.style.opacity = '1' }) setTimeout(() => { done() }, 300) } </script>
Attempts:
2 left
💡 Hint
Consider how CSS transitions change opacity from 0 to 1 over 0.3 seconds.
✗ Incorrect
The element starts with opacity 0, then transitions to opacity 1 over 0.3 seconds. After the transition, opacity remains 1.