Skip to content

LitElement render starts after finishing life cycle events of Angular  #1130

@msbasanth

Description

@msbasanth

Description

We are creating web-component based UI library for our customers. We were using Polymer library to create our web-components and they were working fine. Seeing the advantages and the control that Lit-Element provides to the component developer, we switched to LitElement.

We observed this issue with the Lit Element based webcomponent when used in an Angular component.

Angular-Lit-Angular

Figure 1: Angular->Lit->Angular

The LitElement render is called only after the ngAfterViewInit of both the parent and child Angular elements’ is called.

The result with one Polymer element between 2 Angular elements is as follows:

Angular-Polymer-Angular

Figure 2: Angular->Polymer->Angular

The result with the DOM Structure as Angular -> Polymer -> Angular -> Polymer -> Angular is as follows:

Angular-Polymer-Angular-Polymer-Angular

Figure 3: Angular->Polymer->Angular->Polymer->Angular

The result with the DOM structure as Angular->Lit->Angular->Lit->Angular is as follows:

Angular-Lit-Angular-Lit-Angular

Figure 4: Angular->Lit->Angular->Lit->Angular

  DOM Structure Observation
1 Angular->Lit->Angular Lit element render is called after ngAfterViewInit of both parent and child angular-elements
2 Angular->Polymer->Angular Render is called first before ngOninit of both parent and child angular components
3 Angular->Lit->Angular->Lit->Angular Both Lit elements’ render are called last after ngAfterViewInit of all angular components.
4 Angular->Polymer->Angular->Polymer->Angular Both Polymer elements get template() is called once(probably because they are the same element) before the ngOninit of other angular components

The problem we faced was this:

We have a similar structure as Angular->Lit->Angular as in Figure 1. We have written initialization logic in ngOninit of angular component 1 where we are trying to do some operation on the Lit component. But since the render of the Lit element has not been completed by that time as evidenced by Figure 1, it failed. But when we were using Polymer version of the same component, we didn’t face this issue as component was already rendered before ngOnInit.

What we tried?

Inside ngOnInit & ngAfterViewInit, we fetched the lit element and waited for the lit element’s updateComplete to finish.
This helped in preserving the order of execution of functions (even though it didn’t block the normal execution of angular’s lifecycle events)

Angular-Update-Wait-Code

Angular-Update-Wait

So…is this best way to approach this scenario ?

Acceptance criteria

With the approach we tried the component consumer should handle the lit component differently.

  • Container component really should have to know the DOM structure and wait for lit-element component render.

How can we treat LitElement based webcomponents just like an angular component without such special handling for rendering? Please let us know if there is any better way other than using updateComplete to handle situation described above.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions