Dynamic Transition Group with Custom Class and Visibility Control in Vue.js
This challenge focuses on implementing a Vue.js component that utilizes the <TransitionGroup> to dynamically add and remove elements with custom CSS transitions. The goal is to create a reusable component that allows for controlling the transition classes and visibility of elements entering and leaving the group, providing a flexible way to animate lists or other dynamic content. This is a common requirement in modern web applications for creating smooth and engaging user experiences.
Problem Description
You are tasked with creating a Vue.js component called DynamicTransitionGroup. This component should wrap a list of elements and animate their entry and exit using the <TransitionGroup> component. The component should accept the following props:
items: An array of data items to be rendered within the transition group.enterClass: A string representing the CSS class to apply during the element's entering transition.leaveClass: A string representing the CSS class to apply during the element’s exiting transition.show: A boolean prop that controls the initial visibility of the transition group. Iffalse, the group should be initially hidden.
The component should render each item in the items array as a separate element within the <TransitionGroup>. The enterClass and leaveClass props should be applied to the entering and leaving elements, respectively. The component should also handle the initial visibility based on the show prop.
Key Requirements:
- Use the
<TransitionGroup>component for animating the elements. - Apply the provided
enterClassandleaveClassto the entering and leaving elements. - Control the initial visibility of the entire transition group based on the
showprop. - Ensure the component renders all items from the
itemsarray. - The component should be reusable and adaptable to different transition styles.
Expected Behavior:
- When the
itemsarray changes (e.g., items are added or removed), the<TransitionGroup>should automatically trigger the appropriate transitions for the affected elements. - Elements entering the group should have the
enterClassapplied during their transition. - Elements leaving the group should have the
leaveClassapplied during their transition. - If
showisfalseinitially, the entire transition group should be hidden. Changes toshowshould trigger a transition to reveal or hide the group.
Edge Cases to Consider:
- Empty
itemsarray: The component should render nothing if theitemsarray is empty. - Initial
showprop value: The component should correctly handle the initial visibility based on theshowprop. - Rapid changes to the
itemsarray: The transitions should still function correctly even with frequent updates to theitemsarray.
Examples
Example 1:
Input: items = ['Item 1', 'Item 2', 'Item 3'], enterClass = 'fade-in', leaveClass = 'fade-out', show = true
Output: A list of three elements ('Item 1', 'Item 2', 'Item 3') animated with fade-in on entry and fade-out on exit, initially visible.
Explanation: The component renders the list with the specified transition classes and is initially visible.
Example 2:
Input: items = ['Item A', 'Item B'], enterClass = 'slide-left', leaveClass = 'slide-right', show = false
Output: A list of two elements ('Item A', 'Item B') animated with slide-left on entry and slide-right on exit, initially hidden.
Explanation: The component renders the list with the specified transition classes but is initially hidden. Setting `show` to `true` will trigger a transition to reveal the list.
Example 3: (Edge Case)
Input: items = [], enterClass = 'zoom-in', leaveClass = 'zoom-out', show = true
Output: Nothing is rendered.
Explanation: The component handles the empty array case gracefully by rendering nothing.
Constraints
- The component must be written in TypeScript.
- The component should be self-contained and reusable.
- The transitions should be CSS-based (no JavaScript-based transitions).
- The component should be performant and avoid unnecessary re-renders.
- The
itemsarray can contain any type of data.
Notes
- Consider using the
v-ifdirective on the<TransitionGroup>itself to control the initial visibility based on theshowprop. - The
enterClassandleaveClassprops are strings, so ensure they are correctly applied to the elements. - You can use CSS classes like
v-enter-active,v-leave-active,v-enter-from,v-enter-to,v-leave-from, andv-leave-toto further customize the transitions. However, the providedenterClassandleaveClassshould be applied in addition to these default classes. - Think about how to handle the case where the
itemsarray is initially empty.