0
0
Vueframework~20 mins

JavaScript hooks for transitions in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Transition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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>
AVue throws a runtime error because hooks cannot return Promises.
BVue ignores the Promise and starts the enter transition immediately.
CVue runs the enter transition in parallel with the Promise.
DVue waits for the Promise to resolve before starting the enter transition.
Attempts:
2 left
💡 Hint
Think about how Vue manages asynchronous hooks to control timing.
📝 Syntax
intermediate
1: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.
Afunction enter(done, el) { el.style.opacity = '1'; done(); }
Bfunction enter(el) { el.style.opacity = '1'; }
Cfunction enter(el, done) { el.style.opacity = '1'; done(); }
Dfunction enter(el, done) { el.style.opacity = '1'; return done; }
Attempts:
2 left
💡 Hint
The done callback is the second argument and must be called to finish the transition.
🔧 Debug
advanced
2: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>
AThe done callback is never called, so Vue waits indefinitely for transition end.
BThe opacity style is set incorrectly, so the transition does not start.
CThe <code>v-if</code> directive prevents the leave hook from running.
DVue requires the leave hook to return a Promise to finish.
Attempts:
2 left
💡 Hint
Check if the done callback is called to signal transition completion.
🧠 Conceptual
advanced
1:30remaining
What is the purpose of the @after-leave hook in Vue transitions?
Choose the best description of what the @after-leave hook does.
AIt runs only if the leave transition is cancelled.
BIt runs after the leave transition finishes and the element is removed from the DOM.
CIt runs during the leave transition to update styles dynamically.
DIt runs before the leave transition starts to prepare the element.
Attempts:
2 left
💡 Hint
Think about when cleanup or final actions happen after an element disappears.
state_output
expert
2: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>
A1
Bundefined (opacity style removed)
C0
D0.5
Attempts:
2 left
💡 Hint
Consider how CSS transitions change opacity from 0 to 1 over 0.3 seconds.