Element Upgrades

Web Components โŸฉ Custom 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