You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
- Modifications to Shadow DOM scoping behaviors. This proposal depends on
63
-
existing Shadow DOM behavior as currently defined. Styles defined in a Shadow
64
-
DOM will remain inaccessible to the Light DOM and other Shadow DOMs.
65
62
66
63
## Proposal - Local References for Link Rel Tags
67
64
@@ -148,6 +145,85 @@ the shadow root where they are defined, as illustrated by the following examples
148
145
Styles defined inside the sibling Shadow Root are not applied, so "Inside Sibling Shadow DOM" is not blue.
149
146
</p>
150
147
148
+
In contrast to this proposal, [Declarative CSS Modules](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/ShadowDOM/explainer.md) always have global scope. Shadow DOM scoping could be modified though other means, as discussed
149
+
in [this thread](https://github.com/whatwg/html/issues/11364). All of the suggestions in that thread would
150
+
allow for this feature to work with any shadow root, regardless of scope.
151
+
152
+
For example, the "Referencetarget inspired solution on `<template>`" would work with this feature as follows:
153
+
154
+
```html
155
+
<templateexportids="foo"><!-- Exports 'foo' to the light DOM -->
156
+
<templateexportids="foo, bar">
157
+
<styleid="foo">
158
+
...
159
+
</style>
160
+
<style id="bar">
161
+
...
162
+
</style>
163
+
</template>
164
+
<link rel="stylesheet" href="#bar"> <!-- 'bar' is in scope due to `exportids` -->
165
+
</template>
166
+
<link rel="stylesheet" href="#foo"><!-- 'foo' is in the global scope, reference matched -->
167
+
<link rel="stylesheet" href="#bar"><!-- 'bar' is not in scope, reference not matched -->
168
+
```
169
+
170
+
The other examples in https://github.com/whatwg/html/issues/11364 also use this proposal as an example
171
+
of how scoping can be expanded for Shadow DOM elements.
172
+
173
+
### Key Differences Between This Proposal And Declarative CSS Modules
174
+
175
+
Both this proposal and [Declarative CSS Modules](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/ShadowDOM/explainer.md)
176
+
allow authors to share inline CSS with Shadow Roots. There are some key differences in both syntax and
177
+
behaviors, as illustrated in the following table:
178
+
179
+
| | Local Reference Link Rel | Declarative CSS Modules |
180
+
| :---: | :---: | :---: |
181
+
| Scope | ⚠️ [Standard DOM scoping](#Scoping) | Global scope |
182
+
| Identifier syntax | Standard HTML IDREF | Module identifier |
183
+
| Attribute used | Standard HTML `href` | New attribute for `identifier` |
184
+
| Uses existing HTML concepts | ✅ Yes | ❌ No |
185
+
| Uses existing module concepts | ❌ No | ✅ Yes |
186
+
| Extensibility | Clean @sheet integration, scope expansion could apply to SVG references | More declarative module types (HTML, SVG, etc.) |
187
+
188
+
### Extensibility
189
+
190
+
This proposal works nicely with CSS `@sheet`, allowing for the Light DOM to define styles that
191
+
only apply to selected Shadow DOM elements, as demonstrated by the following example:
192
+
193
+
```html
194
+
<style id="inline_styles">
195
+
@sheet my_sheet {
196
+
p {
197
+
color: blue;
198
+
}
199
+
}
200
+
</style>
201
+
<p>"my_sheet" isn't imported into the light DOM, so this isn't blue</p>
<p>Inside Shadow DOM - "my_sheet" was included so this text is blue!</p>
205
+
</template>
206
+
```
207
+
208
+
All of proposals mentioned in https://github.com/whatwg/html/issues/11364 would not only benefit this
209
+
feature - SVG references, local anchors, and anything that takes an IDREF would benefit. For instance, SVG could benefit with this scoping extension as follows:
210
+
211
+
```html
212
+
<templateexportids="foo"><!-- Exports 'foo' to the light DOM -->
213
+
<templateexportids="foo">
214
+
<svgheight="100"width="100">
215
+
<circleid="foo"r="45"cx="50"cy="50"fill="red" />
216
+
</svg>
217
+
</template>
218
+
<svgheight="100"width="100">
219
+
<usehref="#foo" /><!-- "foo" is in this shadow scope due to the "exportids" attribute above -->
220
+
</svg>
221
+
</template>
222
+
<svgheight="100"width="100">
223
+
<usehref="#foo" /><!-- "foo" is in the Light DOM scope due to the "exportids" attribute above -->
224
+
</svg>
225
+
```
226
+
151
227
### Fetch Behavior
152
228
153
229
In user agents where the Local References In `<link>` Tags feature is not
This proposal builds on [CSS module scripts](https://web.dev/articles/css-module-scripts), enabling authors to declare a CSS module inline in an HTML file and link it to a DSD using its [module specifier](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules#:~:text=The-,module%20specifier,-provides%20a%20string). A `type=”css-module”` attribute on the `<script>` element would define it as a CSS module script and the specifier attribute would add it to the module cache as if it had been imported. This allows the page to render with the necessary CSS modules attached to the correct scopes without needing to load them multiple times. Note that module maps are global, meaning that modules defined in a Shadow DOM will be accessible throughout the document context.
207
207
```js
208
-
<script type="css-module" specifier="/foo.css">
208
+
<style type="css-module" specifier="foo">
209
209
#content {
210
210
color: red;
211
211
}
212
-
</script>
212
+
</style>
213
213
```
214
-
Given this `<script>` tag, the styles could be applied to a DSD as follows:
214
+
Given this `<style>` tag, the styles could be applied to a DSD as follows:
The shadow root will be created with its `adoptedStyleSheets` containing the `"/foo.css"` CSS module script’s CSSStyleSheet instance. This single `CSSStyleSheet` instance can be shared by any number of shadow roots.
223
+
The shadow root will be created with its `adoptedStyleSheets` containing the `"foo"` CSS module script’s `CSSStyleSheet` instance. This single `CSSStyleSheet` instance can be shared by any number of shadow roots.
224
224
225
225
An inline CSS module script could also be imported in a JavaScript module in the usual way:
226
226
```html
227
227
import styles from '/foo.css' with { type: 'css' };
228
228
```
229
229
Another advantage of this proposal is that it can allow multiple module specifiers in the `adoptedstylesheets` property:
@@ -255,24 +255,7 @@ A global map does come with some tradeoffs, particularly when names collide. Wit
255
255
256
256
### `<script>` vs `<style>` For CSS Modules
257
257
258
-
This document uses the `<script>` tag for defining CSS Modules. Developer feedback has shown a preference for using the `<style>` tag when defining a CSS Module. This makes sense for CSS Modules in isolation, but does not align with other types of modules. The table in the [next section](#other-declarative-modules) details other module types and demonstrates the degree of consistency achieved with `<script>`. Developer feedback is important and should be considered, even at the potential expense of consistency.
259
-
260
-
This is an example of a CSS Module defined with the `<style>` tag:
A compromise could be to support both `<script>` tags and `<style>` tags.
258
+
Earlier versions of this document used the `<script>` tag for declaring CSS Modules. Developer feedback has shown a strong preference for using the `<style>` tag when declaring CSS Modules, so this proposal has been updated accordingly. The `<script>` tag remains a more natural wrapper for [other declarative modules](#other-declarative-modules).
276
259
277
260
### Behavior with script disabled
278
261
@@ -318,13 +301,50 @@ CSS Modules are not the only type of module - there are also JavasScript, JSON,
318
301
| Module type | Script Module | Declarative Module |
| JavaScript |`import { foo } from "./bar.js";`|`<script type="script-module" specifier="/bar.js"></script>`|
321
-
| CSS |`import foo from "./bar.css" with { type: "css" };`|`<script type="css-module" specifier="/bar.css"></script>`|
304
+
| CSS |`import foo from "./bar.css" with { type: "css" };`|`<style type="css-module" specifier="bar"></style>`|
322
305
| JSON |`import foo from "./bar.json" with { type: "json" };`|`<script type="json-module" specifier="/bar.json"></script>`|
323
306
| HTML |`import {foo} from "bar.html" with {type: "html"};`|`<script type="html-module" specifier="/bar.html"></script>`|
324
307
| SVG |`import {foo} from "bar.svg" with {type: "svg"};`|`<script type="svg-module" specifier="/bar.svg"></script>`|
325
308
| WASM |`import {foo} from "bar.wasm" with {type: "wasm"};`|`<script type="wasm-module" specifier="/bar.wasm"></script>`|
326
309
327
310
## Alternate proposals
311
+
312
+
### [Local References For Link Rel](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/LocalReferenceLinkRel/explainer.md)
313
+
314
+
This proposal extends the existing `<link>` tag to support local `<style>` tag references as follows:
315
+
316
+
```html
317
+
<styleid="inline_styles">
318
+
p {
319
+
color: blue;
320
+
}
321
+
</style>
322
+
<p>Outside Shadow DOM</p>
323
+
<templateshadowrootmode="open">
324
+
<linkrel="stylesheet"href="#inline_styles" />
325
+
<p>Inside Shadow DOM</p>
326
+
</template>
327
+
```
328
+
329
+
This allows for sharing styles defined in the Light DOM across Shadow Roots. Due to scoping behaviors, it will not allow for styles defined in a Shadow DOM
330
+
to be accessed in any other Shadow Root. This limitation could be addressed with extensions on Shadow DOM scoping suggested in
### Key Differences Between This Proposal And Local References For Link Rel
334
+
335
+
Both this proposal and [Local References For Link Rel](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/LocalReferenceLinkRel/explainer.md)
336
+
allow authors to share inline CSS with Shadow Roots. There are some key differences in both syntax and
337
+
behaviors, as illustrated in the following table:
338
+
339
+
|| Local Reference Link Rel | Declarative CSS Modules |
340
+
| :---: | :---: | :---: |
341
+
| Scope | ⚠️ [Standard DOM scoping](https://github.com/MicrosoftEdge/MSEdgeExplainers/blob/main/LocalReferenceLinkRel/explainer.md#Scoping)| Global scope |
342
+
| Identifier syntax | Standard HTML IDREF | Module identifier |
343
+
| Attribute used | Standard HTML `href`| New attribute for `identifier`|
344
+
| Uses existing HTML concepts | ✅ Yes | ❌ No |
345
+
| Uses existing module concepts | ❌ No | ✅ Yes |
346
+
| Extensibility | Clean @sheet integration, scope expansion could apply to SVG references | More declarative module types (HTML, SVG, etc.) |
347
+
328
348
### [Layer and adoptStyles](https://github.com/w3c/csswg-drafts/issues/10176#proposal)
329
349
This proposal adds the `adoptStyles` attribute to the template element, enabling its shadow root to adopt styles from outside of the shadow DOM.
0 commit comments