gitmyhub

stimulus

TypeScript ★ 13k updated 10d ago

A modest JavaScript framework for the HTML you already have

Stimulus is a lightweight JavaScript framework that adds interactive behavior to server-rendered HTML using special data attributes, without replacing your existing HTML or rendering pipeline.

TypeScriptJavaScriptnpmsetup: easycomplexity 2/5

Stimulus is a small JavaScript framework made by Basecamp, the company behind Ruby on Rails. Rather than taking over how a page is built or rendered, it adds interactive behavior to HTML that already exists on the page. This makes it a natural fit for server-rendered applications that want some client-side behavior without switching to a heavier frontend framework.

The way it works is through special HTML attributes. You mark elements with attributes that say which controller manages a section of the page, which elements a controller can read from or write to, and which user actions should trigger which methods. Stimulus watches the page for these attributes and connects the right JavaScript controller to the right HTML elements automatically, handling setup and teardown as elements appear and disappear.

Because Stimulus reads the current state of the DOM rather than managing its own rendering, it works cleanly alongside server-side rendering, Turbo (a companion library from the same team for fast page navigation without full reloads), and any other tools that update the page through Ajax or HTML swaps. Stimulus does not need to know who updated the page; it just responds to what is there.

The framework is written in TypeScript and published as an npm package. It can also be added with a plain script tag, requiring no build step. The documentation includes a handbook for getting started and a reference guide for the full API. Stimulus is open source under an MIT license.

Where it fits