Hone logo
Hone
Problems

Dynamic Button Styling with CSS Transitions in Vue

This challenge focuses on implementing smooth CSS transitions in a Vue.js component to enhance user experience. You'll be creating a button that changes its background color and size on hover, utilizing Vue's reactivity and CSS transition properties for a visually appealing effect. This is a common requirement in modern web development, and mastering it is crucial for building interactive and engaging interfaces.

Problem Description

You need to create a Vue component called AnimatedButton that displays a button. When the user hovers over the button, its background color should smoothly transition from a default color (e.g., lightblue) to a hover color (e.g., darkblue), and its size should increase slightly. When the user moves the mouse away, the button should smoothly transition back to its original state.

Key Requirements:

  • Vue Component: The solution must be a valid Vue component written in TypeScript.
  • Reactivity: The component should use Vue's reactivity system to manage the button's state (e.g., isHovered).
  • CSS Transitions: Utilize CSS transitions to create the smooth visual effects. Avoid using JavaScript-based animations.
  • Hover State: The component should detect when the mouse is hovering over the button and update its state accordingly.
  • Default and Hover Styles: Define distinct default and hover styles for the button.

Expected Behavior:

  • On mouse hover: The button's background color smoothly transitions to the hover color, and its size increases.
  • On mouse leave: The button's background color smoothly transitions back to the default color, and its size returns to its original size.
  • The transitions should be visually smooth and responsive.

Edge Cases to Consider:

  • Ensure the transitions work correctly on different browsers. (While not explicitly required to test all browsers, be mindful of potential cross-browser compatibility issues with CSS transitions).
  • Consider the performance implications of complex transitions. Keep the transitions relatively simple for this exercise.

Examples

Example 1:

Input: No input required. The component should be self-contained.
Output: A Vue component that displays a button. On hover, the button's background color transitions from lightblue to darkblue, and its size increases. On mouse leave, it transitions back.
Explanation: The component uses Vue's reactivity to track the hover state and applies CSS transitions to the button's styles.

Example 2:

Input:  The component is rendered within a larger Vue application.
Output: The `AnimatedButton` component functions as expected, regardless of its parent component.
Explanation: The component is designed to be reusable and independent.

Constraints

  • Language: TypeScript
  • Framework: Vue.js (version 3 or higher)
  • Transition Duration: The transition duration should be between 200ms and 500ms.
  • Size Increase: The button's size should increase by a factor of 1.1 to 1.3 on hover.
  • No JavaScript Animation Libraries: The solution must use CSS transitions and not rely on JavaScript animation libraries (e.g., GreenSock, Anime.js).
  • Component Size: Keep the component relatively concise and focused on the core task of implementing the transition.

Notes

  • You can use inline styles or a separate CSS file to define the button's styles. Using a separate CSS file is generally preferred for larger projects.
  • Consider using Vue's :class binding to conditionally apply CSS classes based on the hover state.
  • Think about how to make the component configurable (e.g., allowing the user to specify the default color, hover color, and transition duration as props). While not required for this basic challenge, it's a good practice to consider.
  • Focus on the smooth transition effect and the correct handling of the hover state.
Loading editor...
typescript