Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Native and ShadowDom ViewEncapsulation?

In my current angular 7 application we are struggling with a component from a library, which requires some css ressources. We do not want to apply those ressources to all the rest of our application, but to one specific component, its children and grandchildren.

During our research we found these two interesting options:

encapsulation: ViewEncapsulation.Native

and:

encapsulation: ViewEncapsulation.ShadowDom

In consequence, they both seem to use the browser's shadow dom implementation.

What is the difference between those options?

like image 708
slartidan Avatar asked Nov 21 '18 10:11

slartidan


People also ask

What is ViewEncapsulation ShadowDOM?

ViewEncapsulation.ShadowDom. Angular uses the browser's built-in Shadow DOM API to enclose the component's view inside a ShadowRoot (used as the component's host element) and apply the provided styles in an isolated manner. ViewEncapsulation.

What is ViewEncapsulation native in Angular?

In the browser, when you examine source code, you will a Shadow DOM has been created for the AppComponent and the style is scoped to that. Therefore, in ViewEncapsulation. Native , Angular creates a Shadow DOM and the style is scoped to that Shadow DOM.

What is ShadowDOM in Angular?

Shadow DOM is like a parallel DOM tree hosted inside a component (an HTML element, not to be confused with Angular components), hidden away from the main DOM tree. No part of the application has access to this shadow DOM other than the component itself.

What is the use of ViewEncapsulation in Angular?

View encapsulation defines whether the template and styles defined within the component can affect the whole application or vice versa. Angular provides three encapsulation strategies: Emulated (default) - styles from main HTML propagate to the component.


1 Answers

This has been been baffling me days ago, then I realized that they converge a little but not totally. The dissimilarity in fact is about the newer version of shadowDOM (v1). As you can see here in angular's code source they added an other condition for the ViewEncapsulation.ShadowDom:

Here they share the same return

 case ViewEncapsulation.Native:
 case ViewEncapsulation.ShadowDom:
      return new ShadowDomRenderer(this.eventManager, this.sharedStylesHost, element, type);    

And then they check whether it is ViewEncapsulation.ShadowDom or not (else condition)

     if (component.encapsulation === ViewEncapsulation.ShadowDom) {
          this.shadowRoot = (hostEl as any).attachShadow({mode: 'open'});
        } else {
          this.shadowRoot = (hostEl as any).createShadowRoot();
        }

Thus, ViewEncapsulation.ShadowDom is a step to add a support to ShadowDOM V1, and maybe deprecate the ViewEncapsulation.Native as described here:

ViewEncapsulation.Shadow is added as a new API, rather than changing the behavior of the ViewEncapsulation.Native option, which could lead to unexpected results for developers currently using the v0 API. This should (eventually?) deprecate the ViewEncapsulation.Native option.

like image 66
SeleM Avatar answered Jan 06 '23 01:01

SeleM