better-stimulus
БесплатноБез исполняемых скриптовНе проверенApply Better Stimulus best practices for writing maintainable, reusable StimulusJS controllers following SOLID principles
от steveclarke★ 37 звёзд на GitHubОб этом скилле
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:
toggle() {
this.element.classList.toggle("active")
}
Good:
static classes = ["active"]
toggle() {
this.element.classList.toggle(this.activeClass)
}
<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:
connect() {
this.count = 0
}
Good:
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:
connect() {
document.addEventListener("click", this.handler.bind(this))
}
Good:
<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()withdisconnect()for cleanup - Avoid overloading
connect()with state setup - Implement
teardown()for Turbo-specific cleanup
See: references/lifecycle.md
Controller Communication
Three approaches:
- Custom Events: Loose coupling, broadcast pattern
- Outlets: Direct controller references, structured layouts
- 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()notsubmit()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
static values = {
url: String,
count: Number,
enabled: Boolean,
items: Array,
config: Object
}
Event Actions
<!-- 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
// Dispatch
const event = new CustomEvent('name:action', {
bubbles: true,
detail: { key: 'value' }
})
this.element.dispatchEvent(event)
// Listen
data-action="name:action->controller#handler"
Outlets
<div data-controller="parent"
data-parent-child-outlet=".child">
<div class="child" data-control
Установить better-stimulus в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
FAQ
Что делает скилл better-stimulus?
Apply Better Stimulus best practices for writing maintainable, reusable StimulusJS controllers following SOLID principles
Как установить скилл better-stimulus?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл better-stimulus запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
doc-coauthoring
Guide users through a structured workflow for co-authoring documentation. Use when user wants to write documentation, proposals, technical specs, decision…
от Anthropicacademy-guide
Stop and check this skill before finishing any reply to a question about how to use Claude or a Claude product — it recommends matching courses, tutorials…
от Anthropicinternal-comms
A set of resources to help me write all kinds of internal communications, using the formats that my company likes to use. Claude should use this skill…
от Anthropicskill-development
This skill should be used when the user wants to "create a skill", "add a skill to plugin", "write a new skill", "improve skill description", "organize skill…
от Anthropic