Better Stimulus logo

Better Stimulus

Community
obie
better-stimulus

Apply Better Stimulus best practices for writing maintainable, reusable StimulusJS controllers following SOLID principles

Overview

Publisherobie
Repositoryskills
Skill namebetter-stimulus
Stars
101
Forks
6
Bundled files
7
LicenseMIT
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 7 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by obie on GitHub. Read the source before you install it.

Installation

Install the Better Stimulus AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/obie/skills/tree/main/skills/better-stimulus
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/obie/skills.git /tmp/skills
mkdir -p .claude/skills
cp -r /tmp/skills/skills/better-stimulus .claude/skills/better-stimulus
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Better Stimulus in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Better Stimulus on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Better Stimulus is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

Better Stimulus

Apply opinionated best practices from betterstimulus.com when writing or refactoring Stimulus controllers. These patterns emphasize code reusability, proper separation of concerns, and SOLID design principles.

When to Use This Skill

Invoke this skill when:

  • Writing new Stimulus controllers
  • Refactoring existing Stimulus code
  • Reviewing Stimulus controller architecture
  • Debugging inter-controller communication
  • Integrating third-party JavaScript libraries
  • Implementing form submission logic
  • Managing controller state
  • Setting up Turbo integration

Core Principles

1. Make Controllers Configurable

Externalize hardcoded values into data attributes rather than embedding them in controller logic.

Bad:

javascript
toggle() {
  this.element.classList.toggle("active")
}

Good:

javascript
static classes = ["active"]
toggle() {
  this.element.classList.toggle(this.activeClass)
}
html
<div data-controller="toggle" data-toggle-active-class="active"></div>

2. Use Values API for State

Store controller state in Stimulus values, not instance properties, to leverage reactivity and DOM persistence.

Bad:

javascript
connect() {
  this.count = 0
}

Good:

javascript
static values = { count: Number }
countValueChanged(count) {
  this.updateDisplay()
}

3. Keep Controllers Focused (Single Responsibility)

Each controller should have one reason to change. Split controllers that mix concerns.

Ask: "What would cause this controller to change?" If multiple unrelated reasons, split it.

4. Don't Overuse connect()

Use connect() for:

  • Instantiating third-party plugins (Swiper, Chart.js, etc.)
  • Feature detection/browser capabilities

Don't use connect() for:

  • Setting up state (use Values API)
  • Adding event listeners (use data-action)

5. Register Events Declaratively

Use data-action attributes instead of addEventListener() to let Stimulus manage lifecycle.

Bad:

javascript
connect() {
  document.addEventListener("click", this.handler.bind(this))
}

Good:

html
<div data-action="click@document->controller#handler"></div>

Key Patterns

Architecture

  • Configurable Controllers: Inject dependencies via data attributes
  • Application Controller: Base class for shared functionality
  • Mixins: Share behavior via "acts as" relationships
  • Targetless Controllers: Separate element vs. target manipulation
  • Namespaced Attributes: Handle arbitrary parameter sets

See: references/architecture.md

State Management

  • Use Values API for nearly all state
  • Leverage change callbacks ([name]ValueChanged)
  • Keep values serializable
  • Provide sensible defaults

See: references/state-management.md

Lifecycle

  • Use connect() for third-party library initialization
  • Pair connect() with disconnect() for cleanup
  • Avoid overloading connect() with state setup
  • Implement teardown() for Turbo-specific cleanup

See: references/lifecycle.md

Controller Communication

Three approaches:

  1. Custom Events: Loose coupling, broadcast pattern
  2. Outlets: Direct controller references, structured layouts
  3. Callbacks: Request state from other controllers

Choose based on relationship:

  • Unknown receivers → Custom events
  • Known hierarchy → Outlets
  • Data sharing → Callbacks

See: references/events-and-interaction.md

SOLID Principles

  • Single Responsibility: One reason to change
  • Open-Closed: Extend via inheritance, not modification
  • Dependency Inversion: Depend on abstractions, inject via config

See: references/solid-principles.md

