Element Upgrades
Web Components ⟩ Custom Elements ⟩
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()
customElements.whenDefined('custom-element').then(() => {
console.log('custom-element defined');
});<share-buttons>
<social-button type="twitter"><a href="...">Twitter</a></social-button>
<social-button type="fb"><a href="...">Facebook</a></social-button>
<social-button type="plus"><a href="...">G+</a></social-button>
</share-buttons>
<script>
// Fetch all the children of <share-buttons> that are not defined yet.
let undefinedButtons = buttons.querySelectorAll(':not(:defined)');
let promises = [...undefinedButtons].map(socialButton => {
return customElements.whenDefined(socialButton.localName);
});
// Wait for all the social-buttons to be upgraded.
Promise.all(promises).then(() => {
// All social-button children are ready.
});
</script>Last updated
Was this helpful?