You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow MutationObserver to observe across <slot>s #1415
As a custom element author, you have a couple of mechanism available to you to detect changes in slotted elements:
The slotchange event
MutationObserver
For most cases slotchange is enough, but if you want to detect changes to attributes for example, you need MutationObserver.
Now as long as you have no nested slots, then everything is fine. However, as soon as you have a nested slot, MutationObserver also no longer works. For example:
In the above scenario <x-bar> would have a MutationObserver that monitors it's own host element. It will not fire when the button changes or a second button is added.
I propose a new option to the MutationObserver.observe() method called flatten or composed (name TBD) that would instruct the observer to also observe any slotted children of any slots it is observing.
What solutions exist today?
None
How would you solve it?
New flatten or composed option to MutationObserver.observe()
Right now, although it's a little clunky, I think you can leverage slotchange to do most of this:
constmyObserver=newMutationObserver(callback);letpreviousAssigned=newSet();slot.addEventListener('slotchange',(e)=>{myObserver.disconnect();constassigned=newSet(this.observeSlot.assignedElements({flatten: true}));assigned.forEach(el=>{myObserver.observe(el,{attributes: true});});// if needed, an process slot changes similar to other mutation observer childList mutations...constaddedNodes=Array.from(assigned.difference(previousAssigned));constremovedNodes=Array.from(previousAssigned.difference(assigned));callback([{target: slot, addedNodes, removedNodes}])previousAssigned=assigned;})
Activity
sorvell commentedon Oct 24, 2025
This would be a useful addition.
Right now, although it's a little clunky, I think you can leverage
slotchangeto do most of this:LeaVerou commentedon Dec 13, 2025
@sorvell
slotchangedoesn't fire for manual assignment, so that's another use case for this.sorvell commentedon Dec 13, 2025
I'd love to see this added to MutationObserver. This is just a point of clarification: seems like it does fire?
LeaVerou commentedon Dec 15, 2025
It appears you're right! No idea why I had that impression, but I’m very glad I was wrong 😅 Thank you!