|
| 1 | +## Stimulus Core Philosophy |
| 2 | +- Make Stimulus Controllers using the `artisan make:stimulus {name}` command |
| 3 | +- Make Stimulus Bridge Controllers using the `artisan make:stimulus --bridge {name}` command |
| 4 | +- Stimulus enhances static or server-rendered HTML with JavaScript controllers |
| 5 | +- Store state in HTML data attributes, not JavaScript objects |
| 6 | +- Design for progressive enhancement - work without JavaScript, enhance with it |
| 7 | +- Build small, reusable controllers that connect to DOM elements |
| 8 | + |
| 9 | +### Best Practices |
| 10 | +- One responsibility per controller |
| 11 | +- Design for multiple instances on same page |
| 12 | +- Always cleanup in `disconnect()` |
| 13 | +- Use semantic HTML first, enhance with Stimulus |
| 14 | + |
| 15 | +#### Error Handling |
| 16 | +```javascript |
| 17 | +// Global |
| 18 | +Stimulus.handleError = (error, message, detail) => { |
| 19 | + console.warn(message, detail) |
| 20 | + // Send to error tracking |
| 21 | +} |
| 22 | + |
| 23 | +// Controller method |
| 24 | +async fetchData() { |
| 25 | + try { |
| 26 | + // async operation |
| 27 | + } catch (error) { |
| 28 | + this.showError(error.message) |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +#### Default Events by Element |
| 34 | +- `a`, `button`: `click` |
| 35 | +- `form`: `submit` |
| 36 | +- `input`, `textarea`: `input` |
| 37 | +- `select`: `change` |
| 38 | +- `details`: `toggle` |
| 39 | + |
| 40 | +### Controller Structure |
| 41 | + |
| 42 | +#### Basic Controller Template |
| 43 | + |
| 44 | +@verbatim |
| 45 | + ```javascript |
| 46 | + // resources/js/controllers/example_controller.js |
| 47 | + import { Controller } from "@hotwired/stimulus" |
| 48 | + |
| 49 | + export default class extends Controller { |
| 50 | + static targets = [ "targetName" ] |
| 51 | + static values = { propertyName: Type } |
| 52 | + static classes = [ "className" ] |
| 53 | + |
| 54 | + connect() { /* When connected to DOM */ } |
| 55 | + disconnect() { /* When disconnected - cleanup here */ } |
| 56 | + |
| 57 | + actionMethod() { /* Handle events */ } |
| 58 | + propertyNameValueChanged() { /* When value changes */ } |
| 59 | + } |
| 60 | + ``` |
| 61 | +@endverbatim |
| 62 | + |
| 63 | +### Naming Conventions |
| 64 | +- **Files**: `hello_controller.js` → identifier `hello` |
| 65 | +- **Nested**: `admin/users_controller.js` → identifier `admin--users` |
| 66 | +- **Underscores**: become dashes in identifiers |
| 67 | + |
| 68 | +### HTML Data Attributes |
| 69 | + |
| 70 | +#### Controller Binding |
| 71 | + |
| 72 | +@verbatim |
| 73 | + ```html |
| 74 | + <div data-controller="slideshow clipboard"> |
| 75 | + <!-- Multiple controllers on one element --> |
| 76 | + </div> |
| 77 | + ``` |
| 78 | +@endverbatim |
| 79 | + |
| 80 | +#### Actions (Event Handling) |
| 81 | + |
| 82 | +@verbatim |
| 83 | + ```html |
| 84 | + <!-- Full syntax --> |
| 85 | + <button data-action="click->slideshow#next">Next</button> |
| 86 | + |
| 87 | + <!-- Default events (click for buttons) --> |
| 88 | + <button data-action="slideshow#next">Next</button> |
| 89 | + |
| 90 | + <!-- Multiple actions --> |
| 91 | + <input data-action="focus->form#highlight blur->form#reset"> |
| 92 | + ``` |
| 93 | +@endverbatim |
| 94 | + |
| 95 | +#### Targets |
| 96 | + |
| 97 | +@verbatim |
| 98 | + ```html |
| 99 | + <!-- HTML --> |
| 100 | + <input data-slideshow-target="slide"> |
| 101 | + |
| 102 | + <!-- Controller --> |
| 103 | + static targets = [ "slide" ] |
| 104 | + // Creates: this.slideTarget, this.slideTargets, this.hasSlideTarget |
| 105 | + ``` |
| 106 | +@endverbatim |
| 107 | + |
| 108 | +#### Values (State Management) |
| 109 | + |
| 110 | +@verbatim |
| 111 | + ```html |
| 112 | + <!-- HTML --> |
| 113 | + <div data-slideshow-index-value="1" data-slideshow-autoplay-value="true"> |
| 114 | + |
| 115 | + <!-- Controller --> |
| 116 | + static values = { |
| 117 | + index: Number, |
| 118 | + autoplay: Boolean, |
| 119 | + delay: { type: Number, default: 5000 } |
| 120 | + } |
| 121 | + // Creates: this.indexValue, this.autoplayValue, etc. |
| 122 | + ``` |
| 123 | +@endverbatim |
| 124 | + |
| 125 | +### Common Patterns |
| 126 | + |
| 127 | +#### Lifecycle Management |
| 128 | + |
| 129 | +@verbatim |
| 130 | + ```javascript |
| 131 | + connect() { |
| 132 | + this.startTimer() |
| 133 | + } |
| 134 | + |
| 135 | + disconnect() { |
| 136 | + this.stopTimer() // Always cleanup external resources |
| 137 | + } |
| 138 | + |
| 139 | + startTimer() { |
| 140 | + this.timer = setInterval(() => this.refresh(), 1000) |
| 141 | + } |
| 142 | + |
| 143 | + stopTimer() { |
| 144 | + if (this.timer) { |
| 145 | + clearInterval(this.timer) |
| 146 | + } |
| 147 | + } |
| 148 | + ``` |
| 149 | +@endverbatim |
| 150 | + |
| 151 | +#### Progressive Enhancement |
| 152 | + |
| 153 | +@verbatim |
| 154 | + ```javascript |
| 155 | + static classes = [ "supported" ] |
| 156 | + |
| 157 | + connect() { |
| 158 | + if ("clipboard" in navigator) { |
| 159 | + this.element.classList.add(this.supportedClass) |
| 160 | + } |
| 161 | + } |
| 162 | + ``` |
| 163 | +@endverbatim |
| 164 | + |
| 165 | +#### Value Change Callbacks |
| 166 | + |
| 167 | +@verbatim |
| 168 | + ```javascript |
| 169 | + static values = { index: Number } |
| 170 | + |
| 171 | + indexValueChanged() { |
| 172 | + // Called on initialization and value changes |
| 173 | + this.showCurrentSlide() |
| 174 | + } |
| 175 | + ``` |
| 176 | +@endverbatim |
| 177 | + |
| 178 | +#### Action Parameters |
| 179 | + |
| 180 | +@verbatim |
| 181 | + ```html |
| 182 | + <a data-loader-url-param="/messages" data-action="loader#load">Load</a> |
| 183 | + ``` |
| 184 | + |
| 185 | + ```javascript |
| 186 | + load({ params: { url } }) { |
| 187 | + fetch(url).then(/* handle response */) |
| 188 | + } |
| 189 | + ``` |
| 190 | +@endverbatim |
| 191 | + |
| 192 | +### Manual Registration |
| 193 | + |
| 194 | +@verbatim |
| 195 | + ```javascript |
| 196 | + import { Application } from "@hotwired/stimulus" |
| 197 | + import HelloController from "./controllers/hello_controller" |
| 198 | + |
| 199 | + window.Stimulus = Application.start() |
| 200 | + Stimulus.register("hello", HelloController) |
| 201 | + ``` |
| 202 | +@endverbatim |
0 commit comments