Skip to content

Allow MutationObserver to observe across <slot>s #1415

@jpzwarte

Description

@jpzwarte

What problem are you trying to solve?

As a custom element author, you have a couple of mechanism available to you to detect changes in slotted elements:

  1. The slotchange event
  2. 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:

<x-foo>
  <template shadowrootmode="open">
    <x-bar>
      <template shadowrootmode="open">
        <slot></slot>
      </template>      
      <slot></slot>
    </x-bar>
  </template>
  <button>Button</button>
</x-foo>

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()

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    addition/proposalNew features or enhancementsneeds implementer interestMoving the issue forward requires implementers to express interest

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions