<Transition> component wraps a single element?Consider a Vue 3 component using the <Transition> component to wrap a single <div>. What is the visible effect when the wrapped element is conditionally rendered?
<template> <Transition name="fade"> <div v-if="show">Hello</div> </Transition> </template> <script setup> import { ref } from 'vue' const show = ref(true) </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter-from, .fade-leave-to { opacity: 0; } </style>
Think about how the CSS classes named with fade- affect opacity during enter and leave.
The <Transition> component applies CSS classes during the element's lifecycle. Here, the fade-enter-active and fade-leave-active classes define a half-second opacity transition. The fade-enter-from and fade-leave-to classes set opacity to 0 at start and end, causing a smooth fade effect.
<Transition> component with multiple elements?Vue's <Transition> component expects a single child element. Which code snippet correctly wraps multiple elements for transition?
Remember that <Transition> accepts only one root element. How can you group multiple elements without adding extra nodes?
Vue's <Transition> requires exactly one child element. Using a <template> as a wrapper allows grouping multiple elements without adding extra DOM nodes, which is the correct approach here.
isActive after the transition ends?Given this Vue component using <Transition>, what will be the value of isActive after the leave transition finishes?
<template> <Transition @after-leave="handleAfterLeave"> <div v-if="show">Content</div> </Transition> <button @click="show = false">Hide</button> </template> <script setup> import { ref } from 'vue' const show = ref(true) const isActive = ref(true) function handleAfterLeave() { isActive.value = false } </script>
Look at the event @after-leave and what the handler does.
The @after-leave event fires after the leave animation finishes. The handler sets isActive.value to false, so after the transition ends, isActive is false.
This Vue component uses <Transition> but the fade animation does not run when toggling show. What is the likely cause?
<template> <Transition name="fade"> <div v-show="show">Hello</div> </Transition> <button @click="show = !show">Toggle</button> </template> <script setup> import { ref } from 'vue' const show = ref(true) </script> <style> .fade-enter-active, .fade-leave-active { transition: opacity 0.5s; } .fade-enter-from, .fade-leave-to { opacity: 0; } </style>
Think about how v-show and v-if differ in DOM behavior.
v-show toggles the element's CSS display property without removing it from the DOM. The <Transition> component only triggers animations when elements enter or leave the DOM, which happens with v-if. So using v-show prevents the transition animation.
<Transition> and accessibility is true?When using Vue's <Transition> component for animations, which practice best supports accessibility?
Think about how animations affect keyboard focus and screen readers.
Good accessibility means managing focus and ARIA attributes so users relying on assistive tech understand content changes. Ensuring elements are focusable and ARIA attributes update during transitions helps screen readers and keyboard users follow the UI changes.