Skip to content

[Feature] Add Component Life-cycle Hook ngOnElementDestroy for Better Animations #48224

@Char2sGu

Description

@Char2sGu

Which @angular/* package(s) are relevant/related to the feature request?

animations, core

Description

Currently, as ngOnDestroy is called before the removal animation of a component, a lot of fantastic removal animations cannot be implemented easily and we have to use confusing workarounds for these animations.

For example, @angular/cdk/portal portals will detach from the portal outlet in the ngOnDestroy hook. Therefore, there would be no content for portal outlets in removal animations: StackBlitz Demo

Portal

The problem above can actually be easily solved by using ngTemplateOutlet instead of cdkPortalOutlet, but in many cases the problem can be really hard to work around.

I am currently working on implementing the app "Reply" of Material Studies in this repository. I am using the directive libLayoutProjectionNode and the component injection tree to build a tree of nodes, so that I can implement the Container Transform animation by using the Layout Projection technique. The Layout Projection technique basically creates a snapshot of the layout before changing the dom, and transform the elements from their old layouts to their new layouts after the dom has changed.

This is the best Container Transform that we can implement using Angular:

Reply Container Transform

And this is the standard Container Transform from Material Design:

Container Transform

As you can see, our Container Transform works mostly well except that there is no crossfade transition.

Here comes the problem - It's completely impossible to implement a crossfade transition because it's impossible to get the old tree of nodes as them have already been detached from their parents in the ngOnDestroy hook to avoid memory leak. 🤷

Proposed solution

I propose that we should have a new life-cycle hook, which is invoked after the element of a component is actually removed from the DOM. A proposed name can be ngOnElementDestroy.

Developers should consider using this new ngOnElementDestroy hook to clean up their components/directives if the code they put in the ngOnDestroy hook changes the DOM.

This should significantly make the DOM more stable for removal animations.

I know that it's probably not the best choice to introduce a new life-cycle hook, but I failed to find out a better solution. 🤷

Alternatives considered

Otherwise WE HAVE TO DO A LOT OF COMPLICATED WORKAROUNDS TO IMPLEMENT THESE FANTASTIC REMOVAL ANIMATIONS!!!!!!!!!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: animationslegacy animations package only. Otherwise use area: core.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions