Skip to content

Instantly share code, notes, and snippets.

@amkisko
Last active February 17, 2024 09:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amkisko/04a5e2201b0e284e34cd1d1344bbbcfb to your computer and use it in GitHub Desktop.
Save amkisko/04a5e2201b0e284e34cd1d1344bbbcfb to your computer and use it in GitHub Desktop.
2023.09.10 Opinion on Rails with Stimulusjs usage

Rails views with stimulusjs opinion

References

Known component/view patterns:

vite plugins:

Additional information:

What I like in stimulusjs (updated patterns how I would like to have it)

  • Enough for usage within Rails views, partials and helpers
  • Full compatibility with Turbo
  • Mostly valid HTML (not really valid XML)
  • No additional compilation required (except TypeScript/Dart if any used)
  • No virtual DOM or any additional VMs
  • Default controller id from file path and name
  • Attach component to DOM element: data-component="users-index"
  • Define DOM element action handler: data-action-click="users-index#myclickhandler"
  • Define custom action handler: data-action-mycontroller-myevent="users-index#myeventhandler"
  • Define window action handler: data-window-action-click="users-index#myeventhandler"
  • Define keyboard action handler: data-keyboard-action-keydown="users-index#keydownhandler"
  • Define class binding: data-class-loading="loading"
  • Add value to component: data-value-loader-url="/messages"
  • Set DOM element as target: data-target="myelement"
  • Set action parameters: data-param-item-id="1" data-param-item-url="/items/1"

What I don't like in stimulusjs

  • no explanation of convention on controller names (it just what it is, I can't get double dashes thing "--")
  • no explanation of convention on data attr names (why not using prefixes instead of suffixes?)
  • possibility to attach multiple controllers to the same DOM element
  • not clear encapsulation/scoping
  • no lazy loading, loads everything at once (if one follows "official guidelines")
  • targets, values and selectors very long names that include controller name
  • all events are mixed (would like to have them namespaced on data attr name level)

Possible requirements

  • Try using Dart as compiler for new implementations and in the same time it should be possible to use vanilla JavaScript and TypeScript.
  • Component should have configurable selector, by default it's data-component="my-component-name".
  • It should be possible to add services to initialiser of component (same idea as in Angular with automated injection).
  • It should be possible to load component on demand (aka lazy).
  • There should be similar patterns to Rails (e.g. "mycomponent#mymethod").

Questions

  • how can I load javascript/typescript/dart code per view? (use vite tag always on view template level, basically same as require)
  • how can I load css/scss per view? (use vite tag always on view template level, basically same as require)
  • how do I get same amount of sugar syntax and automations without stimulusjs? also considering possible places for memory leaks and heavy resources usage.

Summary

  • avoid using stimulusjs if you can't follow the practices that will include proper encapsulation, pseudo-lazy loading and other optimisations
  • backbonejs seems to be good alternative, but it does not have any HTML sugar and helpers
  • implement own library
  • use stimulusjs if there are no other options
  • use vitejs for bundling assets, avoid using webpack
  • avoid using backbonejs, reactjs, vuejs, nextjs, angularjs, emberjs, elmjs, etc

DATE: 2023.09.10 AUTHOR: Andrei Makarov (github.com/amkisko)

@amkisko
Copy link
Author

amkisko commented Jan 9, 2024

Update 2023.01.09:

  • Rails 7 with propshaft, importmap-rails, stimulusjs and Hotwire Turbo seems to be the perfect setup, it does not require node and node_modules

@amkisko
Copy link
Author

amkisko commented Feb 17, 2024

Update 2024.02.17:

  • heading towards Rails 8 as methodology, the improved classic experience on the way and already available within Rails 7, just enjoy it!

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