Hone logo
Hone
Problems

Angular Staggered List Animation Challenge

This challenge focuses on implementing a common UI pattern: staggered animations for lists in Angular. Staggered animations create a more engaging and fluid user experience by introducing elements with a slight delay, making them appear one after another. This is particularly useful for displaying lists of items, where animating them simultaneously can feel abrupt.

Problem Description

Your task is to create a component in Angular that displays a list of items. When new items are added to the list, they should animate into view with a staggered effect. This means each item should fade in and/or slide in with a small delay after the previous one.

Key Requirements:

  • Dynamic List: The component should be able to handle a dynamic list of items that can grow or shrink.
  • Staggered Animation: When items are added, they must animate in sequentially with a defined delay between each item.
  • CSS-based Animations: Utilize Angular's animation capabilities, ideally leveraging CSS transitions or keyframes.
  • Configurable Delay: The delay between staggered animations should be configurable.
  • Clear Entry/Exit: Items should have a clear visual transition when entering the list. Consider a simple fade-in or slide-in effect.

Expected Behavior:

When items are added to the list (e.g., by clicking a button to add a new item), the new items should appear one by one, with a noticeable delay between each appearance. The animation should be smooth and visually appealing.

Edge Cases to Consider:

  • Rapid Additions: How does the animation behave if multiple items are added in quick succession?
  • List Clearing: What happens when the list is cleared entirely? Should items animate out? (For this challenge, focus primarily on entry animations).

Examples

Example 1: Initial List Load

Imagine an initial list of 3 items.

Input: A data array ['Item 1', 'Item 2', 'Item 3'].

Output: The UI displays these three items, animating in one after another with a staggered effect.

Explanation: The animation begins for 'Item 1', then after a short delay, 'Item 2' starts its animation, followed by 'Item 3'.

Example 2: Adding New Items

Assume the list currently has 3 items, and a button click triggers adding two more.

Input: The data array changes from ['Item 1', 'Item 2', 'Item 3'] to ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'].

Output: 'Item 4' animates in after 'Item 3', and then 'Item 5' animates in after 'Item 4', each with the defined staggered delay.

Explanation: The new items 'Item 4' and 'Item 5' are added to the DOM and trigger their entry animations sequentially.

Constraints

  • Angular Version: Use Angular 14 or later.
  • TypeScript: The solution must be written in TypeScript.
  • Animation Trigger: The animation should be triggered by changes to the list data (e.g., when the array is modified).
  • Performance: The animation should be performant, avoiding unnecessary re-renders or complex computations.
  • No External Libraries (for animations): Primarily use Angular's built-in animation capabilities (@angular/animations).

Notes

  • Think about how Angular's trigger, transition, animate, and query functions can be used to achieve this.
  • The stagger directive from @angular/animations is a key tool for this challenge.
  • Consider using CSS classes for styling and defining the actual animation properties (like opacity, transform).
  • The delay between each staggered item can be set using the stagger function.
  • For this challenge, the focus is on the entry animation when items are added to the list. Exit animations are not a primary requirement.
Loading editor...
typescript