Skip to content

Commit 6ae3858

Browse files
dario-piotrowiczthePunderWoman
authored andcommitted
docs(core): improve viewEncapsulation documentation (angular#44151)
Slighlty improve the `viewEncapsulation` documentation (both in code comments and content files) to make it more clear and understandable. See angular#44099 (comment) PR Close angular#44151
1 parent 2bf131a commit 6ae3858

6 files changed

Lines changed: 74 additions & 87 deletions

File tree

aio/content/examples/component-styles/src/app/quest-summary.component.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,3 @@ import { Component } from '@angular/core';
1010
})
1111
export class QuestSummaryComponent { }
1212
// #enddocregion
13-
/*
14-
// #docregion encapsulation.shadow
15-
// warning: not all browsers support shadow DOM encapsulation at this time
16-
encapsulation: ViewEncapsulation.ShadowDom
17-
// #enddocregion encapsulation.shadow
18-
*/
Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
# View encapsulation
22

3-
In Angular, component CSS styles are encapsulated into the component's view and don't
4-
affect the rest of the application.
3+
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.
4+
5+
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.
56

6-
To control how this encapsulation happens on a _per
7-
component_ basis, set the _view encapsulation mode_ in the component metadata.
87
Choose from the following modes:
98

10-
- `ShadowDom` view encapsulation uses the browser's built-in shadow DOM implementation (see
11-
[Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM))
12-
to attach a shadow DOM to the component's host element, and then puts the component
13-
view inside that shadow DOM. The component's styles are included within the shadow DOM.
9+
- `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.
10+
11+
- `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).
1412

