stimulus_reflex
Build reactive applications with the Rails tooling you already know and love.
Top Related Projects
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Rich, real-time user experiences with server-rendered HTML
A rugged, minimal framework for composing JavaScript behavior in your markup.
Quick Overview
StimulusReflex is a Ruby on Rails library that enables real-time, reactive user interfaces without writing JavaScript. It combines the Stimulus JavaScript framework with server-side Ruby processing to create a seamless, fast, and developer-friendly way to build modern web applications.
Pros
- Simplifies building reactive UIs by leveraging server-side Ruby instead of complex JavaScript
- Integrates seamlessly with existing Rails applications
- Provides a familiar development experience for Ruby on Rails developers
- Reduces the need for separate API endpoints and complex state management
Cons
- Learning curve for developers new to the Stimulus framework or reactive programming concepts
- May not be suitable for applications requiring complex client-side logic
- Potential performance limitations for high-traffic applications due to increased server load
- Limited ecosystem compared to popular JavaScript frameworks like React or Vue
Code Examples
- Basic reflex action:
class ExampleReflex < ApplicationReflex
def increment
@count = element.dataset[:count].to_i + 1
end
end
This reflex increments a counter when triggered.
- Morph specific DOM elements:
class ExampleReflex < ApplicationReflex
def update_content
morph "#content", "New content"
end
end
This reflex updates the content of a specific DOM element.
- Reflex with form submission:
class ExampleReflex < ApplicationReflex
def submit_form
@user = User.new(user_params)
if @user.save
morph :nothing
else
morph "#form", render(partial: "form", locals: { user: @user })
end
end
private
def user_params
params.require(:user).permit(:name, :email)
end
end
This reflex handles form submission, saving a new user or re-rendering the form with errors.
Getting Started
- Add StimulusReflex to your Gemfile:
gem 'stimulus_reflex'
- Run the installer:
bundle install
rails stimulus_reflex:install
- Create a new reflex:
rails generate stimulus_reflex example
- Use the reflex in your view:
<%= link_to "Increment", "#", data: { reflex: "click->Example#increment", count: @count } %>
<p><%= @count %></p>
This sets up a basic StimulusReflex action that increments a counter when a link is clicked.
Competitor Comparisons
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Pros of Livewire
- Easier learning curve for PHP developers
- Better integration with Laravel ecosystem
- More flexible component structure
Cons of Livewire
- Potentially higher server load due to more frequent requests
- Limited JavaScript interoperability compared to Stimulus Reflex
- Slightly more verbose syntax for complex interactions
Code Comparison
Livewire component:
class SearchUsers extends Component
{
public $search = '';
public function render()
{
return view('livewire.search-users', [
'users' => User::where('name', 'like', "%{$this->search}%")->get(),
]);
}
}
Stimulus Reflex component:
class SearchReflex < ApplicationReflex
def search
@users = User.where("name LIKE ?", "%#{element.value}%")
end
end
Both frameworks aim to simplify real-time interactions in web applications, but they approach the problem differently. Livewire is more tightly integrated with Laravel and offers a more PHP-centric development experience. Stimulus Reflex, on the other hand, leverages the power of Rails and provides a more JavaScript-friendly environment. The choice between the two often depends on the developer's familiarity with the respective ecosystems and the specific requirements of the project.
Rich, real-time user experiences with server-rendered HTML
Pros of Phoenix LiveView
- Built on Elixir and Phoenix, offering excellent performance and scalability
- Seamless integration with existing Phoenix applications
- Strong support for real-time features and WebSocket communication
Cons of Phoenix LiveView
- Steeper learning curve for developers unfamiliar with Elixir and Phoenix
- Smaller ecosystem and community compared to Ruby on Rails
- Limited JavaScript interoperability compared to Stimulus Reflex
Code Comparison
Phoenix LiveView:
def mount(_params, _session, socket) do
{:ok, assign(socket, count: 0)}
end
def handle_event("increment", _, socket) do
{:noreply, update(socket, :count, &(&1 + 1))}
end
Stimulus Reflex:
def increment
@count += 1
end
def decrement
@count -= 1
end
Phoenix LiveView uses Elixir's pattern matching and functional programming paradigms, while Stimulus Reflex leverages Ruby's object-oriented approach. LiveView's code is more concise and declarative, whereas Stimulus Reflex relies on imperative state mutations.
Both frameworks aim to simplify real-time UI updates, but they approach the problem differently. Phoenix LiveView operates at a lower level, providing more control over the rendering process, while Stimulus Reflex builds on top of Rails and Stimulus, offering a familiar environment for Ruby developers.
A rugged, minimal framework for composing JavaScript behavior in your markup.
Pros of Alpine
- Lightweight and minimal, with a small learning curve
- Works well with existing server-rendered HTML
- Can be used without a build step or complex setup
Cons of Alpine
- Limited ecosystem and fewer advanced features
- Less suitable for complex, data-heavy applications
- Lacks built-in server-side rendering capabilities
Code Comparison
Alpine:
<div x-data="{ open: false }">
<button @click="open = !open">Toggle</button>
<div x-show="open">Content</div>
</div>
Stimulus Reflex:
class ToggleReflex < ApplicationReflex
def toggle
@open = !@open
end
end
<div data-controller="toggle">
<%= button_to "Toggle", "#", data: { reflex: "click->ToggleReflex#toggle" } %>
<div data-toggle-target="content" class="<%= @open ? '' : 'hidden' %>">Content</div>
</div>
Alpine is more concise for simple interactions, while Stimulus Reflex integrates tightly with server-side logic. Alpine excels in lightweight, client-side interactivity, whereas Stimulus Reflex shines in full-stack applications with real-time updates and complex state management.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Welcome to StimulusReflex ð
ð An exciting new way to build modern, reactive, real-time apps with Ruby on Rails.
StimulusReflex eliminates the complexity imposed by full-stack frontend frameworks. And, it's fast.
It works seamlessly with the Rails tooling you already know and love.
- Server-rendered HTML, delivered in milliseconds over the wire via Websockets
- ERB templates and partials, with first-class ViewComponent support
- Russian doll caching and ActiveJob
- StimulusJS and Turbolinks/Turbo Drive
- Built with CableReady, our secret power-move
Our goal is to help small teams do big things with familiar tools.
This project strives to live up to the vision outlined in The Rails Doctrine.
ð Docs
⨠Demos
- Build a Twitter Clone in 10 Minutes (video)
- BeastMode - faceted search, with filtering, sorting and pagination
- StimulusReflex Patterns - single-file SR apps hosted on Glitch
- Boxdrop - a Dropbox-inspired concept demo
ð©âð©âð§ Discord Community
Please join over 2000 of us on Discord for support getting started, as well as active discussions around Rails, Hotwire, Stimulus, Phlex and CableReady.
Stop by #newcomers and introduce yourselves!
ð Support
Your best bet is to ask for help on Discord before filing an issue on GitHub. We are happy to help, and we ask people who need help to come with all relevant code to look at. A git repo is preferred, but Gists are fine, too. If you need a template for reproducing your issue, try this.
Please note that we are not actively providing support on Stack Overflow. If you post there, we likely won't see it.
ð Installation and upgrading
CLI and manual setup procedures are fully detailed in the official docs.
Rubygem
bundle add stimulus_reflex
JavaScript
There are a few ways to install the StimulusReflex JavaScript client, depending on your application setup.
ESBuild / Webpacker
yarn add stimulus_reflex
Importmaps
# config/importmap.rb
# ...
pin 'stimulus_reflex', to: 'stimulus_reflex.js', preload: true
Rails Asset pipeline (Sprockets):
<!-- app/views/layouts/application.html.erb -->
<%= javascript_include_tag "stimulus_reflex.umd.js", "data-turbo-track": "reload" %>
ð Contributing
Code of Conduct
Everyone interacting with the StimulusReflex projectâs codebases, issue trackers, chat rooms and forum is expected to follow the Code of Conduct.
Coding Standards
This project uses Standard for Ruby code and Prettier-Standard for JavaScript code to minimize bike shedding related to source formatting.
Please run ./bin/standardize
prior to submitting pull requests.
View the wiki to see recommendations for configuring your editor to work best with the project.
ð¦ Releasing
- Always publish CableReady first!
- Update the
cable_ready
dependency version instimulus_reflex.gemspec
andpackage.json
- Make sure that you run
yarn
andbundle
to pick up the latest. - Bump version number at
lib/stimulus_reflex/version.rb
. Pre-release versions use.preN
- Run
bundle exec rake build
andyarn build
- Run
bin/standardize
- Commit and push changes to GitHub
- Run
bundle exec rake release
- Run
yarn publish --no-git-tag-version
- Yarn will prompt you for the new version. Pre-release versions use
-preN
- Commit and push changes to GitHub
- Create a new release on GitHub (here) and generate the changelog for the stable release for it
ð License
StimulusReflex is released under the MIT License.
Originally inspired by Phoenix LiveView. ð
Top Related Projects
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
Rich, real-time user experiences with server-rendered HTML
A rugged, minimal framework for composing JavaScript behavior in your markup.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot