Signal-driven list

ng-gsap

View source

Enter animations come from the dependency itself: the callback reads items(), so adding a row re-runs it, and only rows it hasn't seen before get a tween. Leaving needs the opposite order — animate first, then drop the item from the signal in onComplete.

Design the API
Write the playground
Ship it
private list = viewChild.required<ElementRef>('list');
private entered = new Set<number>();

// enter: reading items() re-runs this on every add, and only rows we haven't
// animated before get a tween
private anim = injectGSAP(() => {
  this.items();
  const fresh = Array.from(this.list().nativeElement.querySelectorAll('.item'))
    .filter((el) => !this.entered.has(Number(el.dataset.id)));

  fresh.forEach((el) => this.entered.add(Number(el.dataset.id)));
  if (fresh.length) {
    gsap.from(fresh, { opacity: 0, x: -24, stagger: 0.05 });
  }
}, { scope: this.list });

// leave: this handler CREATES a tween, so it goes through contextSafe
protected remove = this.anim.contextSafe((id: number, el: HTMLElement) => {
  gsap.to(el, {
    opacity: 0,
    x: 24,
    duration: 0.25,
    onComplete: () => this.items.update((list) => list.filter((i) => i.id !== id)),
  });
});