Herramientas de usuario

Herramientas del sitio


wiki2:webcomponents

¡Esta es una revisión vieja del documento!


Web Components

Basic

Concepts

Web components are those functions that allow you to create new custom, reusable, encapsulated HTML tags to use in web pages and web apps. These are the concepts that involve their creation:

  • Custom elements: A set of JavaScript API for creating web components.
  • Shadow DOM: A DOM that is separated from the main one in the document.
  • HTML templates: Tags like <template> or <slot> that enable you to write markup templates.

Steps for creating a web component

  1. Create a class that extends from HtmlElement.
  2. Register it using CustomElementRegistry.define().
  3. If required, atach a shadow DOM Element.attachShadow().
  4. If required define templates with <template> and <slot>.
  5. Use the defined tag.

The most basic Web Component

(function() {
    class MyTitle extends HTMLElement {
      connectedCallback() {
        this.innerHTML = `
          <style>
            h1 {
              font-size: 2.5rem;
              color: hotpink;
              font-family: monospace;
              text-align: center;
              text-decoration: pink solid underline;
              text-decoration-skip: ink;
            }
          </style>
          <h1>Hello Alligator!</h1>
        `;
      }
    }
 
    window.customElements.define('my-title', MyTitle);
  })();

Now we can add the tag <my-title></my-title> to our code and a pink “Hello Alligator!” will appear.

Resources

Reference

Life-cycle and callbacks

  • connectedCallback: Invoked when the custom element is first added to the document's DOM.
  • disconnectedCallback: Invoked when the custom element is removed from the document's DOM.
  • adoptedCallback: Invoked when the custom element is moved to a new document.
  • attributeChangedCallback: Invoked when one of the custom element's attributes is added, removed, or changed.

Please, remind if you define a constructor you must call super() as the first line of the block.

Shadow DOM

Templates

Instead of defining the HTML and CSS in a JavaScript string, you can use a template tag in HTML and assign it an id:

<template id="custom-title-template">
  <style>
    h1 {
      font-size: 7rem;
      color: #000;
      font-family: Helvetica;
      text-align: center;
    }
  </style>
  <h1>My Custom Title!</h1>
</template>
 
<custom-title></custom-title>

Then you can reference it in your Custom Element constructor and add it to the Shadow DOM:

class CustomTitle extends HTMLElement {
  constructor() {
    super()
    this.attachShadow({ mode: 'open' })
    const tmpl = document.querySelector('#custom-title-template')
    this.shadowRoot.appendChild(tmpl.content.cloneNode(true))
  }
}
 
window.customElements.define('custom-title', CustomTitle)

Slots

CSS

CSS pseudo-classes

  • :defined: - Matches any element that is defined with CustomElementRegistry.define.
  • :host: - Selects the shadow host of the shadow DOM containing the CSS it is used inside.
  • :host(): - Selects the shadow host of the shadow DOM containing the CSS it is used inside (so you can select a custom element from inside its shadow DOM) — but only if the selector given as the function's parameter matches the shadow host.
  • :host-context(): - Selects the shadow host of the shadow DOM containing the CSS it is used inside (so you can select a custom element from inside its shadow DOM) — but only if the selector given as the function's parameter matches the shadow host's ancestor(s) in the place it sits inside the DOM hierarchy. === CSS pseudo-elements === * ::part:'' Represents any element within a shadow tree that has a matching part attribute.
wiki2/webcomponents.1591006774.txt.gz · Última modificación: 2020/06/01 11:19 (editor externo)