Skip to content

Commit bc69182

Browse files
David Shevitzjosephperrott
David Shevitz
authored andcommitted
docs: Migrate section, view encapsulation, from Component Styles topic into its own topic. (angular#38986)
PR Close angular#38986
1 parent 82d54fe commit bc69182

File tree

4 files changed

+90
-87
lines changed

4 files changed

+90
-87
lines changed

.pullapprove.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ groups:
324324
'aio/content/examples/component-interaction/**',
325325
'aio/content/images/guide/component-interaction/**',
326326
'aio/content/guide/component-styles.md',
327+
'aio/content/guide/view-encapsulation.md',
327328
'aio/content/examples/component-styles/**',
328329
'aio/content/guide/dependency-injection.md',
329330
'aio/content/examples/dependency-injection/**',

aio/content/guide/component-styles.md

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ The `/deep/` combinator also has the aliases `>>>`, and `::ng-deep`.
119119

120120
Use `/deep/`, `>>>` and `::ng-deep` only with *emulated* view encapsulation.
121121
Emulated is the default and most commonly used view encapsulation. For more information, see the
122-
[Controlling view encapsulation](guide/component-styles#view-encapsulation) section.
122+
[View Encapsulation](guide/view-encapsulation) section.
123123

124124
</div>
125125

@@ -267,89 +267,3 @@ as explained in the [CLI wiki](https://github.com/angular/angular-cli/wiki/stori
267267
Style strings added to the `@Component.styles` array _must be written in CSS_ because the CLI cannot apply a preprocessor to inline styles.
268268

269269
</div>
270-
271-
{@a view-encapsulation}
272-
273-
## View encapsulation
274-
275-
As discussed earlier, component CSS styles are encapsulated into the component's view and don't
276-
affect the rest of the application.
277-
278-
To control how this encapsulation happens on a *per
279-
component* basis, you can set the *view encapsulation mode* in the component metadata.
280-
Choose from the following modes:
281-
282-
* `ShadowDom` view encapsulation uses the browser's native shadow DOM implementation (see
283-
[Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM)
284-
on the [MDN](https://developer.mozilla.org) site)
285-
to attach a shadow DOM to the component's host element, and then puts the component
286-
view inside that shadow DOM. The component's styles are included within the shadow DOM.
287-
288-
* `Native` view encapsulation uses a now deprecated version of the browser's native shadow DOM implementation - [learn about the changes](https://hayato.io/2016/shadowdomv1/).
289-
290-
* `Emulated` view encapsulation (the default) emulates the behavior of shadow DOM by preprocessing
291-
(and renaming) the CSS code to effectively scope the CSS to the component's view.
292-
For details, see [Inspecting generated CSS](guide/component-styles#inspect-generated-css) below.
293-
294-
* `None` means that Angular does no view encapsulation.
295-
Angular adds the CSS to the global styles.
296-
The scoping rules, isolations, and protections discussed earlier don't apply.
297-
This is essentially the same as pasting the component's styles into the HTML.
298-
299-
To set the components encapsulation mode, use the `encapsulation` property in the component metadata:
300-
301-
<code-example path="component-styles/src/app/quest-summary.component.ts" region="encapsulation.native" header="src/app/quest-summary.component.ts"></code-example>
302-
303-
`ShadowDom` view encapsulation only works on browsers that have native support
304-
for shadow DOM (see [Shadow DOM v1](https://caniuse.com/#feat=shadowdomv1) on the
305-
[Can I use](http://caniuse.com) site). The support is still limited,
306-
which is why `Emulated` view encapsulation is the default mode and recommended
307-
in most cases.
308-
309-
{@a inspect-generated-css}
310-
311-
## Inspecting generated CSS
312-
313-
When using emulated view encapsulation, Angular preprocesses
314-
all component styles so that they approximate the standard shadow CSS scoping rules.
315-
316-
In the DOM of a running Angular application with emulated view
317-
encapsulation enabled, each DOM element has some extra attributes
318-
attached to it:
319-
320-
<code-example format="">
321-
&lt;hero-details _nghost-pmm-5>
322-
&lt;h2 _ngcontent-pmm-5>Mister Fantastic&lt;/h2>
323-
&lt;hero-team _ngcontent-pmm-5 _nghost-pmm-6>
324-
&lt;h3 _ngcontent-pmm-6>Team&lt;/h3>
325-
&lt;/hero-team>
326-
&lt;/hero-detail>
327-
328-
</code-example>
329-
330-
There are two kinds of generated attributes:
331-
332-
* An element that would be a shadow DOM host in native encapsulation has a
333-
generated `_nghost` attribute. This is typically the case for component host elements.
334-
* An element within a component's view has a `_ngcontent` attribute
335-
that identifies to which host's emulated shadow DOM this element belongs.
336-
337-
The exact values of these attributes aren't important. They are automatically
338-
generated and you never refer to them in application code. But they are targeted
339-
by the generated component styles, which are in the `<head>` section of the DOM:
340-
341-
<code-example format="">
342-
[_nghost-pmm-5] {
343-
display: block;
344-
border: 1px solid black;
345-
}
346-
347-
h3[_ngcontent-pmm-6] {
348-
background-color: white;
349-
border: 1px solid #777;
350-
}
351-
</code-example>
352-
353-
These styles are post-processed so that each selector is augmented
354-
with `_nghost` or `_ngcontent` attribute selectors.
355-
These extra selectors enable the scoping rules described in this page.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# View encapsulation
2+
3+
In Angular, component CSS styles are encapsulated into the component's view and don't
4+
affect the rest of the application.
5+
6+
To control how this encapsulation happens on a *per
7+
component* basis, you can set the *view encapsulation mode* in the component metadata.
8+
Choose from the following modes:
9+
10+
* `ShadowDom` view encapsulation uses the browser's native shadow DOM implementation (see
11+
[Shadow DOM](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Shadow_DOM)
12+
on the [MDN](https://developer.mozilla.org) site)
13+
to attach a shadow DOM to the component's host element, and then puts the component
14+
view inside that shadow DOM. The component's styles are included within the shadow DOM.
15+
16+
* `Native` view encapsulation uses a now deprecated version of the browser's native shadow DOM implementation - [learn about the changes](https://hayato.io/2016/shadowdomv1/).
17+
18+
* `Emulated` view encapsulation (the default) emulates the behavior of shadow DOM by preprocessing
19+
(and renaming) the CSS code to effectively scope the CSS to the component's view.
20+
For details, see [Inspecting generated CSS](guide/view-encapsulation#inspect-generated-css) below.
21+
22+
* `None` means that Angular does no view encapsulation.
23+
Angular adds the CSS to the global styles.
24+
The scoping rules, isolations, and protections discussed earlier don't apply.
25+
This is essentially the same as pasting the component's styles into the HTML.
26+
27+
To set the components encapsulation mode, use the `encapsulation` property in the component metadata:
28+
29+
<code-example path="component-styles/src/app/quest-summary.component.ts" region="encapsulation.native" header="src/app/quest-summary.component.ts"></code-example>
30+
31+
`ShadowDom` view encapsulation only works on browsers that have native support
32+
for shadow DOM (see [Shadow DOM v1](https://caniuse.com/#feat=shadowdomv1) on the
33+
[Can I use](http://caniuse.com) site). The support is still limited,
34+
which is why `Emulated` view encapsulation is the default mode and recommended
35+
in most cases.
36+
37+
{@a inspect-generated-css}
38+
39+
## Inspecting generated CSS
40+
41+
When using emulated view encapsulation, Angular preprocesses
42+
all component styles so that they approximate the standard shadow CSS scoping rules.
43+
44+
In the DOM of a running Angular application with emulated view
45+
encapsulation enabled, each DOM element has some extra attributes
46+
attached to it:
47+
48+
<code-example format="">
49+
&lt;hero-details _nghost-pmm-5>
50+
&lt;h2 _ngcontent-pmm-5>Mister Fantastic&lt;/h2>
51+
&lt;hero-team _ngcontent-pmm-5 _nghost-pmm-6>
52+
&lt;h3 _ngcontent-pmm-6>Team&lt;/h3>
53+
&lt;/hero-team>
54+
&lt;/hero-detail>
55+
56+
</code-example>
57+
58+
There are two kinds of generated attributes:
59+
60+
* An element that would be a shadow DOM host in native encapsulation has a
61+
generated `_nghost` attribute. This is typically the case for component host elements.
62+
* An element within a component's view has a `_ngcontent` attribute
63+
that identifies to which host's emulated shadow DOM this element belongs.
64+
65+
The exact values of these attributes aren't important. They are automatically
66+
generated and you never refer to them in application code. But they are targeted
67+
by the generated component styles, which are in the `<head>` section of the DOM:
68+
69+
<code-example format="">
70+
[_nghost-pmm-5] {
71+
display: block;
72+
border: 1px solid black;
73+
}
74+
75+
h3[_ngcontent-pmm-6] {
76+
background-color: white;
77+
border: 1px solid #777;
78+
}
79+
</code-example>
80+
81+
These styles are post-processed so that each selector is augmented
82+
with `_nghost` or `_ngcontent` attribute selectors.
83+
These extra selectors enable the scoping rules described in this page.

aio/content/navigation.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@
116116
"title": "Component Lifecycle",
117117
"tooltip": "Angular calls lifecycle hook methods on directives and components as it creates, changes, and destroys them."
118118
},
119+
{
120+
"url": "guide/view-encapsulation",
121+
"title": "View Encapsulation",
122+
"tooltip": "Describes how component CSS styles are encapsulated into a component's view."
123+
},
119124
{
120125
"url": "guide/component-interaction",
121126
"title": "Component Interaction",

0 commit comments

Comments
 (0)