rolemodel/rolemodel-skills

stimulus-controllers

Create and register Stimulus controllers for interactive JavaScript features. Use when adding client-side interactivity, dynamic UI updates, or when the user mentions Stimulus controllers or JavaScript behavior.

Hot #4093 First seen Feb 19, 2026

Installation

$ npx skills add rolemodel/rolemodel-skills --skill stimulus-controllers

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from rolemodel/rolemodel-skills · top by installs.

npx skills add rolemodel/rolemodel-skills

Browse all from rolemodel/rolemodel-skills

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Repository health

Stars 6
License LICENSE
Default branch main
Open issues 0
Status Active

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 4,840 B
  • docs SUMMARY.md 239 B

History

  1. First seen on skills.sh
  2. First recorded snapshot · 95 installs

SKILL.md

Stimulus Controllers

Overview

Stimulus controllers provide modular JavaScript functionality connected to HTML via data attributes. After creating a new controller, you must register it in the index.js file.

Creating a New Controller

1. Create the Controller File

Create a new controller in app/javascript/controllers/:

// app/javascript/controllers/example_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static targets = ["element"]
  static values = { name: String }

  connect() {
    // Called when controller is connected to DOM
  }

  disconnect() {
    // Called when controller is disconnected from DOM
  }

  // Action methods
  handleClick(event) {
    event.preventDefault()
    // Your logic here
  }
}

2. Register the Controller

CRITICAL: After creating a new controller, run:

bin/rails stimulus:manifest:update

This automatically updates app/javascript/controllers/index.js to register your controller.

Manual Registration (if needed):

import ExampleController from "./example_controller"
application.register("example", ExampleController)

Controller name in HTML uses kebab-case: data-controller="example"

3. Use in HTML

Connect the controller to HTML elements:

.container data-controller="example" data-example-name-value="test"
  button data-action="click->example#handleClick" Click Me
  div data-example-target="element" Target Element

Naming Conventions

  • File: examplecontroller.js (snakecase)
  • Class: export default class extends Controller
  • Registration: "example" (kebab-case)
  • HTML: data-controller="example" (kebab-case)
  • Multi-word: bulksubmitcontroller.js"bulk-submit"

Key Concepts

Targets

Reference specific DOM elements:

static targets = ["input", "output"]

// Access in methods:
this.inputTarget        // First matching element
this.inputTargets       // All matching elements
this.hasInputTarget     // Boolean check

Values

Type-safe data attributes:

static values = {
  url: String,
  count: Number,
  active: Boolean,
  items: Array,
  config: Object
}

// Access in methods:
this.urlValue
this.countValue

// Watch for changes:
urlValueChanged(newUrl, oldUrl) {
  // Called when value changes
}

Actions

Connect events to methods:

<!-- Basic action -->
data-action="click->example#save"

<!-- Multiple actions -->
data-action="click->example#save submit->example#submit"

<!-- Custom events -->
data-action="example:refresh->example#reload"

<!-- Event modifiers -->
data-action="submit->example#save:prevent"

Classes

Manage CSS classes:

static classes = ["active", "hidden"]

// Use in methods:
this.element.classList.add(this.activeClass)
this.element.classList.remove(this.hiddenClass)

Common Patterns

Form Validation

export default class extends Controller {
  static targets = ["form", "submit"]

  validate() {
    const isValid = this.formTarget.checkValidity()
    this.submitTarget.disabled = !isValid
  }
}

Toggle Visibility

export default class extends Controller {
  static targets = ["content"]
  static classes = ["hidden"]

  toggle() {
    this.contentTarget.classList.toggle(this.hiddenClass)
  }
}

AJAX Updates

export default class extends Controller {
  static values = { url: String }

  async refresh() {
    const response = await fetch(this.urlValue)
    const html = await response.text()
    this.element.innerHTML = html
  }
}

Testing

Test Stimulus controllers in system specs:

it 'handles interaction', :js do
  visit page_path

  click_button 'Toggle'

  expect(page).to have_css('[data-controller="example"]')
end

Troubleshooting

Controller not working?

  1. Verify controller is registered in index.js
  2. Run bin/rails stimulus:manifest:update
  3. Check browser console for errors
  4. Verify data attribute spelling (kebab-case)
  5. Ensure JavaScript is enabled in tests (:js tag)

Targets not found?

  • Check target name in static targets matches HTML
  • Use hasXxxTarget to verify existence before accessing
  • Ensure target element is in controller scope

Related Skills

  • [frontend-patterns](../frontend-patterns/SKILL.md) - HTML and CSS patterns
  • [turbo-fetch](../turbo-fetch/SKILL.md) - Dynamic form updates
  • [testing-patterns](../testing-patterns/SKILL.md) - Testing JavaScript features