I am working with standard custom elements and self-closing custom elements.
When declared immediately adjacent to each other, they are not working entirely as I might have expected.
If I write two standard custom elements immediately adjacent to each other:
<colour-list scheme="rainbow"></colour-list>
<colour-list scheme="zebra"></colour-list>
they both display normally:
const schemes = {
rainbowJSON : '["red", "orange", "yellow", "green", "blue", "indigo", "violet"]',
zebraJSON : '["black", "white", "black", "white", "black"]'
}
class colourList_CustomElement extends HTMLElement {
constructor() {
super();
this.root = this.attachShadow({mode: "open"});
}
connectedCallback() {
this.root.innerHTML = `
<style>
:host {
display: inline-block; /* <= Because Custom elements are display:inline by default */
contain: content; /* <= Because this delivers an immediate performance win */
}
ul {
margin: 0 24px 0 0;
padding: 0;
width: 200px;
list-style-type: none;
}
li {
height: 24px;
text-align: center;
font-weight: bold;
text-shadow: 1px 1px rgba(0, 0, 0, 0.7);
}
</style>
`;
let schemeJSON = schemes[this.getAttribute('scheme') + 'JSON'];
let colours = JSON.parse(schemeJSON);
let colourList = document.createElement('ul');
let listItem;
for (let colour of colours) {
listItem = document.createElement('li');
let textShadow = (colour === 'black') ? 'text-shadow: 1px 1px rgba(255, 255, 255, 0.7);' : '';
listItem.setAttribute('style', 'color: '+ colour + '; background-color: ' + colour + ';' + textShadow);
listItem.textContent = colour;
colourList.appendChild(listItem);
}
this.root.appendChild(colourList);
}
}
customElements.define('colour-list', colourList_CustomElement);
<colour-list scheme="rainbow"></colour-list>
<colour-list scheme="zebra"></colour-list>
If, alternatively, I have a self-closing custom element immediately following a standard custom element:
<colour-list scheme="rainbow"></colour-list>
<colour-list scheme="zebra" />
they both also display normally:
const schemes = {
rainbowJSON : '["red", "orange", "yellow", "green", "blue", "indigo", "violet"]',
zebraJSON : '["black", "white", "black", "white", "black"]'
}
class colourList_CustomElement extends HTMLElement {
constructor() {
super();
this.root = this.attachShadow({mode: "open"});
}
connectedCallback() {
this.root.innerHTML = `
<style>
:host {
display: inline-block; /* <= Because Custom elements are display:inline by default */
contain: content; /* <= Because this delivers an immediate performance win */
}
ul {
margin: 0 24px 0 0;
padding: 0;
width: 200px;
list-style-type: none;
}
li {
height: 24px;
text-align: center;
font-weight: bold;
text-shadow: 1px 1px rgba(0, 0, 0, 0.7);
}
</style>
`;
let schemeJSON = schemes[this.getAttribute('scheme') + 'JSON'];
let colours = JSON.parse(schemeJSON);
let colourList = document.createElement('ul');
let listItem;
for (let colour of colours) {
listItem = document.createElement('li');
let textShadow = (colour === 'black') ? 'text-shadow: 1px 1px rgba(255, 255, 255, 0.7);' : '';
listItem.setAttribute('style', 'color: '+ colour + '; background-color: ' + colour + ';' + textShadow);
listItem.textContent = colour;
colourList.appendChild(listItem);
}
this.root.appendChild(colourList);
}
}
customElements.define('colour-list', colourList_CustomElement);
<colour-list scheme="rainbow"></colour-list>
<colour-list scheme="zebra" />
But, finally, if I have two self-closing elements, one immediately following the previous one, the second custom element does not display at all:
<colour-list scheme="rainbow" />
<colour-list scheme="zebra" />
const schemes = {
rainbowJSON : '["red", "orange", "yellow", "green", "blue", "indigo", "violet"]',
zebraJSON : '["black", "white", "black", "white", "black"]'
}
class colourList_CustomElement extends HTMLElement {
constructor() {
super();
this.root = this.attachShadow({mode: "open"});
}
connectedCallback() {
this.root.innerHTML = `
<style>
:host {
display: inline-block; /* <= Because Custom elements are display:inline by default */
contain: content; /* <= Because this delivers an immediate performance win */
}
ul {
margin: 0 24px 0 0;
padding: 0;
width: 200px;
list-style-type: none;
}
li {
height: 24px;
text-align: center;
font-weight: bold;
text-shadow: 1px 1px rgba(0, 0, 0, 0.7);
}
</style>
`;
let schemeJSON = schemes[this.getAttribute('scheme') + 'JSON'];
let colours = JSON.parse(schemeJSON);
let colourList = document.createElement('ul');
let listItem;
for (let colour of colours) {
listItem = document.createElement('li');
let textShadow = (colour === 'black') ? 'text-shadow: 1px 1px rgba(255, 255, 255, 0.7);' : '';
listItem.setAttribute('style', 'color: '+ colour + '; background-color: ' + colour + ';' + textShadow);
listItem.textContent = colour;
colourList.appendChild(listItem);
}
this.root.appendChild(colourList);
}
}
customElements.define('colour-list', colourList_CustomElement);
<colour-list scheme="rainbow" />
<colour-list scheme="zebra" />
It's not clear to me why in the last example both elements are not displaying.
continuing from comments above...
Note that you can create Unknown Elements (creating a FOUC) which you can querySelect
, process into what you want and then remove from the DOM
<my-elements>
<green id=foo />
<red id=bar />
Bye Bye World
</my-elements>
Hello World!
<script>
customElements.define('my-elements', class extends HTMLElement {
connectedCallback() {
setTimeout(() => {
this.append(...[...this.querySelectorAll("*")].map(node => {
console.log(node.outerHTML);
let div = document.createElement("div");
div.style.color = node.nodeName;
div.innerHTML = `${node.id} ${node.nodeName}`;
node.remove();
return div;
}));
});
}
});
</script>