hotwired / hotwired/stimulus

Using action option changes the order of actions

Open
#718 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
13.1k
Forks
441
PR merge metrics
No merged PRs in 30d

Description

In certain situations, using an action option such as :prevent can change the order in which actions registered in the same data-action are executed.

Here is a self-contained example demonstrating the problem:

<html>
  <head>
    <script type="module">
      import { Application, Controller } from "https://unpkg.com/@hotwired/stimulus/dist/stimulus.js"
      window.Stimulus = Application.start()
      Stimulus.debug = true
      Stimulus.register("one", class extends Controller {
        foo() {}
      })
      Stimulus.register("two", class extends Controller {
        bar() {}
      })
    </script>
  </head>
  <body>
    <div data-controller="two">
      <div data-controller="one">
        <a href="#" data-action="one#foo two#bar">Click me</a>
      </div>
    </div>
  </body>
</html>

Without the action option, the debug output shows the correct order:

one #foo
two #bar

However, if I use an action option:

<a href="#" data-action="one#foo:prevent two#bar">Click me</a>

The debug output shows the order in which actions are executed has changed:

two #bar
one #foo

This seems like a bug to me. I encountered it in a scenario where the order of actions was important, and it took me a while to realize it's because of the action option.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the self-contained HTML example from the issue with Stimulus debug output enabled, comparing the two data-action values. Trace the action-option handling and dispatch order, then verify that adding :prevent does not reorder one#foo and two#bar while preserving the option’s behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.