Im trying to understand how I can use <slot>
properly. I made this example which I would like to be able to access the qty from the tag <li name="qty">102</li>
, which has been exposed as a named slot in the web-component as <slot name="qty"></slot>
Running this example:
const template = document.createElement('template');
template.innerHTML = `
<style>
slot{
color: blue;
}
</style>
<p>
<slot></slot>
<ul>
<slot name="component"></slot>
<slot name="qty"></slot>
</ul>
</p>
`
class SlotTest extends HTMLElement {
constructor(){
super();
let shadow = this.attachShadow({mode: "open"});
this.shadowRoot.appendChild( template.content.cloneNode(true) );
}
connectedCallback(){
this.shadowRoot.querySelectorAll("slot").forEach(element => {
if (element.name === '') {
console.info("unamed slot", element)
} else{
console.info(`slot.innerText = "${element.innerHTML}"`, element)
}
});
}
}
window.customElements.define('track-comp', SlotTest);
<track-comp>
<h2>TLC555CP</h2>
<li name="component">Timer IC</li>
<li name="qty">55</li>
</track-comp>
<track-comp>
<h2>2N2369</h2>
<li name="component">High Speed Switching Transistor</li>
<li name="qty">102</li>
</track-comp>
The innerText or HTML of the slot does not seem to be accessible. Is there a way to see this or do you have to add another property describing the quantity? And as for the un-nmed slot; how is that accessed inside the web component?
EDIT
I accept the answer but just wanted to share the correct thing would be to add an event listener like this in the constructor and handle the update that way.
this.shadowRoot.addEventListener('slotchange', event => console.log('event', event.target, this.nodeName, this.innerText));
So something like this would allow me to react to the slot update and view the slot content.
this.shadowRoot.addEventListener('slotchange', event => {
console.info(this.nodeName);
console.info(this.innerHTML);
});
please take a look at this example for further details on slots