Skip to content
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

Proposal: Mutation Observation Syntax #397

Closed
wants to merge 1 commit into from

Conversation

seanpdoyle
Copy link
Contributor

@seanpdoyle seanpdoyle commented Apr 27, 2021

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 type
mutations 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 symbols
like @... 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 for
DOMTokenList 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.

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#367][] should cover `childList` type
mutations like the addition or removal of controller targets.

One alternative could involve combining `[data-mutation]` and
`[data-action]` into a single DOMTokenList, and using additional symbols
like `@...` 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 for
`DOMTokenList` 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.

[hotwired#367]: hotwired#367
[event listen options]: https://stimulus.hotwire.dev/reference/actions#options
[MutationObserverInit options]: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserverInit#properties
@dhh
Copy link
Member

dhh commented Sep 23, 2021

As mentioned on #445, I think adding more syntax in the HTML for this is probably going to tip the complexity scales too far. I think the pattern we have with targetChanged is the way to go and extend. As well as considering a general purpose mutation observer helper function where you can get very specific.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants