Event handlers

ng-gsap

View source

contextSafe is the other half of the API. The reactive callback is already inside a gsap.context(), but a click handler isn't — so tweens it creates would escape cleanup. Wrapping the handler puts them back under the same context.

The distinction that matters: wrap handlers that create animations. A handler that only writes a signal needs nothing — the callback re-runs and reverts the previous run for you.

Click any chip to spin it (contextSafe)
private stage = viewChild.required<ElementRef>('stage');

private anim = injectGSAP(() => {
  gsap.from('.chip', { scale: 0, stagger: 0.05 });
}, { scope: this.stage });

// A handler that CREATES animations lives outside the reactive callback, so
// wrap it: the tweens it makes join the same context and get reverted with
// everything else when this component is destroyed.
protected burst = this.anim.contextSafe((event: MouseEvent) => {
  gsap.to(event.target, { rotate: '+=180', y: gsap.utils.random(-20, 20) });
});

// A plain signal write needs NO wrapper — the callback re-runs and cleans up
// after itself. Wrapping this would be cargo cult.
protected shuffle = () => this.seed.update((n) => n + 1);