Skip to content

Commit f164389

Browse files
authored
Update article.md
1 parent a3b59c6 commit f164389

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

8-web-components/2-custom-elements/article.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
We can create custom HTML elements, described by our class, with its own methods and properties, events and so on.
55

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.
77

88
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.
99

@@ -12,9 +12,9 @@ We can define them with a special class, and then use as if they were always a p
1212
There are two kinds of custom elements:
1313

1414
1. **Autonomous custom elements** -- "all-new" elements, extending the abstract `HTMLElement` class.
15-
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.
1616

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.
1818

1919
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.
2020

@@ -30,12 +30,12 @@ class MyElement extends HTMLElement {
3030
}
3131

3232
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
3434
// (can be called many times if an element is repeatedly added/removed)
3535
}
3636

3737
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
3939
// (can be called many times if an element is repeatedly added/removed)
4040
}
4141

@@ -138,7 +138,7 @@ In the example above, element content is rendered (created) in `connectedCallbac
138138

139139
Why not in the `constructor`?
140140

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.
142142

143143
Besides, if you think about it, that's better performance-wise -- to delay the work until it's really needed.
144144

@@ -242,7 +242,7 @@ customElements.define('user-info', class extends HTMLElement {
242242

243243
If you run it, the `alert` is empty.
244244

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.
246246

247247
If we'd like to pass information to custom element, we can use attributes. They are available immediately.
248248

@@ -297,20 +297,20 @@ Output order:
297297

298298
1. outer connected.
299299
2. inner connected.
300-
2. outer initialized.
300+
3. outer initialized.
301301
4. inner initialized.
302302

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)`.
304304

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.
306306

307307
## Customized built-in elements
308308

309309
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.
310310

311311
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?
312312

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.
314314

315315
For example, buttons are instances of `HTMLButtonElement`, let's build upon it.
316316

@@ -324,7 +324,8 @@ For example, buttons are instances of `HTMLButtonElement`, let's build upon it.
324324
```js
325325
customElements.define('hello-button', HelloButton, *!*{extends: 'button'}*/!*);
326326
```
327-
There exist different tags that share the same class, that's why it's needed.
327+
328+
There may be different tags that share the same DOM-class, that's why specifying `extends` is needed.
328329
329330
3. At the end, to use our custom element, insert a regular `<button>` tag, but add `is="hello-button"` to it:
330331
```html

0 commit comments

Comments
 (0)