Skip to content

Commit d24d574

Browse files
ryan-bendelcrisbeto
authored andcommitted
feat(platform-browser): Add IsolatedShadowDom encapsulation method (#62723)
IsolatedShadowDom encapsulation fixes style leakage in Shadowdom encapsulation by removing sharedstyleshost from dom-renderer IsolatedShadowdom class. Updates docs. PR Close #62723
1 parent fd5fcf3 commit d24d574

File tree

20 files changed

+190
-39
lines changed

20 files changed

+190
-39
lines changed

adev/shared-docs/pipeline/api-gen/rendering/test/fake-cli-entries.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@
177177
"enum": [
178178
"Emulated",
179179
"None",
180-
"ShadowDom"
180+
"ShadowDom",
181+
"IsolatedShadowDom"
181182
],
182183
"description": "The view encapsulation strategy to use in the new application."
183184
}
@@ -367,7 +368,8 @@
367368
"enum": [
368369
"Emulated",
369370
"None",
370-
"ShadowDom"
371+
"ShadowDom",
372+
"IsolatedShadowDom"
371373
],
372374
"description": "The view encapsulation strategy to use in the new component."
373375
}

adev/src/app/features/update/recommendations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ export const RECOMMENDATIONS: Step[] = [
10321032
level: ApplicationComplexity.Advanced,
10331033
step: 'viewencapsulation native removed',
10341034
action:
1035-
'The component view encapsulation option `ViewEncapsulation.Native` has been removed. Use `ViewEncapsulation.ShadowDom` instead. `ng update` will migrate you automatically.',
1035+
'The component view encapsulation option `ViewEncapsulation.Native` has been removed. Use `ViewEncapsulation.ShadowDom` or `ViewEncapsulation.IsolatedShadowDom` instead. `ng update` will migrate you automatically.',
10361036
},
10371037
{
10381038
possibleIn: 1100,

adev/src/content/ai/mcp-server-setup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ This tool lists all the applications and libraries in your current Angular works
9090

9191
## Feedback and New Ideas
9292

93-
The Angular team welcomes your feedback on the existing MCP capabilities and any ideas you have for new tools or features. Please share your thoughts by opening an issue on the [angular/angular GitHub repository](https://github.com/angular/angular/issues).
93+
The Angular team welcomes your feedback on the existing MCP capabilities and any ideas you have for new tools or features. Please share your thoughts by opening an issue on the [angular/angular GitHub repository](https://github.com/angular/angular/issues).

adev/src/content/cli/help/generate.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,4 +998,4 @@
998998
"deprecated": false
999999
}
10001000
]
1001-
}
1001+
}

adev/src/content/cli/help/new.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,4 @@
195195
"description": "Create an initial application that does not utilize `zone.js`."
196196
}
197197
]
198-
}
198+
}

adev/src/content/guide/animations/complex-sequences.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ IMPORTANT: If you need to animate the items of an `*ngFor` list and there is a p
121121

122122
## Animations and Component View Encapsulation
123123

