Implementing Vue Transition Enter/Leave Hooks
This challenge focuses on understanding and implementing Vue's built-in transition enter and leave hooks. These hooks are crucial for animating elements as they are added to or removed from the DOM, providing a smoother user experience. You will be tasked with creating a component that visually demonstrates these hooks.
Problem Description
Your goal is to create a Vue component that allows a user to toggle the visibility of a specific element. When the element is added to or removed from the DOM, you need to implement custom animations using Vue's transition component and its enter/leave hooks. Specifically, you will use the enter-from-class, enter-to-class, leave-from-class, and leave-to-class attributes, along with corresponding CSS classes, to control the animation.
Key Requirements:
- Create a Vue component that contains a button to toggle the visibility of a target element.
- Wrap the target element in a Vue
<transition>component. - Apply CSS classes to the target element to define its appearance before and after entering/leaving the DOM.
- Use the following
transitionattributes on the<transition>component:enter-from-class: The starting CSS class for enter animations.enter-to-class: The ending CSS class for enter animations.leave-from-class: The starting CSS class for leave animations.leave-to-class: The ending CSS class for leave animations.
- Define the CSS rules for these classes to create distinct enter and leave animations.
- The target element should have a clear visual change during the enter and leave transitions.
Expected Behavior:
When the toggle button is clicked:
- If the target element is not visible, it should animate into view using the defined enter transition.
- If the target element is visible, it should animate out of view using the defined leave transition.
Edge Cases:
- Ensure the animations are smooth and don't cause layout shifts or abrupt visual changes.
- Consider the initial state of the element when it's first rendered (i.e., not yet in the DOM).
Examples
Example 1: Basic Fade Transition
<template>
<div>
<button @click="showElement = !showElement">Toggle Element</button>
<transition
enter-from-class="fade-enter-from"
enter-to-class="fade-enter-to"
leave-from-class="fade-leave-from"
leave-to-class="fade-leave-to"
>
<div v-if="showElement" class="target-element">
This element fades in and out.
</div>
</transition>
</div>
</template>
<style>
.target-element {
width: 200px;
height: 100px;
background-color: lightblue;
margin-top: 20px;
display: flex;
justify-content: center;
align-items: center;
}
/* Enter Transition Classes */
.fade-enter-from {
opacity: 0;
}
.fade-enter-to {
opacity: 1;
}
/* Leave Transition Classes */
.fade-leave-from {
opacity: 1;
}
.fade-leave-to {
opacity: 0;
}
</style>
Explanation:
When showElement becomes true, the target-element is added to the DOM. The <transition> component applies fade-enter-from initially and then fade-enter-to after a short delay (managed by Vue's default transition duration or explicitly set in CSS). This creates a fade-in effect. When showElement becomes false, the target-element is removed. The <transition> component applies fade-leave-from initially and then fade-leave-to, creating a fade-out effect.
Example 2: Slide and Fade Transition
<template>
<div>
<button @click="isVisible = !isVisible">Toggle Content</button>
<transition
enter-from-class="slide-fade-enter-from"
enter-to-class="slide-fade-enter-to"
leave-from-class="slide-fade-leave-from"
leave-to-class="slide-fade-leave-to"
>
<div v-if="isVisible" class="content-box">
Sliding and Fading Content
</div>
</transition>
</div>
</template>
<style>
.content-box {
width: 250px;
padding: 20px;
background-color: lightgreen;
border: 1px solid darkgreen;
margin-top: 20px;
text-align: center;
}
/* Enter Transition Classes */
.slide-fade-enter-from {
transform: translateX(-20px);
opacity: 0;
}
.slide-fade-enter-to {
transform: translateX(0);
opacity: 1;
}
/* Leave Transition Classes */
.slide-fade-leave-from {
transform: translateX(0);
opacity: 1;
}
.slide-fade-leave-to {
transform: translateX(20px);
opacity: 0;
}
</style>
Explanation:
This example combines a slide and fade effect. The enter-from-class moves the element 20px to the left and makes it invisible. The enter-to-class brings it back to its original position with full opacity. Conversely, the leave-from-class starts at the element's normal state, and leave-to-class slides it 20px to the right while fading it out.
Constraints
- The Vue component must be written in TypeScript.
- The animation duration for both enter and leave transitions should be between 0.3 seconds and 1 second. This can be controlled via CSS
transition-duration. - The target element must be a
div. - The toggle mechanism must be a button.
Notes
- You'll need to define your own custom CSS classes for the
enter-from-class,enter-to-class,leave-from-class, andleave-to-classattributes. - Remember that the
transitioncomponent manages the application and removal of these classes. - Consider using CSS
transitionproperties to define the animation's duration, timing function, and properties to animate (e.g.,opacity,transform). - Vue also provides
enter-active-classandleave-active-classfor more advanced control over the animation's active state, but this challenge focuses on the enter/leave specific class hooks. - Make sure your CSS is scoped appropriately or globally defined depending on your project setup. For this challenge, assume global or component-scoped CSS is acceptable.