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

Stimulus Controller Action Lists Look Awful, Are Not Meaningfully Connected #47

Open
Shaka-n opened this issue Apr 3, 2024 · 0 comments

Comments

@Shaka-n
Copy link
Collaborator

Shaka-n commented Apr 3, 2024

          🤣 I know, it looks awful, and the connection with the actual controller actions is so tenuous. 

I was thinking of writing a helper module for stuff like this to make it more readable. It would be really cool if we could make something like Phoenix's verified_routes. That way we wouldn't need to have magic strings. We could round up all the controller actions in the JS at compile time and set a global verified_stimulus_actions map. The tough part would be somehow generating all the valid event triggers to go with them... maybe we wouldn't have to though. Ending up with something like this:

$verified_stimulus_actions = { 'modal': ['closeEnd', 'submitEnd'] }

... then have a macro/resolver of some kind check if the action exists and fail more visibly if it does not:

module StimulusHelper
  def verify_action (action)
    check_for_action($verified_stimulus_actions)
  end
end

...Something like that anyway.

In the meantime, I was thinking something like this just for readability:

module StimulusHelper
  def action_list(actions)
    actions.join(' ')
  end
end

...
### modal_component.html.erb
<tag.div id:'modal-overlay;
  data: {
    controller: 'modal',
    action: StimulusHelper.action_list(["turbo:submit-end->modal#submitEnd", "keydown.esc@window->modal#closeWithEscape"])
  } do %>
  <%= content %>
  <% end %>

Originally posted by @Shaka-n in #44 (comment)

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

No branches or pull requests

1 participant