Support custom Action Options #567
Merged
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.
As a follow-up to hotwired/stimulus#535 and
hotwired/stimulus#546, add support for declaring custom action
modifiers in the same style as
:prevent,:stop, and:self.Take, for example, the toggle event. It's dispatched whenever a
<details>element toggles either open or closed. If an applicationwere able to declare a custom
openmodifier, it could choose to routetoggleevents denoted with:openonly when the<details open>.Inversely, they could choose to route
toggleevents denoted with:!openonly when the<details>does not have[open].Similarly, the same kind of customization could apply to custom events.
For example, the turbo:submit-end fires after a
<form>element submits, but does not distinguish between success or failure. A
:successmodifier could skip events with an unsuccessful HTTP responsecode.
Preview of changes to the
Actionsdocumentation page:You can register your own action options with the
Application.registerActionOptionmethod.For example, consider that a
<details>element will dispatch a toggleevent whenever it's toggled. A custom
:openaction option would helpto route events whenever the element is toggled open:
Similarly, a custom
:!openaction option could route events whenever theelement is toggled closed. Declaring the action descriptor option with a
!prefix will yield a
valueargument set tofalsein the callback:In order to prevent the event from being routed to the controller action, the
registerActionOptioncallback function must returnfalse. Otherwise, toroute the event to the controller action, return
true.The callback accepts a single object argument with the following keys:
"open"in the example above):openwould yieldtrue,:!openwould yieldfalse)