Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DSD style sharing explainer #875

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 112 additions & 1 deletion ShadowDOM/explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,118 @@ This document explores several proposals that would allow developers to apply st
## Non-goals
Some developers have expressed interest in CSS selectors crossing through the Shadow DOM, as discussed in [issue 909](https://github.com/WICG/webcomponents/issues/909#issuecomment-1977487651). While this scenario is related to sharing styles with Shadow DOM elements, it is solving a different problem and should be addressed separately.

## Use case
## Use case
### Pages with sub-sections

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should update the example to something similar to what is seen in the wild. Potentially a fake page with a table of contents section on the right or left?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an additional sentence like this will suffice, what do you think? "This pattern of pages with sub-sections are often seen in blogs, search result pages, pages that has a separate table of content, etc".

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern was less about the text but more about the example used. I'm thinking we can showcase something that is more realistic to patterns we see in the wild. I think a table of contents of some kind would be more identifiable to someone reading the explainer

In some web pages with sub-sections that feature unique CSS styles distinct from the rest of the page, a combination of inherited global styles and highly specific CSS is used, which applies only to those sub-sections. The regular HTML structure often forces the browser to process CSS before rendering the first visual elements. When there is a significant number of styles, it can negatively impact page performance. Due to the limitations in style sharing with Declarative Shadow DOM, web authors cannot selectively share styles between the parent document and shadow roots. Here is an example of a web page with global, shared and unique styles:

![image](images/webpagewithsubsection.png)

In this example, CSS variables such as `--font-family` and `--text-color` were added to the global styles, which are used in the main document and within the shadow DOM.

```html
<head>
<style>
/* Global styles that are inherited across the entire document */
:root {
--font-family: 'Arial', sans-serif;
--background-color: #f4f4f9;
--text-color: #333;
--section-bg-color: #ffffff;
--section-border-color: #ccc;
--heading-color: #333;
--paragraph-color: #555;
}

body {
font-family: var(--font-family);
background-color: var(--background-color);
margin: 0;
padding: 20px;
line-height: 1.6;
color: var(--text-color);
}

h1 {
color: var(--heading-color);
font-size: 2em;
}

p {
color: var(--paragraph-color);
}

.section {
background-color: var(--section-bg-color);
border: 1px solid var(--section-border-color);
margin-bottom: 20px;
padding: 15px;
}

</style>
</head>

<body>

<h1>Web Page with Global, Shared, and Unique Styles</h1>

<!-- A regular section that inherits global styles -->
<div class="section">
<h2>Section 1</h2>
<p>This section uses the global styles defined in the document.</p>
</div>
</body>

```

Meanwhile, the shadow DOM section uses shared styles defined in the parent document, but also has its unique styles
```js
// Creating a shadow DOM host
const shadowHost = document.createElement('div');
document.body.appendChild(shadowHost);

// Attaching shadow root
const shadowRoot = shadowHost.attachShadow({ mode: 'open' });

// Defining shadow DOM content with shared and unique styles
shadowRoot.innerHTML = `
```

```html
<style>
/* Shared styles: inheriting global styles using CSS variables */
.shared-sub-section {
background-color: var(--section-bg-color);
border: 1px solid var(--section-border-color);
padding: 20px;
margin: 20px 0;
font-family: var(--font-family);
color: var(--text-color);
}

/* Specific styles for the shadow DOM sub-section */
.shared-sub-section {
background-color: #f0f8ff;
border: 2px solid #4682b4;
}

.shared-sub-section h2 {
color: #4682b4;
}

.shared-sub-section p {
color: #696969;
}
</style>

<!-- Shadow DOM content -->
<div class="shared-sub-section">
<h2>Shadow DOM with Shared and Unique Styles</h2>
<p>This sub-section shares global styles using CSS variables but also applies its own unique styles inside the shadow DOM.</p>
</div>
`;

```

### Media site control widgets
Sharing styles between the parent document and shadow root is also fairly common for media site
control widgets such as play/pause buttons, volume sliders, and progress bars, to share styles
Expand Down
Binary file removed ShadowDOM/images/bing serp.png
Binary file not shown.
Binary file added ShadowDOM/images/webpagewithsubsection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.