Convert Figma logo to code with AI

stimulusreflex logostimulus_reflex

Build reactive applications with the Rails tooling you already know and love.

2,256
171
2,256
23

Top Related Projects

22,155

A full-stack framework for Laravel that takes the pain out of building dynamic UIs.

Rich, real-time user experiences with server-rendered HTML

27,910

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

  1. Basic reflex action:
class ExampleReflex < ApplicationReflex
  def increment
    @count = element.dataset[:count].to_i + 1
  end
end

This reflex increments a counter when triggered.

  1. 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.

  1. 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

  1. Add StimulusReflex to your Gemfile:
gem 'stimulus_reflex'
  1. Run the installer:
bundle install
rails stimulus_reflex:install
  1. Create a new reflex:
rails generate stimulus_reflex example
  1. 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

22,155

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.

27,910

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 Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Welcome to StimulusReflex 👋

downloads License: MIT Documentation
semantic-release Ruby Code Style JavaScript Code Style
Prettier-Standard StandardRB Tests


🎉 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.

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

👩‍👩‍👧 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

  1. Always publish CableReady first!
  2. Update the cable_ready dependency version in stimulus_reflex.gemspec and package.json
  3. Make sure that you run yarn and bundle to pick up the latest.
  4. Bump version number at lib/stimulus_reflex/version.rb. Pre-release versions use .preN
  5. Run bundle exec rake build and yarn build
  6. Run bin/standardize
  7. Commit and push changes to GitHub
  8. Run bundle exec rake release
  9. Run yarn publish --no-git-tag-version
  10. Yarn will prompt you for the new version. Pre-release versions use -preN
  11. Commit and push changes to GitHub
  12. 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. 🙌

NPM DownloadsLast 30 Days