Proposal: Mutation Observation Syntax #397
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add attribute-level support for monitoring changes to attributes on the
marked element and its descendants.
The
data-mutation
syntax draws direct inspiration from the[data-action]
syntax for routing browser events.Similar to how the Action syntax supports event listener options,
the Mutation syntax would support MutationObserverInit options like
!subtree
.The proposed hooks only cover attribute mutations, since the proposal
made by hotwired/stimulus#367 should cover
childList
typemutations like the addition or removal of controller targets.
One alternative could involve combining
[data-mutation]
and[data-action]
into a single DOMTokenList keyed by the existing[data-action]
attribute and using additional symbolslike
@...
or wrapping[...]
as a differentiators (e.g.@aria-expanded->disclosure#toggle
or[aria-expanded]->disclosure#toggle
).Another could push this responsibility application-side by introducing
more publicly available
MutationObserver
utilities like those used forDOMTokenList
parsing or deconstructing the[data-action]
directives.Once available, those utilities could be used by consumers to listen for
their own mutations and "route" them to actions by combining action
directive parsing and
Application.getControllerForElementAndIdentifier(element, identifier)
to invoke fuctions on a
Controller
instance.Notes
The proposal in its current form only includes changes to the test suite for the sake of starting the conversation and demonstrating the concept and usage.
If there's interest, exploring the implementation details can follow.