๐น`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.
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!');
});example
๐พ replit
// โญ๏ธ elem.$('div')
Element.prototype.$ = function(selector){
return this.querySelector(selector);
};
// add 3 new menu items every sec
['Lollipop', 'Apple', 'Milk Shake'].forEach((text, i) => {
setTimeout(() => {
customMenu.insertAdjacentHTML('beforeEnd',
`<li slot="item">${text}</li>`
)
}, 1000 + i * 1000);
})
// change menu title in 4s
setTimeout(() => {
customMenu.$('[slot="title"]').innerHTML = "New menu โญ๏ธ";
}, 4000);<custom-menu id="customMenu">
<!-- โญ๏ธ light dom: slotted elements -->
<span slot="title">Candy menu</span>
</custom-menu>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).
Last updated
Was this helpful?