Hone logo
Hone
Problems

Angular Animation Trigger Implementation

This challenge focuses on implementing a simple animation trigger in Angular. Animation triggers allow you to define animations that are activated based on changes in component state, providing a dynamic and engaging user experience. Your task is to create a component that utilizes an Angular animation trigger to smoothly transition between two states.

Problem Description

You need to build an Angular component that displays a simple element (e.g., a div) and animates its visibility based on a boolean property. The animation should transition the element from a hidden state to a visible state and back again when the boolean property changes. The animation should use Angular's animation system, defining a trigger with states and transitions.

Key Requirements:

  • Component Structure: Create a new Angular component.
  • Animation Trigger: Define an animation trigger named visibility within the component.
  • States: The trigger should have two states: hidden and visible.
  • Transitions: Define transitions between the hidden and visible states. The transition from hidden to visible should include a fade-in effect (opacity change). The transition from visible to hidden should include a fade-out effect (opacity change).
  • Property Binding: Bind a boolean property (e.g., isVisible) in the component to the animation trigger's state.
  • Smooth Transition: The animation should be visually smooth and noticeable.
  • Dynamic Behavior: The element should dynamically appear and disappear based on changes to the isVisible property.

Expected Behavior:

  1. Initially, the element should be hidden.
  2. When the isVisible property is set to true, the element should smoothly fade in.
  3. When the isVisible property is set to false, the element should smoothly fade out.
  4. The animation should be triggered automatically by Angular when the isVisible property changes.

Edge Cases to Consider:

  • Ensure the animation works correctly when the isVisible property changes rapidly.
  • Consider how the animation might behave if the component is destroyed and recreated while the isVisible property is in a particular state. (While not strictly required for this challenge, thinking about this demonstrates good understanding).

Examples

Example 1:

Input: isVisible = false (initial state)
Output: The div is not displayed.
Explanation: The component initializes with the `isVisible` property set to false, triggering the 'hidden' state of the animation.

Example 2:

Input: isVisible = true (after 2 seconds)
Output: The div smoothly fades in over a short duration (e.g., 0.3s).
Explanation: Setting `isVisible` to true triggers the transition from 'hidden' to 'visible', initiating the fade-in animation.

Example 3:

Input: isVisible = false (after 5 seconds)
Output: The div smoothly fades out over a short duration (e.g., 0.3s).
Explanation: Setting `isVisible` to false triggers the transition from 'visible' to 'hidden', initiating the fade-out animation.

Constraints

  • Animation Duration: The fade-in and fade-out animations should have a duration of no more than 0.5 seconds.
  • Animation Timing Function: Use an ease timing function for the animations.
  • Component Size: The component should be relatively simple and focused solely on the animation trigger functionality.
  • Angular Version: Assume you are using a recent version of Angular (v14 or later).
  • No External Libraries: Do not use any external animation libraries. Use Angular's built-in animation system.

Notes

  • You'll need to define the animation trigger in the component's @Component decorator using the animations property.
  • Use the [appVisibility] directive (or similar) to bind the isVisible property to the animation state.
  • Consider using CSS opacity to achieve the fade-in and fade-out effects.
  • Focus on creating a clear and concise animation trigger definition. The visual effect is important, but so is the readability and maintainability of your code.
  • Remember to import Component and animations from @angular/core.
Loading editor...
typescript