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
Copy file name to clipboardExpand all lines: 8-web-components/2-custom-elements/article.md
+8-7
Original file line number
Diff line number
Diff line change
@@ -138,7 +138,7 @@ In the example above, element content is rendered (created) in `connectedCallbac
138
138
139
139
Why not in the `constructor`?
140
140
141
-
The reason is simple: when `constructor` is called, it's yet too early. The element instance is created, but not populated yet. The browser did not yet process/assign attributes at this stage: calls to `getAttribute` would return `null`. So we can't really render there.
141
+
The reason is simple: when `constructor` is called, it's yet too early. The element is created, but the browser did not yet process/assign attributes at this stage: calls to `getAttribute` would return `null`. So we can't really render there.
142
142
143
143
Besides, if you think about it, that's better performance-wise -- to delay the work until it's really needed.
144
144
@@ -242,7 +242,7 @@ customElements.define('user-info', class extends HTMLElement {
242
242
243
243
If you run it, the `alert` is empty.
244
244
245
-
That's exactly because there are no children on that stage, the DOM is unfinished. HTML parser connected the custom element `<user-info>`, and will now proceed to its children, but just didn't yet.
245
+
That's exactly because there are no children on that stage, the DOM is unfinished. HTML parser connected the custom element `<user-info>`, and is going to proceed to its children, but just didn't yet.
246
246
247
247
If we'd like to pass information to custom element, we can use attributes. They are available immediately.
248
248
@@ -297,20 +297,20 @@ Output order:
297
297
298
298
1. outer connected.
299
299
2. inner connected.
300
-
2. outer initialized.
300
+
3. outer initialized.
301
301
4. inner initialized.
302
302
303
-
We can clearly see that the outer element does not wait for the inner one.
303
+
We can clearly see that the outer element finishes initialization `(3)` before the inner one`(4)`.
304
304
305
-
There's no built-in callback that triggers after nested elements are ready. But we can implement such thing on our own. For instance, inner elements can dispatch events like `initialized`, and outer ones can listen and react on them.
305
+
There's no built-in callback that triggers after nested elements are ready. If needed, we can implement such thing on our own. For instance, inner elements can dispatch events like `initialized`, and outer ones can listen and react on them.
306
306
307
307
## Customized built-in elements
308
308
309
309
New elements that we create, such as `<time-formatted>`, don't have any associated semantics. They are unknown to search engines, and accessibility devices can't handle them.
310
310
311
311
But such things can be important. E.g, a search engine would be interested to know that we actually show a time. And if we're making a special kind of button, why not reuse the existing `<button>` functionality?
312
312
313
-
We can extend and customize built-in elements by inheriting from their classes.
313
+
We can extend and customize built-in HTML elements by inheriting from their classes.
314
314
315
315
For example, buttons are instances of `HTMLButtonElement`, let's build upon it.
316
316
@@ -324,7 +324,8 @@ For example, buttons are instances of `HTMLButtonElement`, let's build upon it.
Copy file name to clipboardExpand all lines: 8-web-components/5-slots-composition/article.md
+41-16
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,7 @@ customElements.define('user-card', class extends HTMLElement {
58
58
59
59
In the shadow DOM, `<slot name="X">` defines an "insertion point", a place where elements with `slot="X"` are rendered.
60
60
61
-
Then the browser performs "composition": it takes elements from the light DOM and renders them in corresponding slots of the shadow DOM. At the end, we have exactly what we want -- a generic component that can be filled with data.
61
+
Then the browser performs "composition": it takes elements from the light DOM and renders them in corresponding slots of the shadow DOM. At the end, we have exactly what we want -- a component that can be filled with data.
62
62
63
63
Here's the DOM structure after the script, not taking composition into account:
64
64
@@ -76,7 +76,7 @@ Here's the DOM structure after the script, not taking composition into account:
76
76
</user-card>
77
77
```
78
78
79
-
There's nothing odd here. We created the shadow DOM, so here it is. Now the element has both light and shadow DOM.
79
+
We created the shadow DOM, so here it is, under `#shadow-root`. Now the element has both light and shadow DOM.
80
80
81
81
For rendering purposes, for each `<slot name="...">` in shadow DOM, the browser looks for `slot="..."` with the same name in the light DOM. These elements are rendered inside the slots:
82
82
@@ -89,7 +89,7 @@ The result is called "flattened" DOM:
89
89
#shadow-root
90
90
<div>Name:
91
91
<slotname="username">
92
-
<!-- slotted element is inserted into the slot as a whole -->
92
+
<!-- slotted element is inserted into the slot -->
93
93
<spanslot="username">John Smith</span>
94
94
</slot>
95
95
</div>
@@ -101,7 +101,7 @@ The result is called "flattened" DOM:
101
101
</user-card>
102
102
```
103
103
104
-
...But the "flattened" DOM is only created for rendering and event-handling purposes. That's how things are shown. The nodes are actually not moved around!
104
+
...But the flattened DOM exists only for rendering and event-handling purposes. It's kind of "virtual". That's how things are shown. But the nodes in the document are actually not moved around!
105
105
106
106
That can be easily checked if we run `querySelector`: nodes are still at their places.
107
107
@@ -110,7 +110,7 @@ That can be easily checked if we run `querySelector`: nodes are still at their p
It may look bizarre, but for shadow DOM with slots we have one more "DOM level", the "flattened" DOM -- result of slot insertion. The browser renders it and uses for style inheritance, event propagation. But JavaScript still sees the document "as is", before flattening.
113
+
So, the flattened DOM is derived from shadow DOM by inserting slots. The browser renders it and uses for style inheritance, event propagation (more about that later). But JavaScript still sees the document "as is", before flattening.
114
114
115
115
````warn header="Only top-level children may have slot=\"...\" attribute"
116
116
The `slot="..."` attribute is only valid for direct children of the shadow host (in our example, `<user-card>` element). For nested elements it's ignored.
@@ -120,18 +120,43 @@ For example, the second `<span>` here is ignored (as it's not a top-level child
120
120
<user-card>
121
121
<span slot="username">John Smith</span>
122
122
<div>
123
-
<!-- bad slot, not top-level: -->
123
+
<!-- invalid slot, must be direct child of user-card -->
124
124
<span slot="birthday">01.01.2001</span>
125
125
</div>
126
126
</user-card>
127
127
```
128
-
129
-
In practice, there's no sense in slotting a deeply nested element, so this limitation just ensures the correct DOM structure.
130
128
````
131
129
130
+
If there are multiple elements in light DOM with the same slot name, they are appended into the slot, one after another.
131
+
132
+
For example, this:
133
+
```html
134
+
<user-card>
135
+
<spanslot="username">John</span>
136
+
<spanslot="username">Smith</span>
137
+
</user-card>
138
+
```
139
+
140
+
Gives this flattened DOM with two elements in `<slot name="username">`:
141
+
142
+
```html
143
+
<user-card>
144
+
#shadow-root
145
+
<div>Name:
146
+
<slotname="username">
147
+
<spanslot="username">John</span>
148
+
<spanslot="username">Smith</span>
149
+
</slot>
150
+
</div>
151
+
<div>Birthday:
152
+
<slotname="birthday"></slot>
153
+
</div>
154
+
</user-card>
155
+
```
156
+
132
157
## Slot fallback content
133
158
134
-
If we put something inside a `<slot>`, it becomes the fallback content. The browser shows it if there's no corresponding filler in light DOM.
159
+
If we put something inside a `<slot>`, it becomes the fallback, "default" content. The browser shows it if there's no corresponding filler in light DOM.
135
160
136
161
For example, in this piece of shadow DOM, `Anonymous` renders if there's no `slot="username"` in light DOM.
137
162
@@ -141,11 +166,11 @@ For example, in this piece of shadow DOM, `Anonymous` renders if there's no `slo
141
166
</div>
142
167
```
143
168
144
-
## Default slot
169
+
## Default slot: first unnamed
145
170
146
171
The first `<slot>` in shadow DOM that doesn't have a name is a "default" slot. It gets all nodes from the light DOM that aren't slotted elsewhere.
147
172
148
-
For example, let's add the default slot to our `<user-card>` that collects any unslotted information about the user:
173
+
For example, let's add the default slot to our `<user-card>` that shows all unslotted information about the user:
149
174
150
175
```html run autorun="no-epub" untrusted height=140
151
176
<script>
@@ -243,7 +268,7 @@ The shadow DOM template with proper slots:
243
268
```
244
269
245
270
1.`<span slot="title">` goes into `<slot name="title">`.
246
-
2. There are many `<li slot="item">` in the template, but only one `<slot name="item">` in the template. That's perfectly normal. All elements with `slot="item"` get appended to `<slot name="item">` one after another, thus forming the list.
271
+
2. There are many `<li slot="item">` in the template, but only one `<slot name="item">` in the template. So all such `<li slot="item">` are appended to `<slot name="item">` one after another, thus forming the list.
247
272
248
273
The flattened DOM becomes:
249
274
@@ -293,15 +318,15 @@ Here's the full demo:
293
318
294
319
Of course, we can add more functionality to it: events, methods and so on.
295
320
296
-
## Monitoring slots
321
+
## Updating slots
297
322
298
323
What if the outer code wants to add/remove menu items dynamically?
299
324
300
325
**The browser monitors slots and updates the rendering if slotted elements are added/removed.**
301
326
302
327
Also, as light DOM nodes are not copied, but just rendered in slots, the changes inside them immediately become visible.
303
328
304
-
So we don't have to do anything to update rendering. But if the component wants to know about slot changes, then `slotchange` event is available.
329
+
So we don't have to do anything to update rendering. But if the component code wants to know about slot changes, then `slotchange` event is available.
305
330
306
331
For example, here the menu item is inserted dynamically after 1 second, and the title changes after 2 seconds:
307
332
@@ -406,7 +431,7 @@ setTimeout(() => {
406
431
407
432
## Summary
408
433
409
-
Slots allow to show light DOM children in shadow DOM.
434
+
Usually, if an element has shadow DOM, then its light DOM is not displayed. Slots allow to show elements from light DOM in specified places of shadow DOM.
410
435
411
436
There are two kinds of slots:
412
437
@@ -427,6 +452,6 @@ If we'd like to know what we're showing, we can track slot contents using:
427
452
-`slotchange` event -- triggers the first time a slot is filled, and on any add/remove/replace operation of the slotted element, but not its children. The slot is `event.target`.
428
453
-[MutationObserver](info:mutation-observer) to go deeper into slot content, watch changes inside it.
429
454
430
-
Now, as we have elements from light DOM in the shadow DOM, let's see how to style them properly. The basic rule is that shadow elements are styled inside, and light elements -- outside, but there are notable exceptions.
455
+
Now, as we know how to show elements from light DOM in shadow DOM, let's see how to style them properly. The basic rule is that shadow elements are styled inside, and light elements -- outside, but there are notable exceptions.
0 commit comments