New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Observing slotchange mutation records: a SlotChangeObserver
API similar to MutationObserver
with childList:true
#1042
Comments
SlotChangeObserver
API similar to MutationObserver
with childList:true
slotchange doesn't use a separate task |
Definitely in agreement that we need more power in this area: #933 The other thing that |
+1 to this idea. Another thing that I'd like to advocate for with That said, if the Rationale A lot (all?) front end template frameworks will try to intelligently re-use DOM elements when state updates cause DOM re-renders. I know for a fact that both React and Lit intelligently re-use DOM when possible. If slotted children of a web component are re-used by a framework re-render cycle and thereby only the attributes/textContent are changed, Currently developers that consume web components implemented wth |
The issue
The
slotchange
event is similar to DOM Mutation Events (but better), but it does not provide a list of changes (being better, it is batched, , so someone using this event to track which slotted nodes were added and which were removed has to implement a diff strategy by tracking previous slotted nodes to diff against.But a diff strategy ends up in net-zero changes if an element was disconnected from DOM and then reconnected to the same parent in the same tick, because the end result is that the reconnected node is still slotted to the same slot. Because
slotchange
runs once in a future task after the current macrotask, all changes are lumped into one event with no way to detect what happened apart from diffing.Here is code showing what we want to do (but which is not currently possible):
With
MutationObserver
, it is possible to react to each mutation. When we are building complex apps that rely on the composed tree shape, it is desirable to be able to react to all mutations. Currently:Because of this discrepancy, complex state that relies on dis/connectedCallback, MutationObserver, and slotchange together, can get out of sync when a synchronous disconnect and reconnect happens:
Possible solution
Either
slotchange
event objects can be updated to include change records, or a newSlotChangeObserver
(or similar named API) can be added with very similar shape to MutationObserver but only for slot change added/removed nodes.Important
If we add change records to the
slotchange
events, or a newSlotChangeObserver
API, we must be absolutely sure to not repeat the problem thatMutationObserver
has, so sanity sake:childList
records across multipleMutationObserver
s are observed in fundamentally incorrect order whatwg/dom#1111Besides the above hypotehtical example using
slotchange
events, here's a hypotheticalSlotChangeObserver
example:The text was updated successfully, but these errors were encountered: