Have you ever dreamed of being able to write your Stimulus controllers in Ruby instead of JavaScript?
Here's OpalStimulus, a Stimulusjs wrapper for Opal, with a Rails installer.
Obviously for now (while being functional) this project is still in development and not complete, but if you want to contribute or simply try it out, you can do so https://github.com/josephschito/opal_stimulus/pulls
Here's a small example of how it works:
Basic Example
Here's a Hello World example with OpalStimulus. Compare with the original Stimulus example.
Add this line to your Gemfile:
gem 'opal_stimulus'
Run:
bundle install
rails generate opal_stimulus:install
Start the application:
bin/dev
Create app/opal/controllers/hello_controller.rb and fill it with:
class HelloController < StimulusController
self.targets = ["name", "output"]
def greet
output_target.text_content = "Hello, #{name_target.value}!"
end
end
Add this to a view:
<div data-controller="hello">
<input data-hello-target="name" type="text">
<button data-action="click->hello#greet">
Greet
</button>
<span data-hello-target="output">
</span>
</div>
Result:
Enjoy! 😉






too much overhead of having the code full of
.JS[]