๐น`slotchange` event
JS.info โฉ Updating slots
Google โฉ Working with slots in JS
MDN โฉ MutationObserver
MDN โฉ
HTMLSlotElement โฉ assignedNodes(): Node[ ]
Element โฉ assignedSlot: HTMLSlotElement - slot that element is assigned to.
Note: "slotchange" does not fire when an instance of the component is first initialized.
The slotchange event fires when a slot's distributed nodes changes. For example, if the user adds/removes children from the light DOM.
const slot = this.shadowRoot.querySelector('#slot');
// โญ๏ธ `slotchange` event listener
slot.addEventListener('slotchange', e => {
console.log('light dom children changed!');
});To monitor other types of changes to light DOM, you can setup a MutationObserver in your element's constructor.
example
๐พ replit
const { log } = console;
// CustomMenu
class CustomMenu extends HTMLElement {
// connected to doc
connectedCallback() {
// shadow root
const root = this.attachShadow({mode: 'open'});
// shadow dom tree (with named slots โญ๏ธ)
root.innerHTML = `
<div class="container">
<slot name="title"></slot>
<ul><slot name="item"></slot></ul>
</div>
`;
// container (div.container)
this.container = this.shadowRoot.firstElementChild;
// โญ๏ธ shadowRoot can't have event handlers,
// so using the first child.
this.container.addEventListener('slotchange', e => {
let slot = e.target; // changed slot (in shadow DOM)
log("slotchange: " + slot.name) // โญ๏ธ slot name
if(slot.name === 'item'){
// โญ๏ธ slot.assignedElements() is of type `Element[]`
let items = slot.assignedElements().map(elem => elem.textContent);
log('items:', items);
}
});
}// end: connectedCallback()
}// end: CustomMenu
// register <custom-menu> tag
customElements.define('custom-menu', CustomMenu);slot name
notes
title
<span slot="title">Candy menu</span> inserted into title slot.
item
new <li slot="item"> tag inserted into item slot.
item
new <li slot="item"> tag inserted into item slot.
item
new <li slot="item"> tag inserted into item slot.
there's NOslotchange event after 4 sec (on modifying the content of a slotted element).
If weโd like to track internal modifications of light DOM from JavaScript, use: MutationObserver.
Last updated