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
+13-12Lines changed: 13 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
4
4
We can create custom HTML elements, described by our class, with its own methods and properties, events and so on.
5
5
6
-
Once an custom element is defined, we can use it on par with built-in HTML elements.
6
+
Once a custom element is defined, we can use it on par with built-in HTML elements.
7
7
8
8
That's great, as HTML dictionary is rich, but not infinite. There are no `<easy-tabs>`, `<sliding-carousel>`, `<beautiful-upload>`... Just think of any other tag we might need.
9
9
@@ -12,9 +12,9 @@ We can define them with a special class, and then use as if they were always a p
2.**Customized built-in elements** -- extending built-in elements, like customized `HTMLButtonElement` etc.
15
+
2.**Customized built-in elements** -- extending built-in elements, like a customized button, based on`HTMLButtonElement` etc.
16
16
17
-
First we'll create autonomous elements, and then customized built-in ones.
17
+
First we'll cover autonomous elements, and then move to customized built-in ones.
18
18
19
19
To create a custom element, we need to tell the browser several details about it: how to show it, what to do when the element is added or removed to page, etc.
20
20
@@ -30,12 +30,12 @@ class MyElement extends HTMLElement {
30
30
}
31
31
32
32
connectedCallback() {
33
-
// browser calls it when the element is added to the document
33
+
// browser calls this method when the element is added to the document
34
34
// (can be called many times if an element is repeatedly added/removed)
35
35
}
36
36
37
37
disconnectedCallback() {
38
-
// browser calls it when the element is removed from the document
38
+
// browser calls this method when the element is removed from the document
39
39
// (can be called many times if an element is repeatedly added/removed)
40
40
}
41
41
@@ -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.
0 commit comments