@@ -34,6 +34,11 @@ export const CONTENT_ATTR = `_ngcontent-${COMPONENT_VARIABLE}`;
3434 */
3535const REMOVE_STYLES_ON_COMPONENT_DESTROY_DEFAULT = true ;
3636
37+ /**
38+ * The default value for the `ISOLATED_SHADOW_DOM` DI token.
39+ */
40+ const ISOLATED_SHADOW_DOM_DEFAULT = false ;
41+
3742/**
3843 * A [DI token](guide/glossary#di-token "DI token definition") that indicates whether styles
3944 * of destroyed components should be removed from DOM.
@@ -47,6 +52,20 @@ export const REMOVE_STYLES_ON_COMPONENT_DESTROY =
4752 factory : ( ) => REMOVE_STYLES_ON_COMPONENT_DESTROY_DEFAULT ,
4853 } ) ;
4954
55+ /**
56+ * A [DI token](guide/glossary#di-token "DI token definition") that indicates whether the style
57+ * of components that are using ShadowDom as encapsulation must remain isolated from other
58+ * components instances styles and/or global styles.
59+ *
60+ * By default, the value is set to `false`.
61+ * @publicApi
62+ */
63+ export const ISOLATED_SHADOW_DOM =
64+ new InjectionToken < boolean > ( ngDevMode ? 'IsolatedShadowDom' : '' , {
65+ providedIn : 'root' ,
66+ factory : ( ) => ISOLATED_SHADOW_DOM_DEFAULT ,
67+ } ) ;
68+
5069export function shimContentAttribute ( componentShortId : string ) : string {
5170 return CONTENT_ATTR . replace ( COMPONENT_REGEX , componentShortId ) ;
5271}
@@ -71,6 +90,7 @@ export class DomRendererFactory2 implements RendererFactory2, OnDestroy {
7190 private readonly sharedStylesHost : SharedStylesHost ,
7291 @Inject ( APP_ID ) private readonly appId : string ,
7392 @Inject ( REMOVE_STYLES_ON_COMPONENT_DESTROY ) private removeStylesOnCompDestroy : boolean ,
93+ @Inject ( ISOLATED_SHADOW_DOM ) private readonly isolatedShadowDom : boolean ,
7494 @Inject ( DOCUMENT ) private readonly doc : Document ,
7595 @Inject ( PLATFORM_ID ) readonly platformId : Object ,
7696 readonly ngZone : NgZone ,
@@ -123,7 +143,7 @@ export class DomRendererFactory2 implements RendererFactory2, OnDestroy {
123143 break ;
124144 case ViewEncapsulation . ShadowDom :
125145 return new ShadowDomRenderer (
126- eventManager , sharedStylesHost , element , type , doc , ngZone , this . nonce ,
146+ eventManager , this . isolatedShadowDom ? null : sharedStylesHost , element , type , doc , ngZone , this . nonce ,
127147 platformIsServer ) ;
128148 default :
129149 renderer = new NoneEncapsulationDomRenderer (
@@ -358,7 +378,7 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
358378
359379 constructor (
360380 eventManager : EventManager ,
361- private sharedStylesHost : SharedStylesHost ,
381+ private readonly sharedStylesHost : SharedStylesHost | null ,
362382 private hostEl : any ,
363383 component : RendererType2 ,
364384 doc : Document ,
@@ -374,7 +394,8 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
374394 this . shadowRoot . innerHTML = "" ;
375395 }
376396
377- this . sharedStylesHost . addHost ( this . shadowRoot ) ;
397+ this . sharedStylesHost ?. addHost ( this . shadowRoot ) ;
398+
378399 const styles = shimStylesContent ( component . id , component . styles ) ;
379400
380401 for ( const style of styles ) {
@@ -407,7 +428,7 @@ class ShadowDomRenderer extends DefaultDomRenderer2 {
407428 }
408429
409430 override destroy ( ) {
410- this . sharedStylesHost . removeHost ( this . shadowRoot ) ;
431+ this . sharedStylesHost ? .removeHost ( this . shadowRoot ) ;
411432 }
412433}
413434
0 commit comments