Table of Contents

HTML - Custom Tag / Element

About

custom is the possibility create custom element

By adding them in a shadow DOM (to scope the DOM selection) and using templates element, you are creating web components

Example: Words Count

HTML Element

count word analytics element.

<textarea cols=40 rows=4>Word Count example</textarea>
<word-count/>
// Create a class for the element
class WordCount extends HTMLElement {
  constructor() {
    // Always call super first in constructor
    super();

    // count words in element's parent element
    const wcParent = this.previousElementSibling;

    function countWords(node){
      const text = node.value || node.innerText || node.textContent;
      return text.split(/\s+/g).length;
    }

    const count = `Words: ${countWords(wcParent)}`;

    // Create a shadow root
    const shadow = this.attachShadow({mode: 'open'});

    // Create text node and add word count to it
    const text = document.createElement('p');
    text.textContent = count;

    // Append it to the shadow root
    shadow.appendChild(text);

    // Update count at interval
    setInterval(function() {

      const count = `Words: ${countWords(wcParent)}`;
      text.textContent = count;
    }, 5000);
  }
}

// Define the new element
customElements.define('word-count', WordCount);

Extends HTML Element

This example shows how to extends an actual HTML element. It 's the same previous count word example but that extends the p element

The difference with the previous example are:

<p is="word-count"></p>
// Create a class for the element
class WordCount extends HTMLParagraphElement {
  // ...
customElements.define('word-count', WordCount, { extends: 'p' });

Ref: Taken from custom element

Documentation / Reference