๐Ÿ”ฐweb component

isolated pieces (kinds of blocks) with which a user interface (UI) can communicate with other elements, through properties and events.

browser โŸฉ web component

web components are implemented as a custom element that uses a <template> tag for efficiency and a shadow root for encapsulation.

  • define their own HTML tag names, with the important restriction that those tag names must include a hyphen.

  • cannot be defined with self-closing tags.

some web components are written to:

  • expect children.

  • not expect children (and won't display them)

  • optionally accept specially labelled children that will appear in named "slots".

popular frameworks๏ผšAngular, React, Vue.js

cheatsheet

๐Ÿ“ html

<!-- โญ๏ธ 1. custom element -->
<my-component></my-component>

<!-- โญ๏ธ 2. customized built-in element -->
<!-- โญ๏ธ   `is="my-componet"` attribute -->
<button is="fancy-button">some text</button>

๐Ÿ“ js

// โญ๏ธ 2. `new MyComponent()`
let button = new FancyButton();

// โญ๏ธ 3. `document.createElement('my-component')` 
//                                            โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โญ๏ธโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
let button = document.createElement('button', {is: 'fancy-button'});

button.textContent = 'Fancy button!';
button.disabled = true;
document.body.appendChild(button);

Last updated