124-
Angular animations are based on the components DOM structure and do not directly take [View Encapsulation](guide/components/styling#style-scoping) into account, this means that components using `ViewEncapsulation.Emulated` behave exactly as if they were using `ViewEncapsulation.None` (`ViewEncapsulation.ShadowDom` behaves differently as we'll discuss shortly).
124+
Angular animations are based on the components DOM structure and do not directly take [View Encapsulation](guide/components/styling#style-scoping) into account, this means that components using `ViewEncapsulation.Emulated` behave exactly as if they were using `ViewEncapsulation.None` (`ViewEncapsulation.ShadowDom` and `ViewEncapsulation.IsolatedShadowDom` behave differently as we'll discuss shortly).
125125

126126
For example if the `query()` function (which you'll see more of in the rest of the Animations guide) were to be applied at the top of a tree of components using the emulated view encapsulation, such query would be able to identify (and thus animate) DOM elements on any depth of the tree.
127127

128-
On the other hand the `ViewEncapsulation.ShadowDom` changes the component's DOM structure by "hiding" DOM elements inside [`ShadowRoot`](https://developer.mozilla.org/docs/Web/API/ShadowRoot) elements. Such DOM manipulations do prevent some of the animations implementation to work properly since it relies on simple DOM structures and doesn't take `ShadowRoot` elements into account. Therefore it is advised to avoid applying animations to views incorporating components using the ShadowDom view encapsulation.
128+
On the other hand the `ViewEncapsulation.ShadowDom` and `ViewEncapsulation.IsolatedShadowDom` changes the component's DOM structure by "hiding" DOM elements inside [`ShadowRoot`](https://developer.mozilla.org/docs/Web/API/ShadowRoot) elements. Such DOM manipulations do prevent some of the animations implementation to work properly since it relies on simple DOM structures and doesn't take `ShadowRoot` elements into account. Therefore it is advised to avoid applying animations to views incorporating components using the ShadowDom view encapsulation.
129129

130130
## Animation sequence summary
131131

@@ -141,4 +141,4 @@ You might also be interested in the following:
141141
<docs-pill href="guide/animations/transition-and-triggers" title="Transition and triggers"/>
142142
<docs-pill href="guide/animations/reusable-animations" title="Reusable animations"/>
143143
<docs-pill href="guide/routing/route-transition-animations" title="Route transition animations"/>
144-
</docs-pill-row>
144+
</docs-pill-row>

adev/src/content/guide/components/styling.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ and [stylus](https://stylus-lang.com).
3636
## Style scoping
3737

3838
Every component has a **view encapsulation** setting that determines how the framework scopes a
39-
component's styles. There are three view encapsulation modes: `Emulated`, `ShadowDom`, and `None`.
39+
component's styles. There are four view encapsulation modes: `Emulated`, `ShadowDom`, `IsolatedShadowDom`, and `None`.
4040
You can specify the mode in the `@Component` decorator:
4141

4242
<docs-code language="angular-ts" highlight="[3]">
@@ -82,16 +82,20 @@ using [the web standard Shadow DOM API](https://developer.mozilla.org/docs/Web/W
8282
When enabling this mode, Angular attaches a shadow root to the component's host element and renders
8383
the component's template and styles into the corresponding shadow tree.
8484

85-
This mode strictly guarantees that _only_ that component's styles apply to elements in the
86-
component's template. Global styles cannot affect elements in a shadow tree and styles inside the
87-
shadow tree cannot affect elements outside of that shadow tree.
85+
Styles inside the shadow tree cannot affect elements outside of that shadow tree.
8886

8987
Enabling `ShadowDom` encapsulation, however, impacts more than style scoping. Rendering the
9088
component in a shadow tree affects event propagation, interaction
9189
with [the `<slot>` API](https://developer.mozilla.org/docs/Web/Web_Components/Using_templates_and_slots),
9290
and how browser developer tools show elements. Always understand the full implications of using
9391
Shadow DOM in your application before enabling this option.
9492

93+
### ViewEncapsulation.IsolatedShadowDom
94+
95+
Behaves as above, except this mode strictly guarantees that _only_ that component's styles apply to elements in the
96+
component's template. Global styles cannot affect elements in a shadow tree and styles inside the
97+
shadow tree cannot affect elements outside of that shadow tree.
98+
9599
### ViewEncapsulation.None
96100

97101
This mode disables all style encapsulation for the component. Any styles associated with the
@@ -114,4 +118,4 @@ use [the `<link>` element](https://developer.mozilla.org/docs/Web/HTML/Element/l
114118
reference CSS files. Additionally, your CSS may
115119
use [the `@import`at-rule](https://developer.mozilla.org/docs/Web/CSS/@import) to reference
116120
CSS files. Angular treats these references as _external_ styles. External styles are not affected by
117-
emulated view encapsulation.
121+
emulated view encapsulation.

adev/src/content/reference/migrations/overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ Learn about how you can migrate your existing angular project to the latest feat
3030
<docs-card title="Self-closing tags" link="Migrate now" href="reference/migrations/self-closing-tags">
3131
Convert component templates to use self-closing tags where possible.
3232
</docs-card>
33-
</docs-card-container>
33+
</docs-card-container>

devtools/projects/ng-devtools/src/lib/devtools-tabs/directive-explorer/property-tab/component-metadata.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class ComponentMetadataComponent {
3636

3737
private _nestedProps = inject(ElementPropertyResolver);
3838

39-
angularViewEncapsulationModes = ['Emulated', 'Native', 'None', 'ShadowDom'];
39+
angularViewEncapsulationModes = ['Emulated', 'Native', 'None', 'ShadowDom', 'IsolatedShadowDom'];
4040
acxViewEncapsulationModes = ['Emulated', 'None'];
4141

4242
readonly controller = computed(() => {

goldens/public-api/core/index.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,7 @@ export abstract class ViewContainerRef {
20222022
// @public
20232023
export enum ViewEncapsulation {
20242024
Emulated = 0,
2025+
IsolatedShadowDom = 4,
20252026
None = 2,
20262027
ShadowDom = 3
20272028
}

0 commit comments

Comments
 (0)