Re: [WICG/webcomponents] Options for controlling specifics of "slotchange" events? (#933)

I fully agree that the first impression of how `slotchange` works tends to be "anything that changes in the slot will emit this event" and that's obviously not true. I also agree that having _something_ to solve this more elegantly would be useful, so thank you for starting the discussion!

The issue seems to stem from the ambiguity of "change." What exactly is changing? With `slotchange`, it's not changes to the slotted element's attributes nor its children — in those cases, the element remains the same so, correctly, no event is emitted. Only when the slotted element itself is added/removed will the event emitted. IMO, this behavior is correct and acceptable, albeit sometimes inconvenient.

What this proposes is similar to a partial reimplementation of the [MutationEvent API](https://developer.mozilla.org/en-US/docs/Web/API/MutationEvent), which was deprecated [for performance reasons](https://developer.mozilla.org/en-US/docs/Web/API/MutationEvent#performance). Even if performance were a non-issue, the concept of customizing when or how events are emitted through options and/or attributes feels a bit weird. I can't think of any other APIs that work like this.

If this were to be supported, I would prefer not to allow this:

```js
this.attachShadow({ type: 'open', slotChangeOptions: { ... });
```

And instead explicitly opt each slot in:

```html
<slot subtree childlist></slot>
```

While I don't think an event-based approach to this problem will be entertained due to performance reasons, it doesn't seem difficult to polyfill this behavior.

1. Listen to `slotchange` on all slots
2. Attach a mutation observer to each slotted element, emitting a custom event (e.g. `slotmutated`)
3. Add a mutation observer to watch all slot attributes (e.g. `subtree childlist etc.`)
4. Update the observer when a slot or any of its "option" attributes change

It would be a pretty handy utility that gives you the behavior you want without the performance implications.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/933#issuecomment-868504799

Received on Friday, 25 June 2021 13:36:18 UTC