DOM & Turbo

  • Use <template> to restore DOM state
  • Use requestSubmit() not submit() for forms
  • Implement global teardown for Turbo caching
  • Handle Turbo events declaratively

See: references/dom-and-turbo.md

Error Handling

  • Create ApplicationController with handleError() method
  • Integrate with error tracking (Sentry, Honeybadger)
  • Provide user-friendly messages
  • Use try-catch for async operations

See: references/error-handling.md

Quick Reference

Value Types

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

Event Actions

html
<!-- Element events -->
<div data-action="click->controller#method">

<!-- Global events -->
<div data-action="resize@window->controller#layout">
<div data-action="keydown@document->controller#handleKey">

<!-- Multiple actions -->
<div data-action="click->ctrl1#method1 click->ctrl2#method2">

Custom Events

javascript
// Dispatch
const event = new CustomEvent('name:action', {
  bubbles: true,
  detail: { key: 'value' }
})
this.element.dispatchEvent(event)

// Listen
data-action="name:action->controller#handler"

Outlets

html
<div data-controller="parent"
     data-parent-child-outlet=".child">
  <div class="child" data-controller="child"></div>
</div>
javascript
static outlets = ['child']
this.childOutlets.forEach(outlet => outlet.method())

Lifecycle Hooks

javascript
connect()           // Element connected to DOM
disconnect()        // Element removed from DOM
[name]TargetConnected(element)      // Target added
[name]TargetDisconnected(element)   // Target removed
[name]ValueChanged(value, oldValue) // Value changed
[name]OutletConnected(outlet)       // Outlet connected
[name]OutletDisconnected(outlet)    // Outlet disconnected

Implementation Workflow

When writing a new controller:

  1. Identify responsibility - What single purpose does this serve?
  2. Choose state approach - Use Values API unless non-serializable
  3. Declare static properties - values, targets, classes, outlets
  4. Implement change callbacks - React to value changes
  5. Keep connect() minimal - Only for third-party setup
  6. Use declarative actions - Avoid addEventListener
  7. Handle errors gracefully - Wrap risky operations in try-catch
  8. Test lifecycle - Verify connect/disconnect behavior

When refactoring:

  1. Check Single Responsibility - Split if multiple concerns
  2. Extract configuration - Move hardcoded values to data attributes
  3. Convert to Values API - Replace instance properties with values
  4. Simplify connect() - Move state and listeners out
  5. Use inheritance/mixins - Share common behavior properly
  6. Decouple controllers - Use events/outlets for communication
  7. Add error handling - Implement handleError from ApplicationController

Common Mistakes to Avoid

  • ❌ Hardcoding CSS classes, selectors, or IDs in controllers
  • ❌ Using instance properties for state instead of values
  • ❌ Overloading connect() with state setup and event listeners
  • ❌ Creating "page controllers" that handle multiple concerns
  • ❌ Using addEventListener() without proper cleanup
  • ❌ Calling .bind() separately in connect and disconnect
  • ❌ Using submit() instead of requestSubmit()
  • ❌ Modifying base classes instead of extending them
  • ❌ Tight coupling between controllers
  • ❌ Swallowing errors without logging or reporting

Resources

All patterns in this skill come from betterstimulus.com, an opinionated collection of StimulusJS best practices.

For detailed explanations and examples, see:

  • references/architecture.md - Controller design patterns
  • references/state-management.md - Values API usage
  • references/lifecycle.md - Lifecycle best practices
  • references/events-and-interaction.md - Communication patterns
  • references/solid-principles.md - SOLID design principles
  • references/dom-and-turbo.md - DOM manipulation and Turbo
  • references/error-handling.md - Error management

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Better Stimulus AI skill do?

Apply Better Stimulus best practices for writing maintainable, reusable StimulusJS controllers following SOLID principles

Why use Better Stimulus on TypingMind?

Because you install it once and use it with any model. Better Stimulus is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Better Stimulus in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/obie/skills/tree/main/skills/better-stimulus. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Better Stimulus?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Better Stimulus?

As many as you like. As long as a model supports skills, you can use Better Stimulus with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Better Stimulus AI skill free?

Yes. It is published on GitHub by obie under the MIT license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