Repository navigation
Description
Which @angular/* package(s) are relevant/related to the feature request?
animations
Description
It is currently not possible to handle animation lifecycle events like (animate.enter) or (animate.leave) on the host element using @HostBinding() or @HostListener().
Please provide a way to host-bind or host-listen a callback function for these animation events so that custom animations can be run without adding template event bindings everywhere.
Why is this useful?
With GSAP (or any third-party animation lib), I currently have to manually bind AnimationCallbackEvent handlers in the component template for every instance:
<div (animate.enter)="animateEnter($event)" (animate.leave)="animateLeave($event)">
...
</div>and inside component:
...
animateEnter(event: AnimationCallbackEvent) {
gsap.effects.fadeIn(event.target).eventCallback('onComplete', () => event.animationComplete());
}
animateEnter(event: AnimationCallbackEvent) {
gsap.effects.fadeOut(event.target).eventCallback('onComplete', () => event.animationComplete());
}This results in repetitive boilerplate.
Now imagine a simple AnimateLeaveGsapDirective:
@Directive({
selector: '[animateLeaveGsap]'
})
export class AnimateLeaveGsapDirective implements OnInit {
/** The name of GSAP effect to apply for animate.leave */
readonly animateLeaveGsap = input.required<string>();
@HostBinding('(animate.leave)')
animateLeave: Function;
// or
@HostListener('animate.leave', ['$event'])
onLeave(event: AnimationCallbackEvent) {
...
}
ngOnInit(): void {
const effectName = this.animateLeaveGsap();
const effect = gsap.effects[this.animateLeaveGsap()] as (targets: gsap.TweenTarget) => gsap.core.Tween;
if (!effect) {
console.warn('Error binding GSAP leave animation. Effect not found:', effectName);
return;
}
this.animateLeave = (event: AnimationCallbackEvent) => {
effect(event.target).eventCallback('onComplete', () => event.animationComplete());
}
}
}
Then templates become as simple as CSS-class-like bindings:
<div animateEnterGsap="fade-in" animateLeaveGsap="fade-out">
...
</div>No more boilerplate.
Proposed solution
Allow directives/components to attach animation lifecycle handlers on the host via a decorator or host metadata, similar to DOM events:
@HostBinding('(animate.enter)')
animationCallbackFunction: (event: AnimationCallbackEvent) => void;Alternatives considered
No good alternatives. The only current option is to bind (animate.enter) / (animate.leave) in every template where the component/directive is used, which scales poorly and scatters animation wiring across templates.
Activity
thePunderWoman commentedon Aug 18, 2025
Host binding does already work, but not through the decorators. We recommend using the
hostfield in the component or directive decorator instead.JeanMeche commentedon Aug 18, 2025
Note, that we recommend using the
hostproperty over the decorators in our docs.ahimik commentedon Aug 18, 2025
Hi @thePunderWoman — thanks again for the suggestion! I tried the approach you described, but I can’t get it to work. Here’s a minimal example I tested:
The handler doesn’t fire for me - neither when this code lives inside the directive nor when I try a similar setup in the component.
I was able to make it work with css classes, but not with callback function.
Could you point out what I’m missing? If this should work, I’m happy to open a bug with a repro. Thanks!
thePunderWoman commentedon Aug 18, 2025
@ahimik Can you create this as a minimal reproduction on stackblitz for me to play with? It's possible that because you're using this on a directive, it may be having trouble finding the element to apply the binding to, but I'd need the reproduction to experiment with.
ahimik commentedon Aug 18, 2025
@thePunderWoman , sure. Here is the minimal reproduction with both directive + component: https://stackblitz.com/edit/stackblitz-starters-uqwnng2z?file=src%2Fmain.ts
1 remaining item
thePunderWoman commentedon Aug 18, 2025
Thanks for that. Looks like I can replicate it just for the event binding version. We'll take a look.
fix(compiler): fixes animation event host bindings not firing
fix(compiler): fixes animation event host bindings not firing
fix(compiler): fixes animation event host bindings not firing
fix(compiler): fixes animation event host bindings not firing (#63217)
fix(compiler): fixes animation event host bindings not firing (angula…
angular-automatic-lock-bot commentedon Sep 18, 2025
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.