Skip to content

Commit 8de6fa6

Browse files
committed
draft
1 parent 9c3ac13 commit 8de6fa6

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

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

+25-22
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ A DOM element can have two types of DOM subtrees:
4040
1. Light tree -- a regular DOM subtree, made of HTML children.
4141
2. Shadow tree -- a hidden DOM subtree, not reflected in HTML, hidden from prying eyes.
4242

43-
Technically, it's possible for an element to have both at the same time. Then the browser renders only the shadow tree. But usually the element content is either "light" (included into the main DOM/HTML) or "shadowed" (encapsulated from it).
43+
Technically, it's possible for an element to have both at the same time. Then the browser renders only the shadow tree. We'll why that may be needed later in this chapter.
4444

4545
Shadow tree can be used in Custom Elements to hide internal implementation details.
4646

@@ -82,33 +82,25 @@ That's how it looks in Chrome dev tools:
8282
For example:
8383

8484
```html run untrusted height=40
85-
<div id="elem"></div>
86-
<script>
87-
88-
customElements.define('show-hello', class extends HTMLElement {
89-
connectedCallback() {
90-
this.attachShadow({mode: 'open'});
91-
*!*
92-
// inner style is applied to shadow tree (1)
93-
*/!*
94-
this.shadowRoot.innerHTML = `
95-
<style> p { font-weight: bold; } </style>
96-
<p>Hello, ${this.getAttribute('name')}!</p>
97-
`;
98-
}
99-
});
100-
</script>
101-
102-
<show-hello id="elem" name="John"></show-hello>
103-
10485
<style>
10586
*!*
10687
/* document style not applied to shadow tree (2) */
10788
*/!*
10889
p { color: red; }
10990
</style>
11091

92+
<div id="elem"></div>
93+
11194
<script>
95+
elem.attachShadow({mode: 'open'});
96+
*!*
97+
// inner style is applied to shadow tree (1)
98+
*/!*
99+
elem.shadowRoot.innerHTML = `
100+
<style> p { font-weight: bold; } </style>
101+
<p>Hello, John!</p>
102+
`;
103+
112104
*!*
113105
// <p> is only visible from inside the shadow tree (3)
114106
*/!*
@@ -117,8 +109,8 @@ customElements.define('show-hello', class extends HTMLElement {
117109
</script>
118110
```
119111

120-
1. The style from the shadow tree itself applies.
121-
2. ...But the style from the outside doesn't.
112+
1. The style from the document does not affect the shadow tree.
113+
2. ...But the style from the inside works.
122114
3. To get elements in shadow tree, we must query from inside the tree.
123115

124116
The element with a shadow root is called a "shadow tree host", and is available as the shadow root `host` property:
@@ -128,6 +120,17 @@ The element with a shadow root is called a "shadow tree host", and is available
128120
alert(elem.shadowRoot.host === elem); // true
129121
```
130122

123+
## Composition
124+
125+
We can create "slotted" components, e.g. articles, tabs, menus, that can be filled by
126+
127+
polyfill https://github.com/webcomponents/webcomponentsjs
128+
129+
130+
131+
132+
133+
131134

132135

133136
<hr>

0 commit comments

Comments
 (0)