Maybe this is a stupid question, but why does the `slotchange` event exist? A mutation observer can do the one thing slotchange can do, but also much more. #javascript #WebComponents #shadowDOM
@noleli It’s kind of the same argument for using `.reduce()` for every array use case, right? 😀 The multi-tool might be powerful, but sometimes you just want the tool that does one thing and does it well.
@knowler @noleli Pretty sure it’s for ergonomics—no need to create and manage an observer for this common case—and (more importantly) performance reasons. Although MOs may be relatively cheap, they’re not free; you probably don’t want a large DOM tree full of them, and since the browser code to implement slotting already knows whenever a change occurs, it’s cheaper just to fire an event.

@graynorton @knowler all good points! The event is certainly easier to manage. This came up because I was wishing the event object had some detail similar to a childList mutation record that said what was added or removed.

@tbroyer also made the excellent point that a MO isn’t going to handle manually assigned children, which is something I’d forgotten. (@deebloo also just added that)

@noleli @graynorton @knowler @tbroyer honestly sometimes I avoid the whole thing my manually calling methods on the parent. Child notifies parent it is being added or removed. https://github.com/deebloo/go-board/blob/main/src/stone.element.ts#L54
go-board/src/stone.element.ts at main · deebloo/go-board

Go Board WebComponent. Contribute to deebloo/go-board development by creating an account on GitHub.

GitHub