Skip to content

nested animations don't behave as expected #63388

Closed

Description

@CoreAngel

Which @angular/* package(s) are the source of the bug?

core, animations

Is this a regression?

No

Description

I wanted to migrate a simple dialog with a backdrop from @angular/animation to the new API. The requirements are:

  1. Structure: backdrop > container > content
  2. The container animation must be longer than the backdrop animation
  3. I need to know when the animations (both enter and leave) have finished

1. animate.leave doesn't emit animationend

Referring to the comment (#62212 (reply in thread)), the preferred way to react to animation events is now through native browser events. Unfortunately, in the case of animate.leave, it seems that neither animationend nor animationcancel is emitted.

2. Nested animations

I want to add animations to the 'backdrop ' and 'container'. I've tested two ways to achieve my goal:

  1. Nested attributes animate
<backdrop animate.enter="..." animate.leave="...">
    <container animate.enter="..." animate.leave="...">
        ...content
    </container>
</backdrop>
  1. Nested styles with animation
<style>
    backdrop.enter { animation: ... }
    backdrop.enter > container { animation: ... }
    backdrop.leave { animation: ... }
    backdrop.leave > container { animation: ... }
</style>

<backdrop animate.enter="..." animate.leave="...">
    <container>
        ...content
    </container>
</backdrop>

In the first approach, animate.enter works correctly, but animate.leave for the container does not fire at all.
In the second attempt, the enter and leave animations for the container are interrupted.

To be honest, I'm not sure how this situation with nested animations should work, but I think the more natural approach is the first one, and the nested 'animate' should also be notified when the parent is destroyed. The question remains whether a nested animation should prevent its parent from being destroyed.

I am aware that I can achieve this with (animate) or a second approach with animations of equal duration.

3. Class-based animation termination

According to documentation (https://angular.dev/guide/animations#event-bindings-functions-and-third-party-libraries):

If you don't call animationComplete() when using animate.leave, Angular calls the function automatically after a four-second delay.

This information is incomplete, as animations based on CSS classes also terminate after 4 seconds. This can be misleading, as the documentation suggests that the timeout only serves as a safeguard against a missing call animationComplete().

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/kyent3r4?file=src%2Fmain.ts

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 20.2.0
Node: 22.17.1
Package Manager: npm 10.9.2
OS: win32 x64


Angular: 20.2.1
... animations, common, compiler, compiler-cli, core, elements
... forms, language-service, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------
@angular-devkit/architect       0.2002.0
@angular-devkit/build-angular   20.2.0
@angular-devkit/core            20.2.0 (cli-only)
@angular-devkit/schematics      20.2.0
@angular/cdk                    20.2.0
@angular/cli                    20.2.0
@schematics/angular             20.2.0
rxjs                            7.8.2
typescript                      5.8.3

Anything else?

No response

Activity

  1. added
    area: animations
    on Aug 25, 2025
  2. added this to the needsTriage milestone on Aug 25, 2025
  3. thePunderWoman commented on Aug 26, 2025

    @thePunderWoman
    Contributor

    @CoreAngel Thanks for this report. There's a few things here:

    1. animate.leave should emit animationend. If it's not, that's something we'll need to address, but in order to actually remove the element, internally it relies on animationend. So it has to emit it. We do cancel bubbling though, and maybe that's why you're not seeing it. We can maybe revisit if we should allow bubbling to continue. We'll take a look at the repro to see what's happening.

    2. This one is complicated, but we'll look at your repro and see what's happening.

    3. This is a good callout. That timeout was intended solely for event based binding protection, and probably should not affect non-event based bindings. We can look at addressing this.

  4. removed
    area: animations
    on Aug 26, 2025
  5. removed this from the needsTriage milestone on Aug 26, 2025
  6. added
    area: core
    core: animations
    on Aug 26, 2025
  7. added this to the needsTriage milestone on Aug 26, 2025
  8. thePunderWoman commented on Aug 26, 2025

    @thePunderWoman
    Contributor

    I can confirm that animationend is not firing on animate.leave. This is definitely a bug. I'm going to create a separate issue to track. The nested animation one is more complicated. Essentially there's a waterfall happening where the parent node animation fires when destroy is called. So that animation runs, and then once it's done the destroy is called on the child node. However...once that parent animation is done, the node is removed. So the child node is removed immediately along with it.

    This is definitely something we'll need to fix, but it's a more complicated one. In the meantime, definitely use the (event) version or nested version the to achieve your goals. The nested version behaves the way it does because it's looking at the element that the animate.enter or animate.leave is on to know when to remove the element or class. So unless the parent element has the same or longer animation duration, the child animation will always be interrupted.

  9. changed the title [-]animate.leave doesn't emit animationend, nested animation and timeout docs[/-] [+]nested animations don't behave as expected[/+] on Aug 26, 2025
  10. thePunderWoman commented on Aug 26, 2025

    @thePunderWoman
    Contributor

    I've created #63391 to track the animationend bug.

  11. thePunderWoman commented on Aug 26, 2025

    @thePunderWoman
    Contributor

    @CoreAngel #63393 should address the timeout behavior and ensure it only applies to (animate.leave) usages.

  12. thePunderWoman commented on Aug 27, 2025

    @thePunderWoman
    Contributor

    @CoreAngel So the reason animation events are not firing on the custom binding has to do with timing of when leave animations happen. It's technically happening post component destroy, which means all the event bindings on the component level are cleaned up already. They're getting emitted internally to the nodes that were manually subscribed for animations to work, but they will never emit all the way up to your custom binding due to the current timing. This will require a larger refactor to fix. Stay tuned.

  13. added a commit that references this issue on Aug 29, 2025
    9a58c72
  14. 13 remaining items

  15. added 10 commits that reference this issue on Sep 10, 2025
    739bafb
    f380b5a
    2a7cced
    9f12222
    ffe4a8e
    bbd4842
    de73d46
    74acd8f
    850e2f0
    6edffe4
  16. added a commit that references this issue on Sep 10, 2025
    5f5828f
  17. added a commit that references this issue on Sep 10, 2025
    4924108
  18. angular-automatic-lock-bot commented on Oct 11, 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.

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

Metadata

Metadata

Labels

area: corecore: animations

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions