talira

Stimulus

A modest JavaScript framework for the HTML you already have

open sourceMITself-hostablefree

Stimulus is a small JavaScript framework that adds behaviour to server-rendered HTML instead of rendering it: data-controller, data-action and data-*-target attributes connect elements to controller classes, and state lives in the HTML. It watches the page and connects controllers whenever matching attributes appear, whether from a full page load, a Turbo page change or an Ajax update. 37signals extracted it from Basecamp, and with Turbo it forms Hotwire. The stimulus-rails gem sets it up in Ruby on Rails apps with import maps or a JavaScript bundler, and Rails 7 and later configure it by default.

Problems it solves

  • add a bit of interactivity to server-rendered pages without a single-page app framework
  • our jQuery snippets break after Turbo page changes because nothing rebinds them
  • wire a button in my HTML to a JavaScript method without writing selectors by hand
  • attach JavaScript behaviour to elements that get inserted into the page later
  • organise the scattered JavaScript sprinkles in a Rails app into reusable pieces
  • toggle, copy to clipboard and show a dropdown on pages my server renders
  • keep UI state in the HTML so the server stays the source of truth
  • progressively enhance HTML forms with client-side behaviour
  • a small team wants interactive pages without maintaining a separate front-end app
  • set up JavaScript controllers in a Rails app that uses import maps and no Node build
  • add small pieces of JavaScript behaviour to HTML my server already renders, without React or Vue
  • a lightweight alternative to React for making Rails views interactive
  • connect HTML elements to JavaScript classes with data attributes instead of hand-written selectors

Features

  • controllers connected with data-controller attributes
  • actions with data-action (event->controller#method)
  • targets with data-*-target attributes
  • values stored in data attributes
  • lifecycle callbacks when elements connect and disconnect
  • works with any DOM update: full loads, Turbo and Ajax
  • stimulus-rails installer for import maps or JavaScript bundlers

Limitations

  • Doesn't render HTML or client-side templates: it adds behaviour to HTML the server already rendered1

Capabilities

Has an APIplan not known

Alternatives

The same job

These do what Stimulus does.

  1. htmx High power tools for HTML
  2. React on Rails Integration of React + Webpack + Rails including server-side rendering of React
  3. Alpine.js Your new, lightweight, JavaScript framework

Compare Stimulus with the top 3