Stimulus
In this guide you will learn how to add client-side behavior to a Proper app with Stimulus.
Stimulus is a small JavaScript framework with a modest goal: it connects JavaScript objects, called controllers, to elements of the page through data- attributes. It does not render HTML. Your Jx components render the HTML on the server, and Stimulus adds the behavior - a dropdown that opens, a field that shows a character count, a form that saves itself.
That division is what makes Stimulus a good fit for a Proper app. Turbo (see the Turbo guide) brings HTML from the server; Stimulus handles the small amount of behavior that has to happen in the browser.
Use Stimulus where Turbo does not apply: to change HTML that is already on the page, without asking the server. Some examples are a keyboard shortcut, a text area that grows as the user types, or a drop zone for uploading files.
This page is a compact introduction with the parts that are specific to Proper. The Stimulus handbook and reference cover every option in more depth.
1. What You Already Have
A new Proper app comes with Stimulus set up:
assets/js/vendor/stimulus.jsis the library itself, vendored into your app so there is no CDN request at runtime.- The
IMPORT_MAPconfig maps the bare name@hotwired/stimulusto that file, soimport { Controller } from "@hotwired/stimulus"works in any of your scripts with no bundler (see Assets - Import Maps). assets/js/application.jsstarts the Stimulus application and keeps it inwindow.Stimulus:
import { Application } from "@hotwired/stimulus"
window.Stimulus = Application.start()
The base layout loads application.js as a module after the import map, so window.Stimulus exists by the time any other script runs.
Proper does not scan a folder for controllers. Each controller registers itself, and each Jx component declares the controller files it needs. The next section shows how.
2. Your First Controller
A controller is a JavaScript class in its own file. The example below hides and shows a panel when a button is clicked.
import { Controller } from "@hotwired/stimulus"
class ToggleController extends Controller {
static targets = ["panel"]
toggle() {
this.panelTarget.hidden = !this.panelTarget.hidden
}
}
window.Stimulus.register("toggle", ToggleController)
The last line registers the class under the identifier toggle. That identifier is what the HTML uses to refer to the controller.
Then a Jx component declares the script with the {#js #} directive and uses the controller in its markup:
{#js "js/toggle-controller.js" #}
{#def label #}
<div data-controller="toggle">
<button type="button" data-action="toggle#toggle">{{ label }}</button>
<div data-toggle-target="panel" hidden>
{{ content }}
</div>
</div>
Three attributes do all the work:
data-controller="toggle"connects aToggleControllerinstance to the<div>. This element is the controller's scope.data-action="toggle#toggle"calls thetoggle()method when the button is clicked.data-toggle-target="panel"marks the element the controller reaches throughthis.panelTarget.
The {#js #} directive is what ties a controller file to a component. The layout collects the scripts of every component used on the page and emits one <script type="module"> for each, so a controller loads only on pages that render the component (see Jx Components - Assets). The path is relative to the assets/ folder.
2.1 Naming
Pick a short, kebab-case identifier that describes the behavior (toggle, autosave, char-count), and name the file after it: assets/js/<identifier>-controller.js. The identifier appears in every attribute, so a dash in it is written as is: data-char-count-target.
Name the controller after what it does, not where it is used. A toggle controller works on a menu, a sidebar, and a details panel. A sidebar controller works on one thing.
3. The Building Blocks
3.1 Targets
Targets are the elements a controller needs to reach. Declare them in static targets and mark them with data-<identifier>-target:
static targets = ["input", "count"]
<div data-controller="char-count">
<textarea data-char-count-target="input"></textarea>
<span data-char-count-target="count"></span>
</div>
For a target named input, the controller gets three properties:
| Property | Returns |
|---|---|
this.inputTarget |
the first matching element (an error if there is none) |
this.inputTargets |
all matching elements, as an array |
this.hasInputTarget |
true if at least one exists |
A target must be inside the controller's element. Nested controllers with the same identifier do not see each other's targets.
3.2 Actions
Actions connect DOM events to controller methods. The format of data-action is event->identifier#method:
<input data-action="input->char-count#update">
Common elements have a default event, so you can leave it out: click for buttons and links, input for text fields and text areas, change for selects and checkboxes, submit for forms. The toggle#toggle of the first example relied on this.
Several actions go in the same attribute, separated by spaces. Two suffixes extend where an action listens:
@windowor@documentlistens on the window or the document instead of the element:click@window->menu#close..keyafter a keyboard event filters by key:keydown.esc->menu#close,keydown.enter,keydown.ctrl+s.
Options after the method change the handling: menu#close:prevent calls preventDefault(), :stop calls stopPropagation(), :once runs the action one time.
A dropdown menu is the classic case. It opens on click, and closes on a click anywhere else or on the Escape key. Without Stimulus, that is three listeners to set up and remove by hand. With actions, it is markup:
{#js "js/menu-controller.js" #}
{#def label #}
<div data-controller="menu"
data-action="keydown.esc->menu#close click@window->menu#closeOutside">
<button type="button" data-action="menu#toggle">{{ label }}</button>
<div data-menu-target="panel" hidden>
{{ content }}
</div>
</div>
import { Controller } from "@hotwired/stimulus"
class MenuController extends Controller {
static targets = ["panel"]
toggle() {
this.panelTarget.hidden = !this.panelTarget.hidden
}
close() {
this.panelTarget.hidden = true
}
closeOutside(event) {
if (!this.element.contains(event.target)) this.close()
}
}
window.Stimulus.register("menu", MenuController)
Every action method receives the event. event.target is the element that fired it, and event.params holds any action parameters set on it as data-<identifier>-<name>-param attributes.
3.3 Values
Values are typed settings read from data-<identifier>-<name>-value attributes. This is the way to pass data from your Python code to a controller:
static values = {
url: String,
interval: { type: Number, default: 2000 },
enabled: Boolean,
}
<form data-controller="autosave"
data-autosave-url-value="{{ url_for('Draft.update', object=draft) }}"
data-autosave-interval-value="5000">
The controller reads this.urlValue, this.intervalValue, and this.enabledValue, converted to the declared type. Writing to this.intervalValue updates the attribute. Types are String, Number, Boolean, Array, and Object; the last two are read as JSON, so render them with Jinja's tojson filter: data-chart-points-value="{{ points | tojson }}".
Define a method named <name>ValueChanged(value, previousValue) and Stimulus calls it when the value changes, and once when the controller connects.
3.4 Classes
Classes keep CSS class names out of your JavaScript. The controller declares the names it needs, and the HTML supplies the actual classes:
static classes = ["open"]
toggle() {
this.element.classList.toggle(this.openClass)
}
<div data-controller="menu" data-menu-open-class="is-open">
this.openClass returns the string, and this.openClasses returns an array when the attribute lists several classes separated by spaces.
3.5 Outlets
Outlets let one controller talk to another by CSS selector:
static outlets = ["result"]
search() {
this.resultOutlets.forEach((result) => result.show())
}
<div data-controller="search" data-search-result-outlet=".result">
this.resultOutlet is the first controller instance matching the selector, and this.resultOutlets is all of them. The target element must have its own data-controller="result".
3.6 Lifecycle Callbacks
Stimulus calls three methods on its own:
| Method | When |
|---|---|
initialize() |
once, the first time the controller is created |
connect() |
each time the controller's element is attached to the document |
disconnect() |
each time the element is removed from the document |
Use connect() for setup that touches the page (start a timer, create an observer, focus a field) and disconnect() to undo it. Anything you add outside the controller's element in connect(), such as a listener on window, a setInterval, or a ResizeObserver, must be removed in disconnect(), or it will stay behind after the element is gone.
Targets have callbacks too: <name>TargetConnected(element) and <name>TargetDisconnected(element) run when a target is added to or removed from the page, which is how a controller notices rows that Turbo appended.
3.7 Events Between Controllers
A controller can emit a DOM event with this.dispatch("saved", { detail: { id } }). The event is named <identifier>:saved, bubbles from the controller's element, and any other controller can listen with data-action="autosave:saved->flash#show".
4. Controllers and Jx Components
Jx components and Stimulus controllers pair well: one component renders the markup, one controller gives it behavior, and the {#js #} directive keeps them together. A few points help when you write the component.
Attribute names. Keyword arguments in Jx turn underscores into dashes when rendered as attributes. That matters for anything built through attrs or through tags that take keyword arguments, like frame:
{% do attrs.set(data_controller="autosave", data_autosave_url_value=url) %}
<form {{ attrs.render() }}>
renders data-controller="autosave" data-autosave-url-value="...". In plain HTML inside the component, write the dashes yourself.
Form fields. Formidable's field renderers accept extra attributes the same way, so you can attach a target or an action to the input they render. The password component of the auth addon does exactly this:
{#def field, label: str #}
{#js "js/show-password-controller.js" #}
{% do attrs.set(data_show_password_target="input") %}
<div class="password-field" data-controller="show-password">
{{ field.label(label) }}
{{ field.password_input(**attrs.as_dict) }}
<button type="button"
data-show-password-target="show"
data-action="show-password#show">Reveal</button>
<button type="button" class="hidden"
data-show-password-target="hide"
data-action="show-password#hide">Hide</button>
{{ field.error_tag() }}
</div>
Nested forms. Adding and removing sub-forms in the browser does not need a controller of your own. Formidable ships a script for it; see Rendering Forms - Dynamic Add / Remove.
One controller, many components. Several components can declare the same {#js #} file. The layout emits the script once per page, and the controller connects to every element that names it.
The addons as examples. The auth addon ships show-password-controller.js, and the storage addon ships file-input-controller.js and image-input-controller.js. Installing an addon copies them into your assets/js/, so they are a good place to see real controllers that use targets, classes, and actions together.
5. Stimulus and Turbo
Stimulus watches the document with a MutationObserver, so it does not care how an element arrived. This is what makes it work with Turbo without extra code:
- Drive replaces the
<body>on every visit. Controllers on the old body are disconnected, controllers on the new body are connected.connect()runs again on every page that has the element. - Frames and Streams insert and replace fragments. Controllers inside them connect as soon as the fragment lands, and
<name>TargetConnectedfires for new targets. - Scripts load once. When a visit brings a page whose components declare a controller the current page did not have, Turbo adds the new
<script type="module">to the<head>, and the browser runs it. Scripts already in the head are not run again, sowindow.Stimulus.registeris called once per controller for the life of the browser tab.
Two consequences to keep in mind:
Clean up in disconnect(). Because Drive keeps the window alive between visits, a listener on window or a running interval does not go away when the page changes. Remove in disconnect() everything connect() created outside the element.
The page snapshot comes first. Before Drive replaces the body, it saves a snapshot of the current page to show on "Back" and as a preview. That happens before disconnect() runs, so changes you make in disconnect() do not reach the snapshot. To leave the page in a state that makes sense when it reappears, listen for the turbo:before-cache event:
connect() {
this.reset = this.reset.bind(this)
document.addEventListener("turbo:before-cache", this.reset)
}
disconnect() {
document.removeEventListener("turbo:before-cache", this.reset)
}
reset() {
this.panelTarget.hidden = true
}
For elements that should simply not be in the snapshot, such as a flash message, the data-turbo-temporary attribute needs no JavaScript. See Turbo - Cleanup before leaving.
Note
application.js also re-dispatches DOMContentLoaded on every turbo:load. That is for third-party scripts written for full page loads. Stimulus controllers do not need it; use connect() instead.
6. Patterns
6.1 Split controllers to make them composable
Say you are asked for a personal bio field with a 280-character limit, and the spec calls for showing the character count, autosizing the text area, and autosaving the form.
You will likely need these behaviors elsewhere. So, instead of one bio controller that wraps all of them, write three: an autosave controller on the form, an autosize controller on the text area, and a char-count controller on a <div> that wraps the text area and the counter. Each one is small, works alone, and can be reused on the next form:
<form data-controller="autosave" data-autosave-url-value="{{ url_for('Profile.update') }}">
<div data-controller="char-count" data-char-count-max-value="280">
<textarea name="bio"
data-controller="autosize"
data-char-count-target="input"
data-action="input->char-count#update input->autosave#schedule"></textarea>
<span data-char-count-target="count"></span>
</div>
</form>
6.2 Use actions instead of hand-written listeners
Keyboard shortcuts, clicks outside an element, and window events are all a data-action away: keydown.esc->, click@window->, resize@window->. Writing them as actions keeps the listeners in the markup, next to the element they belong to, and Stimulus removes them when the element goes away. Reach for addEventListener only for events Stimulus cannot express, and then always pair it with a removal in disconnect().
6.3 Keep state in the DOM
A controller is recreated on every Turbo visit, so state held in JavaScript properties does not survive navigation, and it is invisible to the server. Prefer values and classes: they live on the element, Turbo snapshots them, and a stream that replaces the element also resets them. Store in a JavaScript property only what has no meaning outside the current connection, like a timer id.
7. Related
- Stimulus Handbook and Reference.
- Turbo - Drive, Frames, and Streams, and the patterns that pair with Stimulus.
- Jx Components - the
{#js #}directive and theattrsobject. - Assets - import maps and how
stimulus.jsis served.