Consider the following scenario where I have a list of custom elements which is dynamically created. The CustomElement has a custom property displayName that should do something when set:
class CustomElement extends HTMLElement {
set displayName(v) {
this.querySelector(".name").textContent = v;
}
}
customElements.define("custom-element", CustomElement)
const template = document.querySelector("template");
const list = document.querySelector(".list");
document.querySelector("button").addEventListener("click", () => {
const customElTemplate = template.content.firstElementChild;
console.log("Template constructor: ", customElTemplate.constructor.name);
const el = template.content.firstElementChild.cloneNode(true);
console.log("Cloned el constructor:", el.constructor.name);
el.displayName = "Should has content";
const frag = new DocumentFragment();
frag.appendChild(el);
console.log("Cloned el in DocumentFragment constructor:", el.constructor.name);
list.appendChild(frag);
console.log("Cloned el in Document constructor:", el.constructor.name);
});
custom-element {
display: block;
margin-bottom: .5rem;
border: 1px solid black;
}
<template>
<custom-element><span class="name"></span></custom-element>
</template>
<button>Add Item</button>
<div class="list"></div>
When clicking the button, the console output are:
Template constructor: HTMLElement
Cloned el constructor: HTMLElement
Cloned el in DocumentFragment constructor: HTMLElement
Cloned el in Document constructor: CustomElement
As you can see, the cloned element doesn't become CustomElement until it's attached to the root document and displayName is simply a plain property that does nothing.
el.displayName = "Should has content"; won't work even after attaching to a DocumentFragment. It only works after attaching to the document (i.e. after list.appendChild(frag); in the above example)
Why is this happening? Is there anyway I can set up the element before attaching it to the main document?
UPDATE: I see there is customElements.upgrade, but even adding it, nothing is changed, unlike the example provided in the article:
customElements.upgrade(el);