Skip to content

Commit 8319c71

Browse files
committed
minor
1 parent 4a8d898 commit 8319c71

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

Diff for: 8-web-components/6-shadow-dom-style/article.md

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Shadow DOM styling
22

3-
Shadow DOM may include both `<style>` and `<link rel="stylesheet" href="…">` tags. In the latter case, stylesheets are HTTP-cached, so they are not redownloaded. There's no overhead in @importing or linking same styles for many components.
3+
Shadow DOM may include both `<style>` and `<link rel="stylesheet" href="…">` tags. In the latter case, stylesheets are HTTP-cached, so they are not redownloaded for multiple components that use same template.
44

55
As a general rule, local styles work only inside the shadow tree, and document styles work outside of it. But there are few exceptions.
66

@@ -44,7 +44,7 @@ customElements.define('custom-dialog', class extends HTMLElement {
4444

4545
## Cascading
4646

47-
The shadow host (`<custom-dialog>` itself) resides in the light DOM, so it's affected by the main CSS cascade.
47+
The shadow host (`<custom-dialog>` itself) resides in the light DOM, so it's affected by document CSS rules.
4848

4949
If there's a property styled both in `:host` locally, and in the document, then the document style takes precedence.
5050

@@ -58,7 +58,7 @@ custom-dialog {
5858
```
5959
...Then the `<custom-dialog>` would be without padding.
6060

61-
It's very convenient, as we can setup "default" styles in the component `:host` rule, and then easily override them in the document.
61+
It's very convenient, as we can setup "default" component styles in its `:host` rule, and then easily override them in the document.
6262

6363
The exception is when a local property is labelled `!important`, for such properties, local styles take precedence.
6464

@@ -79,6 +79,7 @@ For example, we'd like to center the `<custom-dialog>` only if it has `centered`
7979
left: 50%;
8080
top: 50%;
8181
transform: translate(-50%, -50%);
82+
border-color: blue;
8283
}
8384
8485
:host {
@@ -108,13 +109,13 @@ customElements.define('custom-dialog', class extends HTMLElement {
108109
</custom-dialog>
109110
```
110111

111-
Now the additional centering styles are only applied to the first dialog `<custom-dialog centered>`.
112+
Now the additional centering styles are only applied to the first dialog: `<custom-dialog centered>`.
112113

113114
## :host-context(selector)
114115

115116
Same as `:host`, but applied only if the shadow host or any of its ancestors in the outer document matches the `selector`.
116117

117-
E.g. `:host-context(.dark-theme)` matches only if there's `dark-theme` class on `<custom-dialog>` on above it:
118+
E.g. `:host-context(.dark-theme)` matches only if there's `dark-theme` class on `<custom-dialog>` on anywhere above it:
118119

119120
```html
120121
<body class="dark-theme">
@@ -190,11 +191,11 @@ customElements.define('user-card', class extends HTMLElement {
190191
</script>
191192
```
192193
193-
Here `<p>John Smith</p>` becomes bold, because CSS inheritance is in effect between the `<slot>` and its contents. But not all CSS properties are inherited.
194+
Here `<p>John Smith</p>` becomes bold, because CSS inheritance is in effect between the `<slot>` and its contents. But in CSS itself not all properties are inherited.
194195
195196
Another option is to use `::slotted(selector)` pseudo-class. It matches elements based on two conditions:
196197
197-
1. The element from the light DOM that is inserted into a `<slot>`. Then slot name doesn't matter. Just any slotted element, but only the element itself, not its children.
198+
1. That's a slotted element, that comes from the light DOM. Slot name doesn't matter. Just any slotted element, but only the element itself, not its children.
198199
2. The element matches the `selector`.
199200
200201
In our example, `::slotted(div)` selects exactly `<div slot="username">`, but not its children:
@@ -239,21 +240,21 @@ Also, `::slotted` can only be used in CSS. We can't use it in `querySelector`.
239240
240241
## CSS hooks with custom properties
241242
242-
How do we style a component in-depth from the main document?
243+
How do we style internal elements of a component from the main document?
243244
244-
Naturally, document styles apply to `<custom-dialog>` element or `<user-card>`, etc. But how can we affect its internals? For instance, in `<user-card>` we'd like to allow the outer document change how user fields look.
245+
Selectors like `:host` apply rules to `<custom-dialog>` element or `<user-card>`, but how to style shadow DOM elements inside them?
245246
246-
Just as we expose methods to interact with our component, we can expose CSS variables (custom CSS properties) to style it.
247+
There's no selector that can directly affect shadow DOM styles from the document. But just as we expose methods to interact with our component, we can expose CSS variables (custom CSS properties) to style it.
247248
248249
**Custom CSS properties exist on all levels, both in light and shadow.**
249250
250-
For example, in shadow DOM we can use `--user-card-field-color` CSS variable to style fields:
251+
For example, in shadow DOM we can use `--user-card-field-color` CSS variable to style fields, and the outer document can set its value:
251252
252253
```html
253254
<style>
254255
.field {
255256
color: var(--user-card-field-color, black);
256-
/* if --user-card-field-color is not defined, use black */
257+
/* if --user-card-field-color is not defined, use black color */
257258
}
258259
</style>
259260
<div class="field">Name: <slot name="username"></slot></div>
@@ -321,8 +322,8 @@ Local styles can affect:
321322
- slotted elements (coming from light DOM), `::slotted(selector)` allows to select slotted elements themselves, but not their children.
322323
323324
Document styles can affect:
324-
- shadow host (as it's in the outer document)
325-
- slotted elements and their contents (as it's physically in the outer document)
325+
- shadow host (as it lives in the outer document)
326+
- slotted elements and their contents (as that's also in the outer document)
326327
327328
When CSS properties conflict, normally document styles have precedence, unless the property is labelled as `!important`. Then local styles have precedence.
328329

0 commit comments

Comments
 (0)