diff --git a/aio/content/examples/component-styles/src/app/quest-summary.component.ts b/aio/content/examples/component-styles/src/app/quest-summary.component.ts index 418937182b42..c50bee777f55 100644 --- a/aio/content/examples/component-styles/src/app/quest-summary.component.ts +++ b/aio/content/examples/component-styles/src/app/quest-summary.component.ts @@ -10,9 +10,3 @@ import { Component } from '@angular/core'; }) export class QuestSummaryComponent { } // #enddocregion -/* - // #docregion encapsulation.shadow - // warning: not all browsers support shadow DOM encapsulation at this time - encapsulation: ViewEncapsulation.ShadowDom - // #enddocregion encapsulation.shadow -*/ diff --git a/aio/content/guide/view-encapsulation.md b/aio/content/guide/view-encapsulation.md index c4a12073f4bc..e531edd7139d 100644 --- a/aio/content/guide/view-encapsulation.md +++ b/aio/content/guide/view-encapsulation.md @@ -1,46 +1,37 @@ # View encapsulation -In Angular, component CSS styles are encapsulated into the component's view and don't -affect the rest of the application. +In Angular, a component's styles can be encapsulated within the component's host element so that they don't affect the rest of the application. + +The `Component`'s decorator provides the [`encapsulation`](api/core/Component#encapsulation) option which can be used to control how the encapsulation is applied on a _per component_ basis. -To control how this encapsulation happens on a _per -component_ basis, set the _view encapsulation mode_ in the component metadata. Choose from the following modes: -- `ShadowDom` view encapsulation uses the browser's built-in shadow DOM implementation (see - [Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM)) - to attach a shadow DOM to the component's host element, and then puts the component - view inside that shadow DOM. The component's styles are included within the shadow DOM. +- `ViewEncapsulation.ShadowDom`, Angular uses the browser's built-in [Shadow DOM API](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM)) 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.Emulated`, Angular modifies the component's CSS selectors so that they are only applied to the component's view and do not affect other elements in the application (_emulating_ Shadow DOM behavior). For more details, see [Inspecting generated CSS](guide/view-encapsulation#inspect-generated-css). -- `Emulated` view encapsulation (the default) emulates the behavior of shadow DOM by preprocessing - (and renaming) the CSS code to effectively scope the CSS to the component's view. - For details, see [Inspecting generated CSS](guide/view-encapsulation#inspect-generated-css). +- `ViewEncapsulation.None`, Angular does not apply any sort of view encapsulation meaning that any styles specified + for the component are actually globally applied and can affect any HTML element present within the application. + This mode is essentially the same as including the styles into the HTML itself. -- `None` means that Angular does no view encapsulation. - Angular adds the CSS to the global styles. - The scoping rules, isolations, and protections discussed earlier don't apply. - This mode is essentially the same as pasting the component's styles into the HTML. +
@@ -116,14 +109,12 @@ The second example shows a component that has `ViewEncapsulation.Emulated`. This
@@ -144,8 +135,8 @@ The styles added by the `ShadowDomEncapsulationComponent` component are availabl
The `EmulatedEncapsulationComponent` has specific "scoped" styles, so the styling of this component's template is unaffected.
But since styles from `ShadowDomEncapsulationComponent` are added to the shadow host after the global styles, the `h2` style overrides the style from the `NoEncapsulationComponent`.
-The result is that the `
-@reviewed 2021-09-17
+@reviewed 2021-11-10
diff --git a/packages/compiler/src/render3/partial/api.ts b/packages/compiler/src/render3/partial/api.ts
index 3009c773715b..03f5428d1629 100644
--- a/packages/compiler/src/render3/partial/api.ts
+++ b/packages/compiler/src/render3/partial/api.ts
@@ -177,7 +177,7 @@ export interface R3DeclareComponentMetadata extends R3DeclareDirectiveMetadata {
changeDetection?: ChangeDetectionStrategy;
/**
- * An encapsulation policy for the template and CSS styles.
+ * An encapsulation policy for the component's styling.
* Defaults to `ViewEncapsulation.Emulated`.
*/
encapsulation?: ViewEncapsulation;
diff --git a/packages/compiler/src/render3/view/api.ts b/packages/compiler/src/render3/view/api.ts
index 64c7cc29efe8..c12ec91acf20 100644
--- a/packages/compiler/src/render3/view/api.ts
+++ b/packages/compiler/src/render3/view/api.ts
@@ -206,11 +206,12 @@ export interface R3ComponentMetadata extends R3DirectiveMetadata {
styles: string[];
/**
- * An encapsulation policy for the template and CSS styles. One of:
- * - `ViewEncapsulation.Emulated`: Use shimmed CSS that emulates the native behavior.
- * - `ViewEncapsulation.None`: Use global CSS without any encapsulation.
- * - `ViewEncapsulation.ShadowDom`: Use the latest ShadowDOM API to natively encapsulate styles
- * into a shadow root.
+ * An encapsulation policy for the component's styling.
+ * Possible values:
+ * - `ViewEncapsulation.Emulated`: Apply modified component styles in order to emulate
+ * a native Shadow DOM CSS encapsulation behavior.
+ * - `ViewEncapsulation.None`: Apply component styles globally without any sort of encapsulation.
+ * - `ViewEncapsulation.ShadowDom`: Use the browser's native Shadow DOM API to encapsulate styles.
*/
encapsulation: ViewEncapsulation;
diff --git a/packages/core/src/metadata/directives.ts b/packages/core/src/metadata/directives.ts
index 660b1d72d795..25f8103aef38 100644
--- a/packages/core/src/metadata/directives.ts
+++ b/packages/core/src/metadata/directives.ts
@@ -517,18 +517,19 @@ export interface Component extends Directive {
animations?: any[];
/**
- * An encapsulation policy for the template and CSS styles. One of:
- * - `ViewEncapsulation.Emulated`: Use shimmed CSS that
- * emulates the native behavior.
- * - `ViewEncapsulation.None`: Use global CSS without any
- * encapsulation.
- * - `ViewEncapsulation.ShadowDom`: Use Shadow DOM v1 to encapsulate styles.
- *
- * If not supplied, the value is taken from `CompilerOptions`. The default compiler option is
- * `ViewEncapsulation.Emulated`.
- *
- * If the policy is set to `ViewEncapsulation.Emulated` and the component has no `styles`
- * or `styleUrls` specified, the policy is automatically switched to `ViewEncapsulation.None`.
+ * An encapsulation policy for the component's styling.
+ * Possible values:
+ * - `ViewEncapsulation.Emulated`: Apply modified component styles in order to emulate
+ * a native Shadow DOM CSS encapsulation behavior.
+ * - `ViewEncapsulation.None`: Apply component styles globally without any sort of encapsulation.
+ * - `ViewEncapsulation.ShadowDom`: Use the browser's native Shadow DOM API to encapsulate styles.
+ *
+ * If not supplied, the value is taken from the `CompilerOptions`
+ * which defaults to `ViewEncapsulation.Emulated`.
+ *
+ * If the policy is `ViewEncapsulation.Emulated` and the component has no
+ * {@link Component#styles styles} nor {@link Component#styleUrls styleUrls},
+ * the policy is automatically switched to `ViewEncapsulation.None`.
*/
encapsulation?: ViewEncapsulation;
diff --git a/packages/core/src/metadata/view.ts b/packages/core/src/metadata/view.ts
index 5cc72c69662c..04838345b191 100644
--- a/packages/core/src/metadata/view.ts
+++ b/packages/core/src/metadata/view.ts
@@ -7,7 +7,8 @@
*/
/**
- * Defines template and style encapsulation options available for Component's {@link Component}.
+ * Defines the CSS styles encapsulation policies for the {@link Component} decorator's
+ * `encapsulation` option.
*
* See {@link Component#encapsulation encapsulation}.
*
@@ -23,10 +24,9 @@ export enum ViewEncapsulation {
// https://github.com/angular/angular/issues/44119 for additional information.
/**
- * Emulate `Native` scoping of styles by adding an attribute containing surrogate id to the Host
- * Element and pre-processing the style rules provided via {@link Component#styles styles} or
- * {@link Component#styleUrls styleUrls}, and adding the new Host Element attribute to all
- * selectors.
+ * Emulates a native Shadow DOM encapsulation behavior by adding a specific attribute to the
+ * component's host element and applying the same attribute to all the CSS selectors provided
+ * via {@link Component#styles styles} or {@link Component#styleUrls styleUrls}.
*
* This is the default option.
*/
@@ -35,16 +35,16 @@ export enum ViewEncapsulation {
// Historically the 1 value was for `Native` encapsulation which has been removed as of v11.
/**
- * Don't provide any template or style encapsulation.
+ * Doesn't provide any sort of CSS style encapsulation, meaning that all the styles provided
+ * via {@link Component#styles styles} or {@link Component#styleUrls styleUrls} are applicable
+ * to any HTML element of the application regardless of their host Component.
*/
None = 2,
/**
- * Use Shadow DOM to encapsulate styles.
- *
- * For the DOM this means using modern [Shadow
- * DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM) and
- * creating a ShadowRoot for Component's Host Element.
+ * Uses the browser's native Shadow DOM API to encapsulate CSS styles, meaning that it creates
+ * a ShadowRoot for the component's host element which is then used to encapsulate
+ * all the Component's styling.
*/
ShadowDom = 3
}