15-
- `Emulated` view encapsulation (the default) emulates the behavior of shadow DOM by preprocessing
16-
(and renaming) the CSS code to effectively scope the CSS to the component's view.
17-
For details, see [Inspecting generated CSS](guide/view-encapsulation#inspect-generated-css).
13+
- `ViewEncapsulation.None`, Angular does not apply any sort of view encapsulation meaning that any styles specified
14+
for the component are actually globally applied and can affect any HTML element present within the application.
15+
This mode is essentially the same as including the styles into the HTML itself.
1816

19-
- `None` means that Angular does no view encapsulation.
20-
Angular adds the CSS to the global styles.
21-
The scoping rules, isolations, and protections discussed earlier don't apply.
22-
This mode is essentially the same as pasting the component's styles into the HTML.
17+
<div class="alert is-important">
2318

24-
To set the component's encapsulation mode, use the `encapsulation` property in the component metadata:
19+
`ViewEncapsulation.ShadowDom` only works on browsers that have built-in support
20+
for the shadow DOM (see [Can I use - Shadow DOM v1](https://caniuse.com/shadowdomv1)).
21+
Not all browsers support it, which is why the `ViewEncapsulation.Emulated` is the recommended and default mode.
2522

26-
<code-example path="component-styles/src/app/quest-summary.component.ts" region="encapsulation.shadow" header="src/app/quest-summary.component.ts"></code-example>
23+
</div>
2724

28-
`ShadowDom` view encapsulation only works on browsers that have built-in support
29-
for shadow DOM (see [Can I use - Shadow DOM v1](https://caniuse.com/shadowdomv1)).
30-
The support is still limited, which is why `Emulated` view encapsulation is the default mode and recommended in most cases.
3125

3226
{@a inspect-generated-css}
3327

3428
## Inspecting generated CSS
3529

36-
When using emulated view encapsulation, Angular preprocesses
37-
all component styles so that they approximate the standard shadow CSS scoping rules.
30+
When using the emulated view encapsulation, Angular pre-processes all the component's styles so that they are only applied to the component's view.
3831

39-
In the DOM of a running Angular application with emulated view
40-
encapsulation enabled, each DOM element has some extra attributes
41-
attached to it:
32+
In the DOM of a running Angular application, elements belonging to components using emulated view encapsulation have some extra attributes attached to them:
4233

43-
<code-example format="">
34+
<code-example format="html" language="html">
4435
&lt;hero-details _nghost-pmm-5>
4536
&lt;h2 _ngcontent-pmm-5>Mister Fantastic&lt;/h2>
4637
&lt;hero-team _ngcontent-pmm-5 _nghost-pmm-6>
@@ -49,18 +40,16 @@ attached to it:
4940
&lt;/hero-detail>
5041
</code-example>
5142

52-
There are two kinds of generated attributes:
43+
There are two kinds of such attributes:
44+
45+
- `_nghost` attributes are added to elements that enclose a component's view and that would be ShadowRoots in a native Shadow DOM encapsulation. This is typically the case for components' host elements.
46+
- `_ngcontent` attributes are added to child element within a component's view, those are used to match the elements with their respective emulated ShadowRoots (host elements with a matching `_nghost` attribute).
5347

54-
- An element that would be a shadow DOM host in native encapsulation has a
55-
generated `_nghost` attribute. This is typically the case for component host elements.
56-
- An element within a component's view has a `_ngcontent` attribute
57-
that identifies to which host's emulated shadow DOM this element belongs.
48+
The exact values of these attributes are a private implementation detail of Angular. They are automatically generated and you should never refer to them in application code.
5849

59-
The exact values of these attributes aren't important. They are automatically
60-
generated and you should never refer to them in application code. But they are targeted
61-
by the generated component styles, which are in the `<head>` section of the DOM:
50+
They are targeted by the generated component styles, which are injected in the `<head>` section of the DOM:
6251

63-
<code-example format="">
52+
<code-example format="css" language="css">
6453
[_nghost-pmm-5] {
6554
display: block;
6655
border: 1px solid black;
@@ -72,25 +61,29 @@ h3[_ngcontent-pmm-6] {
7261
}
7362
</code-example>
7463

75-
These styles are post-processed so that each selector is augmented
76-
with `_nghost` or `_ngcontent` attribute selectors.
77-
These extra selectors enable the scoping rules described in this page.
64+
These styles are post-processed so that each CSS selector is augmented with the appropriate `_nghost` or `_ngcontent` attribute. These modified selectors make sure the styles to be applied to components' views in an isolated and targeted fashion.
7865

7966
## Mixing encapsulation modes
8067

81-
Avoid mixing components that use different view encapsulation. Where it is necessary, you should be aware of how the component styles will interact.
68+
As previously mentioned you specify the encapsulation mode in the Component's decorator on a _per component_ basis, this means that within your application you can have different components using different encapsulation strategies.
69+
70+
Although possible, this is not recommended. If it is really needed you should be aware of how the styles of components using different encapsulation modes will interact with each other:
71+
72+
- The styles of components with `ViewEncapsulation.Emulated` are added to the `<head>` of the document, making them available throughout the application, but their selectors only affect elements within their respective components' templates.
73+
74+
- The styles of components with `ViewEncapsulation.None` are added to the `<head>` of the document, making them available throughout the application, so are completely global and affect any matching elements within the document.
8275

83-
- The styles of components with `ViewEncapsulation.Emulated` are added to the `<head>` of the document, making them available throughout the application, but are "scoped" so they only affect elements within the component's template.
76+
- The styles of components with `ViewEncapsulation.ShadowDom` are only added to the shadow DOM host, ensuring that they only affect elements within their respective components' views.
8477

85-
- The styles of components with `ViewEncapsulation.None` are added to the `<head>` of the document, making them available throughout the application, and are not "scoped" so they can affect any element in the application.
78+
<div class="alert is-helpful">
8679

87-
- The styles of components with `ViewEncapsulation.ShadowDom` are only added to the shadow DOM host, ensuring that they only affect elements within the component's template.
80+
Styles of `ViewEncapsulation.Emulated` and `ViewEncapsulation.None` components are also added to the shadow DOM host of each `ViewEncapsulation.ShadowDom` component.
8881

89-
**All the styles for `ViewEncapsulation.Emulated` and `ViewEncapsulation.None` components are also added to the shadow DOM host of each `ViewEncapsulation.ShadowDom` component.**
82+
This means that styles for components with `ViewEncapsulation.None` will affect matching elements within shadow DOMs.
9083

91-
The result is that styling for components with `ViewEncapsulation.None` will affect matching elements within the shadow DOM.
84+
This approach may seem counter-intuitive at first, but without it a component with `ViewEncapsulation.None` would be rendered differently within a component using `ViewEncapsulation.ShadowDom`, since its styles would not be available.
9285

93-
This approach may seem counter-intuitive at first, but without it a component with `ViewEncapsulation.None` could not be used within a component with `ViewEncapsulation.ShadowDom`, since its styles would not be available.
86+
</div>
9487

9588
### Examples
9689

@@ -106,7 +99,7 @@ The first example shows a component that has `ViewEncapsulation.None`. This comp
10699

107100
Angular adds the styles for this component as global styles to the `<head>` of the document.
108101

109-
**Angular also adds the styles to all shadow DOM hosts.** Therefore, the styles are available throughout the application.
102+
As already mentioned Angular also adds the styles to all shadow DOM hosts. Therefore, the styles are available throughout the whole application.
110103

111104
<img src="generated/images/guide/view-encapsulation/no-encapsulation.png" alt="component with no encapsulation">
112105

@@ -116,14 +109,12 @@ The second example shows a component that has `ViewEncapsulation.Emulated`. This
116109

117110
<code-example path="view-encapsulation/src/app/emulated-encapsulation.component.ts" header="src/app/emulated-encapsulation.component.ts"></code-example>>
118111

119-
Similar to `ViewEncapsulation.None`, Angular adds the styles for this component to the `<head>` of the document, and to all the shadow DOM hosts.
120-
But in this case, the styles are "scoped" by the attributes described in ["Inspecting generated CSS"](#inspecting-generated-css).
112+
Similar to `ViewEncapsulation.None`, Angular adds the styles for this component to the `<head>` of the document, but with "scoped" styles.
121113

122114
Therefore, only the elements directly within this component's template will match its styles.
123115
Since the "scoped" styles from the `EmulatedEncapsulationComponent` are very specific, they override the global styles from the `NoEncapsulationComponent`.
124116

125-
In this example, the `EmulatedEncapsulationComponent` contains a `NoEncapsulationComponent`.
126-
The `NoEncapsulationComponent` is styled as expected because the scoped styles do not match elements in its template.
117+
In this example, the `EmulatedEncapsulationComponent` contains a `NoEncapsulationComponent`, but `NoEncapsulationComponent` is still styled as expected since the `EmulatedEncapsulationComponent`'s "scoped" styles do not match elements in its template.
127118

128119
<img src="generated/images/guide/view-encapsulation/emulated-encapsulation.png" alt="component with no encapsulation">
129120

@@ -144,8 +135,8 @@ The styles added by the `ShadowDomEncapsulationComponent` component are availabl
144135
The `EmulatedEncapsulationComponent` has specific "scoped" styles, so the styling of this component's template is unaffected.
145136

146137
But since styles from `ShadowDomEncapsulationComponent` are added to the shadow host after the global styles, the `h2` style overrides the style from the `NoEncapsulationComponent`.
147-
The result is that the `<h2>` element in the `NoEncapsulationComponent` is colored blue rather than red, which may not be what the component author intended.
138+
The result is that the `<h2>` element in the `NoEncapsulationComponent` is colored blue rather than red, which may not be what the component's author intended.
148139

149140
<img src="generated/images/guide/view-encapsulation/shadow-dom-encapsulation.png" alt="component with no encapsulation">
150141

151-
@reviewed 2021-09-17
142+
@reviewed 2021-11-10

packages/compiler/src/render3/partial/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export interface R3DeclareComponentMetadata extends R3DeclareDirectiveMetadata {
177177
changeDetection?: ChangeDetectionStrategy;
178178

179179
/**
180-
* An encapsulation policy for the template and CSS styles.
180+
* An encapsulation policy for the component's styling.
181181
* Defaults to `ViewEncapsulation.Emulated`.
182182
*/
183183
encapsulation?: ViewEncapsulation;

packages/compiler/src/render3/view/api.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,12 @@ export interface R3ComponentMetadata extends R3DirectiveMetadata {
206206
styles: string[];
207207

208208
/**
209-
* An encapsulation policy for the template and CSS styles. One of:
210-
* - `ViewEncapsulation.Emulated`: Use shimmed CSS that emulates the native behavior.
211-
* - `ViewEncapsulation.None`: Use global CSS without any encapsulation.
212-
* - `ViewEncapsulation.ShadowDom`: Use the latest ShadowDOM API to natively encapsulate styles
213-
* into a shadow root.
209+
* An encapsulation policy for the component's styling.
210+
* Possible values:
211+
* - `ViewEncapsulation.Emulated`: Apply modified component styles in order to emulate
212+
* a native Shadow DOM CSS encapsulation behavior.
213+
* - `ViewEncapsulation.None`: Apply component styles globally without any sort of encapsulation.
214+
* - `ViewEncapsulation.ShadowDom`: Use the browser's native Shadow DOM API to encapsulate styles.
214215
*/
215216
encapsulation: ViewEncapsulation;
216217

packages/core/src/metadata/directives.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -516,18 +516,19 @@ export interface Component extends Directive {
516516
animations?: any[];
517517

518518
/**
519-
* An encapsulation policy for the template and CSS styles. One of:
520-
* - `ViewEncapsulation.Emulated`: Use shimmed CSS that
521-
* emulates the native behavior.
522-
* - `ViewEncapsulation.None`: Use global CSS without any
523-
* encapsulation.
524-
* - `ViewEncapsulation.ShadowDom`: Use Shadow DOM v1 to encapsulate styles.
525-
*
526-
* If not supplied, the value is taken from `CompilerOptions`. The default compiler option is
527-
* `ViewEncapsulation.Emulated`.
528-
*
529-
* If the policy is set to `ViewEncapsulation.Emulated` and the component has no `styles`
530-
* or `styleUrls` specified, the policy is automatically switched to `ViewEncapsulation.None`.
519+
* An encapsulation policy for the component's styling.
520+
* Possible values:
521+
* - `ViewEncapsulation.Emulated`: Apply modified component styles in order to emulate
522+
* a native Shadow DOM CSS encapsulation behavior.
523+
* - `ViewEncapsulation.None`: Apply component styles globally without any sort of encapsulation.
524+
* - `ViewEncapsulation.ShadowDom`: Use the browser's native Shadow DOM API to encapsulate styles.
525+
*
526+
* If not supplied, the value is taken from the `CompilerOptions`
527+
* which defaults to `ViewEncapsulation.Emulated`.
528+
*
529+
* If the policy is `ViewEncapsulation.Emulated` and the component has no
530+
* {@link Component#styles styles} nor {@link Component#styleUrls styleUrls},
531+
* the policy is automatically switched to `ViewEncapsulation.None`.
531532
*/
532533
encapsulation?: ViewEncapsulation;
533534

packages/core/src/metadata/view.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88

99
/**
10-
* Defines template and style encapsulation options available for Component's {@link Component}.
10+
* Defines the CSS styles encapsulation policies for the {@link Component} decorator's
11+
* `encapsulation` option.
1112
*
1213
* See {@link Component#encapsulation encapsulation}.
1314
*
@@ -23,10 +24,9 @@ export enum ViewEncapsulation {
2324
// https://github.com/angular/angular/issues/44119 for additional information.
2425

2526
/**
26-
* Emulate `Native` scoping of styles by adding an attribute containing surrogate id to the Host
27-
* Element and pre-processing the style rules provided via {@link Component#styles styles} or
28-
* {@link Component#styleUrls styleUrls}, and adding the new Host Element attribute to all
29-
* selectors.
27+
* Emulates a native Shadow DOM encapsulation behavior by adding a specific attribute to the
28+
* component's host element and applying the same attribute to all the CSS selectors provided
29+
* via {@link Component#styles styles} or {@link Component#styleUrls styleUrls}.
3030
*
3131
* This is the default option.
3232
*/
@@ -35,16 +35,16 @@ export enum ViewEncapsulation {
3535
// Historically the 1 value was for `Native` encapsulation which has been removed as of v11.
3636

3737
/**
38-
* Don't provide any template or style encapsulation.
38+
* Doesn't provide any sort of CSS style encapsulation, meaning that all the styles provided
39+
* via {@link Component#styles styles} or {@link Component#styleUrls styleUrls} are applicable
40+
* to any HTML element of the application regardless of their host Component.
3941
*/
4042
None = 2,
4143

4244
/**
43-
* Use Shadow DOM to encapsulate styles.
44-
*
45-
* For the DOM this means using modern [Shadow
46-
* DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM) and
47-
* creating a ShadowRoot for Component's Host Element.
45+
* Uses the browser's native Shadow DOM API to encapsulate CSS styles, meaning that it creates
46+
* a ShadowRoot for the component's host element which is then used to encapsulate
47+
* all the Component's styling.
4848
*/
4949
ShadowDom = 3
5050
}

0 commit comments

Comments
 (0)