Hone logo
Hone
Problems

Smooth Element Transitions with Vue CSS Transitions

This challenge focuses on implementing CSS transitions in a Vue.js application to create visually appealing animations for elements appearing and disappearing. This is a fundamental technique for enhancing user experience by providing visual feedback and making interfaces feel more dynamic and responsive.

Problem Description

Your task is to create a Vue component that toggles the visibility of a specific element (e.g., a message box, an image, or a list item). This toggling should be accompanied by a smooth CSS transition, rather than an abrupt appearance or disappearance.

Key Requirements:

  1. Vue Component: Create a single-file Vue component (.vue file) using TypeScript.
  2. Toggle Button: Include a button that, when clicked, toggles the visibility of a designated content element.
  3. CSS Transition: Implement a CSS transition for the content element. The transition should animate changes in its opacity and height (or transform for a scaling effect, though height is a good starting point).
  4. Vue's Transition Component: Utilize Vue's built-in <transition> component to manage the entry and exit animations.
  5. TypeScript: All Vue component logic (script section) must be written in TypeScript.

Expected Behavior:

  • When the component loads, the content element should be initially hidden.
  • Clicking the "Show/Hide" button should trigger the appearance/disappearance of the content element.
  • The appearance should be animated from invisible and zero height to visible and its natural height.
  • The disappearance should be animated from visible and its natural height to invisible and zero height.
  • The animation should be smooth and visually pleasing, taking a noticeable but not overly long duration.

Edge Cases to Consider:

  • Initial State: Ensure the transition works correctly when the component is first rendered and the element is hidden.
  • Rapid Toggling: The transitions should handle rapid clicks of the toggle button gracefully without visual glitches.

Examples

Example 1: Basic Fade and Slide

Assume we have a simple div that we want to toggle.

Input: (Conceptual - based on user interaction)

  1. Component mounts. Content is hidden.
  2. User clicks the "Toggle Content" button.

Output: The content div fades in from opacity: 0 to opacity: 1 and smoothly expands its height from 0px to its natural height over, say, 0.3 seconds.

Input: (Conceptual)

  1. Content div is visible.
  2. User clicks the "Toggle Content" button again.

Output: The content div fades out from opacity: 1 to opacity: 0 and smoothly collapses its height from its natural height to 0px over, say, 0.3 seconds.

Example 2: Different Transition Properties

Consider toggling an image.

Input: (Conceptual)

  1. Component mounts. Image is hidden.
  2. User clicks the "Toggle Image" button.

Output: The image scales in from scale(0.5) and opacity: 0 to scale(1) and opacity: 1 over 0.5 seconds.

Input: (Conceptual)

  1. Image is visible.
  2. User clicks the "Toggle Image" button again.

Output: The image scales out from scale(1) and opacity: 1 to scale(0.5) and opacity: 0 over 0.5 seconds.

Constraints

  • Vue Version: Vue 3.
  • TypeScript: The <script setup lang="ts"> syntax is preferred.
  • CSS: Transitions should be implemented using standard CSS transition properties.
  • Animation Duration: Aim for transition durations between 0.2s and 1s.
  • No External Libraries: Do not use any external animation libraries (e.g., Animate.css, GSAP).

Notes

  • Vue's <transition> component automatically applies specific CSS classes during the entry and exit phases of an element. Familiarize yourself with these classes: v-enter-from, v-enter-active, v-enter-to, v-leave-from, v-leave-active, v-leave-to.
  • For animating height, you might need to set overflow: hidden; on the element being transitioned.
  • Consider using display: none; only when the element is completely gone after the transition, or rely on opacity and height to control visibility and layout.
  • Think about the transition-timing-function for a more natural feel (e.g., ease-in-out).
  • The goal is to have a functional Vue component that demonstrates a smooth CSS transition upon conditional rendering.
Loading editor...
typescript