Element Upgrades

Web ComponentsCustom Elements

  • Custom elements can be used before their definition is registered.

  • The process of calling customElements.define() and endowing an existing element with a class definition is called "element upgrades". 📘 Google ⟩ Element Upgrades

If browser encounters <custom-element> before customElements.define(), the element is yet unknown, just like any non-standard tag. Undefined elements can be styled with CSS selector:not(:defined).

When customElement.define() is called, they are “upgraded”: a new instance of CustomElement is created for each, and connectedCallback is called. They become :defined. 👉 pseudo-class

.whenDefined()

To know when a tag name becomes defined, you can use customElements.whenDefined(). It returns a Promise that resolves when the element becomes defined.

customElements.whenDefined('custom-element').then(() => {
  console.log('custom-element defined');
});

Last updated