Skip to content

fix(core): prevent child animation elements from being orphaned#67411

Merged
thePunderWoman merged 1 commit intoangular:mainfrom
thePunderWoman:animation-bug
Mar 4, 2026
Merged

fix(core): prevent child animation elements from being orphaned#67411
thePunderWoman merged 1 commit intoangular:mainfrom
thePunderWoman:animation-bug

Conversation

@thePunderWoman
Copy link
Copy Markdown
Contributor

When routing between two different routes, child animations were not finishing, causing elements to be left behind in the dom. The fix ensures the proper fallback is handled to avoid automatically cancelled custom events. This ensures the animation-fallback cancelling the animation actually completes, and ensures the element is removed.

fixes: #67400

@thePunderWoman thePunderWoman added action: review The PR is still awaiting reviews from at least one requested reviewer area: core Issues related to the framework runtime target: patch This PR is targeted for the next patch release labels Mar 3, 2026
@ngbot ngbot bot added this to the Backlog milestone Mar 3, 2026
@AndrewKushnir
Copy link
Copy Markdown
Contributor

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a robust fix for an issue where animated elements were being orphaned in the DOM due to missed animationend events. The core of the solution is the implementation of a fallback timer for leave animations, which ensures cleanup is performed even if the browser drops the animation event. The changes also include handling for transition-property: all, fixes for potential race conditions using a hasCompleted flag, and a switch from stopImmediatePropagation to stopPropagation to improve event handling behavior. These changes are well-implemented and are accompanied by a new e2e test that specifically validates the fallback mechanism. Overall, this is a solid contribution that improves the reliability of animations.

When routing between two different routes, child animations were not finishing, causing elements to be left behind in the dom. The fix ensures the proper fallback is handled to avoid automatically cancelled custom events. This ensures the animation-fallback cancelling the animation actually completes, and ensures the element is removed.

fixes: angular#67400
if (event.target !== el) return;
if (event instanceof CustomEvent || isLongestAnimation(event, el)) {
// Custom fallback events don't have a target, so we bypass this check for them.
if (event.target !== el && event.type !== 'animation-fallback') return;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that event.target is a bit weird if the event comes from the shadow DOM. We use this utility in Components: https://github.com/angular/components/blob/main/src/cdk/platform/features/shadow-dom.ts#L59

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. I'll do a quick follow up after this to use a similar function.

@thePunderWoman thePunderWoman added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Mar 4, 2026
@thePunderWoman thePunderWoman removed the request for review from AndrewKushnir March 4, 2026 16:20
@thePunderWoman thePunderWoman merged commit 9e64147 into angular:main Mar 4, 2026
21 checks passed
@thePunderWoman
Copy link
Copy Markdown
Contributor Author

This PR was merged into the repository. The changes were merged into the following branches:

@thePunderWoman thePunderWoman deleted the animation-bug branch March 4, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: core Issues related to the framework runtime target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In some cases, the animate.leave animations leave a component in the DOM.

3 participants