Skip to content

Provide hostbinding for (animate.enter) and (animate.leave) events #63199

Closed

Description

@ahimik

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

  1. added
    area: animations
    area: core
    on Aug 17, 2025
  2. modified the milestone: needsTriage on Aug 17, 2025
  3. thePunderWoman commented on Aug 18, 2025

    @thePunderWoman
    Contributor

    Host binding does already work, but not through the decorators. We recommend using the host field in the component or directive decorator instead.

    @Component({
       ...
       host: {'(animate.leave)', 'blah'},
       ...
    })
    
  4. JeanMeche commented on Aug 18, 2025

    @JeanMeche
    Member

    Note, that we recommend using the host property over the decorators in our docs.

    Always prefer using the host property over @HostBinding and @HostListener. These decorators exist exclusively for backwards compatibility.

  5. ahimik commented on Aug 18, 2025

    @ahimik
    Author

    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:

    @Directive({
      selector: '[animateLeaveGsap]',
      host: {'(animate.leave)': 'animateLeave($event)'}
    })
    export class AnimateLeaveGsapDirective {
    
      readonly animateLeaveGsap = input.required<string>();
    
      animateLeave(event: AnimationCallbackEvent): 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;
        }
    
        effect(event.target).eventCallback('onComplete', () => event.animationComplete());
    
      }
    
    }

    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!

  6. thePunderWoman commented on Aug 18, 2025

    @thePunderWoman
    Contributor

    @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.

  7. added
    needs reproduction
    on Aug 18, 2025
  8. modified the milestones: needsTriage, Backlog on Aug 18, 2025
  9. ahimik commented on Aug 18, 2025

    @ahimik
    Author
  10. 1 remaining item

  11. modified the milestones: Backlog, needsTriage on Aug 18, 2025
  12. thePunderWoman commented on Aug 18, 2025

    @thePunderWoman
    Contributor

    Thanks for that. Looks like I can replicate it just for the event binding version. We'll take a look.

  13. added 3 commits that reference this issue on Aug 18, 2025
    93e62f0
    530d28d
    c018748
  14. added a commit that references this issue on Aug 18, 2025
    7b51728
  15. added a commit that references this issue on Aug 18, 2025
    cab3adf
  16. angular-automatic-lock-bot commented on Sep 18, 2025

    @angular-automatic-lock-bot

    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.

  17. locked and limited conversation to collaborators on Sep 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area: animationsarea: corestate: confirmedstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions