I created a header element which at its initial state shows loading
export const FmHeader = (data) => {
fetch("../../templates/fm-header/index.html")
.then((response) => response.text())
.then((html) => HeaderElement(html));
};
const HeaderElement = (html, data) => {
class FmHeader extends HTMLElement {
constructor() {
super();
var el = this.attachShadow({ mode: "open" });
el.innerHTML = html;
if (data) {
const artistName = el.querySelector(".artist_name");
artistName.innerText = "data.artistName;";
artistName.classList.remove("skeleton");
}
}
}
customElements.define("fm-header", FmHeader);
};
I'm calling it like so:
(function(){
FmHeader();
setTimeout(() => {
FmHeader({data: 'some data from server'});
}, 1000);
})()
I understand the error - that the same element can't be redefined. But is there a way to achieve something like this?
A basic code pen version