Skip to content

Commit 9a55431

Browse files
[Shadow DOM Styling] Update the Constructible Stylesheets example to be more realistic. (#1093)
Constructible Stylesheets are most often shared among all instances of a element class, so they're set up outside of the constructor. This updates the example to do that.
1 parent 7c90490 commit 9a55431

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

ShadowDOM/explainer.md

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ content location of future work and discussions.
2525
* Expected venue: [Web Components CG](https://w3c.github.io/webcomponents-cg/)
2626
* Current version: this document
2727
## Table of Contents
28+
- [Declarative shadow DOM style sharing](#declarative-shadow-dom-style-sharing)
2829
- [Authors](#authors)
2930
- [Participate](#participate)
3031
- [Status of this Document](#status-of-this-document)
32+
- [Table of Contents](#table-of-contents)
3133
- [Background](#background)
3234
- [Problem](#problem)
3335
- [Goals](#goals)
@@ -36,8 +38,21 @@ content location of future work and discussions.
3638
- [Media site control widgets](#media-site-control-widgets)
3739
- [Anywhere web components are used](#anywhere-web-components-are-used)
3840
- [Alternatives to using style in DSD](#alternatives-to-using-style-in-dsd)
39-
- [Proposal: Inline, declarative SS module scripts](#proposal-inline-declarative-css-module-scripts)
41+
- [Constructable Stylesheets](#constructable-stylesheets)
42+
- [Using `rel="stylesheet"` attribute](#using-relstylesheet-attribute)
43+
- [CSS `@import` rules](#css-import-rules)
44+
- [Proposal: Inline, declarative CSS module scripts](#proposal-inline-declarative-css-module-scripts)
45+
- [Scoping](#scoping)
46+
- [`<script>` vs `<style>` For CSS Modules](#script-vs-style-for-css-modules)
47+
- [Behavior with script disabled](#behavior-with-script-disabled)
48+
- [Other declarative modules](#other-declarative-modules)
4049
- [Alternate proposals](#alternate-proposals)
50+
- [Local References For Link Rel](#local-references-for-link-rel)
51+
- [Key Differences Between This Proposal And Local References For Link Rel](#key-differences-between-this-proposal-and-local-references-for-link-rel)
52+
- [Layer and adoptStyles](#layer-and-adoptstyles)
53+
- [`@Sheet`](#sheet)
54+
- [Id-based `adoptedstylesheet` attribute on template](#id-based-adoptedstylesheet-attribute-on-template)
55+
- [Polyfills](#polyfills)
4156
- [Summary](#summary)
4257
- [Open issues](#open-issues)
4358
- [References and acknowledgements](#references-and-acknowledgements)
@@ -123,37 +138,37 @@ Some developers have expressed interest in CSS selectors crossing through the Sh
123138
```
124139
Meanwhile, the styles defined within the Shadow DOM are specific to the media control widget. These styles ensure that the widget looks consistent and isn't affected by other styles on the page.
125140
```js
141+
// Shared stylesheet for all <media-control> elements
142+
const sheet = new CSSStyleSheet();
143+
sheet.replaceSync(`
144+
.media-control-container {
145+
display: flex;
146+
flex-direction: column;
147+
align-items: center;
148+
border: 1px solid #ccc;
149+
padding: 16px;
150+
background-color: #fff;
151+
}
152+
.controls {
153+
margin-top: 8px;
154+
display: flex;
155+
gap: 8px;
156+
align-items: center;
157+
}
158+
button, input[type="range"] {
159+
cursor: pointer;
160+
margin: 5px;
161+
}
162+
`);
163+
126164
class MediaControl extends HTMLElement {
127165
constructor() {
128166
super();
129167

130168
// Attach a shadow root to the element.
131169
const shadow = this.attachShadow({ mode: 'open' });
132170

133-
// Create elements
134-
135-
// Style the elements within the shadow DOM
136-
const sheet = new CSSStyleSheet();
137-
sheet.replaceSync(`
138-
.media-control-container {
139-
display: flex;
140-
flex-direction: column;
141-
align-items: center;
142-
border: 1px solid #ccc;
143-
padding: 16px;
144-
background-color: #fff;
145-
}
146-
.controls {
147-
margin-top: 8px;
148-
display: flex;
149-
gap: 8px;
150-
align-items: center;
151-
}
152-
button, input[type="range"] {
153-
cursor: pointer;
154-
margin: 5px;
155-
}
156-
`);
171+
// Adopt the shared styles
157172
shadow.adoptedStyleSheets.push(sheet);
158173

159174
// Initialize content from template here

0 commit comments

Comments
 (0)