# Stimulus API Reference ## Table of Contents - [Lifecycle Callbacks](#lifecycle-callbacks) - [Targets](#targets) - [Values](#values) - [Actions](#actions) - [CSS Classes](#css-classes) - [Outlets](#outlets) - [Controller Properties](#controller-properties) --- ## Lifecycle Callbacks ```javascript export default class extends Controller { initialize() { // Called once when controller is first instantiated // Use for one-time setup that doesn't require DOM } connect() { // Called each time controller connects to DOM // Element is available, targets are accessible // May be called multiple times (Turbo navigation) } disconnect() { // Called each time controller disconnects from DOM // Clean up: remove listeners, cancel timers, abort fetches } } ``` Connection happens when: - Element with `data-controller` is in document - Controller identifier matches Disconnection happens when: - Element removed from DOM - `data-controller` attribute removed/changed - Turbo replaces page body --- ## Targets ### Definition ```javascript static targets = ['query', 'results', 'errorMessage'] ``` ### Generated Properties | Target name | Property | Returns | |-------------|----------|---------| | `item` | `this.itemTarget` | First element (throws if none) | | `item` | `this.itemTargets` | Array of all elements | | `item` | `this.hasItemTarget` | Boolean | ### HTML ```html
``` ### Target Callbacks ```javascript static targets = ['item'] itemTargetConnected(element) { // Called when a target element is added to DOM // Fires before connect() on initial load } itemTargetDisconnected(element) { // Called when a target element is removed from DOM // Fires before disconnect() } ``` ### Shared Targets Element can be target for multiple controllers: ```html ``` ### Naming Use camelCase in JS, kebab-case in HTML: ```javascript static targets = ['errorMessage'] ``` ```html ``` --- ## Values ### Definition ```javascript static values = { url: String, // required, no default count: Number, enabled: Boolean, items: Array, config: Object, delay: { type: Number, default: 300 }, // with default query: { type: String, default: '' } } ``` ### Types and Defaults | Type | Empty default | HTML encoding | |------|---------------|---------------| | `String` | `""` | As-is | | `Number` | `0` | Numeric string | | `Boolean` | `false` | `"true"` or `"false"` | | `Array` | `[]` | JSON | | `Object` | `{}` | JSON | ### Generated Properties | Value name | Property | Description | |------------|----------|-------------| | `url` | `this.urlValue` | Get/set value | | `url` | `this.hasUrlValue` | Boolean, true if attribute exists | ### HTML ```html
``` ### Value Change Callbacks ```javascript static values = { count: Number } countValueChanged(value, previousValue) { // Called when data-*-count-value changes // Also called on connect() with previousValue = undefined console.log(`Changed from ${previousValue} to ${value}`); } ``` ### Setter Behavior ```javascript this.countValue = 10; // Updates DOM attribute this.countValue = undefined; // Removes attribute, reverts to default ``` --- ## Actions ### Syntax ``` event->controller#method event->controller#method:option ``` ### HTML Examples ```html