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: Learn/31. Web Components.md
+267-2Lines changed: 267 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -109,7 +109,7 @@ There is specific web component lifecycle which the browser follows, when instan
109
109
110
110
- For this, there is another method we can add in our class which will be executed by the browser once this element (custom component) has been mounted onto the browser's DOM, and that is the `connectedCallback()` method. This is called when our element has been attached to the DOM; and therefore this is a place for DOM initializations. So where we can now start adding content or where we can start accessing the DOM.
111
111
112
-
- There also is a `disconnectedCallback()` method which will also be executed automatically for us and this method will be called by the browser whenever our element (i.e. our custom web component) is detached from the DOM. This is a great method for some cleanup work; for example, if you were sending a HttpRequest, this would be where you could cancle it.
112
+
- There also is a `disconnectedCallback()` method which will also be executed automatically for us and this method will be called by the browser whenever our element (i.e. our custom web component) is detached from the DOM. This is a great method for some cleanup work; for example, if you were sending a HttpRequest, this would be where you could cancel it.
113
113
114
114
- There is another method, the `attributeChangedCallback()` and that will be important for listening to changes to attributes to our own element. This is important for updating the data and the DOM of our web component when some attributes which can passed to our component changed.
115
115
@@ -367,7 +367,7 @@ This web component now requires some content to be added to our HTML file where
367
367
368
368
We can bring back our removed text by `<slot></slot>` tag and nothing in between:
369
369
370
-
```js
370
+
```html
371
371
<!DOCTYPE html>
372
372
<htmllang="en">
373
373
@@ -504,8 +504,12 @@ class Tooltip extends HTMLElement {
504
504
customElements.define("ps-tooltip", Tooltip);
505
505
```
506
506
507
+
**Note**: We can see the all nodes that accessed our slots with `slots.assignedNodes()` method. Consider that we make a array with `const slots = this.shadowRoot.querySelectorAll("slot")` code). You can also use `console.dir(slots[1].assignedNodes())` code (`1` is just a element index).
508
+
507
509
We used `this.shadowRoot.innerHTML` instead of appending the template content.
508
510
511
+
**Note**: It means that we can remove the `this.shadowRoot.appendChild(tooltipIcon);` line. Because we appended in above (i.e. `<span> (?)</span>`).
512
+
509
513
You might wonder: How can we set the HTML content here, inside our constructor? The reason is simple, `innerHTML` is just a property of our element here (of our object) and this is just setting up some HTML code that will be rendered to the DOM, once this element is mounted to the DOM. So unlike `appendChild()`, this does not try to access the DOM at this point; it just prepares some content for the DOM once it later is available and the browser will take care about rendering this when it's able to.
- The **`bubbles`** read-only property of the `Event` interface indicates whether the event bubbles up through the DOM or not.
1347
+
1348
+
A `Boolean`, which is `true` if the event bubbles up through the DOM.
1349
+
1350
+
- The read-only **`composed`** property of the `Event` interface returns a `Boolean` which indicates whether or not the event will propagate across the shadow DOM boundary into the standard DOM.
1351
+
1352
+
A `Boolean` which is `true` if the event will cross from the shadow DOM into the standard DOM after reaching the shadow root. (That is, the first node in the shadow DOM in which the event began to propagate).
1353
+
1354
+
This is a first way of creating custom elementm there is another and easier way to creating our own events:
1355
+
1356
+
```js
1357
+
classEveeentextendsHTMLElement {
1358
+
constructor() {
1359
+
super();
1360
+
}
1361
+
1362
+
cancel() {
1363
+
constcancelEvent=newEvent("cancel");
1364
+
this.dispatchEvent(cancelEvent);
1365
+
1366
+
constexample=document.queryselector("button");
1367
+
example.adEventListener("cancel", func);
1368
+
}
1369
+
}
1370
+
```
1371
+
1372
+
It's very easy to understand and useful. We directly use the `HTMLElement` global object for using the `dispatchEvent()` method and we don't need to pass the `bubbles` and `composed` into an object.
1373
+
1374
+
## CSS Animations
1375
+
1376
+
You can learn alot about CSS animations with this following link: <https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions>
1377
+
1378
+
## Useful Resources & Links
1379
+
1380
+
MDN Docs on Web Components: <https://developer.mozilla.org/en-US/docs/Web/Web_Components>
1381
+
1382
+
More about Templates & Slots: <https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_templates_and_slots>
1383
+
1384
+
Google Article on Custom Elements: <https://developers.google.com/web/fundamentals/web-components/customelements>
1385
+
1386
+
Google Article on Shadow DOM: <https://developers.google.com/web/fundamentals/web-components/shadowdom>
0 commit comments