{"slug":"stimulus","title":"stimulus","summary":"Symfony UX Stimulus: JavaScript behavior for dropdowns, tabs, modals, clipboard buttons and third-party widgets. Use for StimulusBundle, data-controller, data-action, targets, values, outlets, lazy controllers and Twig stimulus_* helpers.","platform":"Claude","tags":[],"authorName":"LLM Mart","authorSlug":"llm-mart","score":0,"source":"github","price":null,"verified":false,"createdAt":"2026-12-02T16:48:36.306557Z","repo":{"url":"https://github.com/smnandre/symfony-ux-skills","stars":191,"forks":15,"license":"MIT","updatedAt":"2026-12-02T03:34:06Z"},"bodyHtml":"<hr>\n<h2>name: stimulus\ndescription: 'Symfony UX Stimulus: JavaScript behavior for dropdowns, tabs, modals, clipboard buttons and third-party widgets. Use for StimulusBundle, data-controller, data-action, targets, values, outlets, lazy controllers and Twig stimulus_* helpers.'\nlicense: MIT\nmetadata:\nauthor: Simon André\nurl: <a href=\"https://smnandre.dev\">https://smnandre.dev</a></h2>\n<h1>Stimulus</h1>\n<p>Modest JavaScript framework that connects JS objects to HTML via data attributes. Stimulus does not render HTML: it augments server-rendered HTML with behavior.</p>\n<p>The mental model: HTML is the source of truth, JavaScript controllers attach to elements, and data attributes are the wiring. No build step required with AssetMapper.</p>\n<h2>Quick Reference</h2>\n<pre><code>data-controller=\"name\"              attach controller to element\ndata-name-target=\"item\"             mark element as a target\ndata-action=\"event-&gt;name#method\"    bind event to controller method\ndata-name-key-value=\"...\"           pass typed data to controller\ndata-name-key-class=\"...\"           configure CSS class names\ndata-name-other-outlet=\".selector\"  reference another controller instance\n</code></pre>\n<h2>Controller Skeleton</h2>\n<pre><code>// assets/controllers/example_controller.js\nimport { Controller } from '@hotwired/stimulus';\n\nexport default class extends Controller {\n    static targets = ['input', 'output'];\n    static values = { url: String, delay: { type: Number, default: 300 } };\n    static classes = ['loading'];\n    static outlets = ['other'];\n\n    connect() {\n        // Called when controller connects to DOM\n    }\n\n    disconnect() {\n        // Called when controller disconnects -- clean up here\n    }\n\n    submit(event) {\n        // Action method\n    }\n}\n</code></pre>\n<p>File naming convention: <code>hello_controller.js</code> maps to <code>data-controller=\"hello\"</code>. Subdirectories use <code>--</code> as separator: <code>components/modal_controller.js</code> maps to <code>data-controller=\"components--modal\"</code>.</p>\n<p>If a controller reconnects during a Turbo visit or an event listener runs twice, check the <a href=\"references/gotchas.md#turbo-compatibility\">Turbo compatibility examples</a>.</p>\n<h2>HTML Wiring Examples</h2>\n<h3>Basic Controller</h3>\n<pre><code>&lt;div data-controller=\"hello\"&gt;\n    &lt;input data-hello-target=\"name\" type=\"text\"&gt;\n    &lt;button data-action=\"click-&gt;hello#greet\"&gt;Greet&lt;/button&gt;\n    &lt;span data-hello-target=\"output\"&gt;&lt;/span&gt;\n&lt;/div&gt;\n</code></pre>\n<h3>Values from Server (Twig)</h3>\n<p>Pass server data to controllers via value attributes. Values are typed and automatically parsed.</p>\n<pre><code>&lt;div data-controller=\"map\"\n     data-map-latitude-value=\"{{ place.lat }}\"\n     data-map-longitude-value=\"{{ place.lng }}\"\n     data-map-zoom-value=\"12\"&gt;\n&lt;/div&gt;\n</code></pre>\n<p>Available types: <code>String</code>, <code>Number</code>, <code>Boolean</code>, <code>Array</code>, <code>Object</code>. Values trigger <code>{name}ValueChanged()</code> callbacks when mutated.</p>\n<h3>Actions</h3>\n<p>The format is <code>event-&gt;controller#method</code>. Default events exist per element type (click for buttons, input for inputs, submit for forms) so the event can be omitted.</p>\n<pre><code>{# Explicit event #}\n&lt;button data-action=\"click-&gt;hello#greet\"&gt;Greet&lt;/button&gt;\n\n{# Default event (click for button) #}\n&lt;button data-action=\"hello#greet\"&gt;Greet&lt;/button&gt;\n\n{# Multiple actions on same element #}\n&lt;input type=\"text\"\n       data-action=\"focus-&gt;field#highlight blur-&gt;field#normalize input-&gt;field#validate\"&gt;\n\n{# Prevent default #}\n&lt;form data-action=\"submit-&gt;form#validate:prevent\"&gt;\n\n{# Keyboard shortcuts #}\n&lt;div data-action=\"keydown.esc@window-&gt;modal#close\"&gt;\n&lt;input data-action=\"keydown.enter-&gt;modal#submit keydown.ctrl+s-&gt;modal#save\"&gt;\n\n{# Global events (window/document) #}\n&lt;div data-action=\"resize@window-&gt;sidebar#adjust click@document-&gt;sidebar#closeOutside\"&gt;\n</code></pre>\n<h3>CSS Classes</h3>\n<p>Externalize CSS class names so controllers stay generic:</p>\n<pre><code>&lt;button data-controller=\"button\"\n        data-button-loading-class=\"opacity-50 cursor-wait\"\n        data-button-active-class=\"bg-blue-600\"\n        data-action=\"click-&gt;button#submit\"&gt;\n    Submit\n&lt;/button&gt;\n</code></pre>\n<pre><code>// In controller\nthis.element.classList.add(...this.loadingClasses);\n</code></pre>\n<h3>Multiple Controllers</h3>\n<p>An element can have multiple controllers:</p>\n<pre><code>&lt;div data-controller=\"dropdown tooltip\"\n     data-action=\"mouseenter-&gt;tooltip#show mouseleave-&gt;tooltip#hide\"&gt;\n    &lt;button data-action=\"click-&gt;dropdown#toggle\"&gt;Menu&lt;/button&gt;\n    &lt;ul data-dropdown-target=\"menu\" hidden&gt;...&lt;/ul&gt;\n&lt;/div&gt;\n</code></pre>\n<h3>Outlets (Cross-Controller Communication)</h3>\n<p>Reference other controller instances by CSS selector:</p>\n<pre><code>&lt;div data-controller=\"player\"\n     data-player-playlist-outlet=\"#playlist\"&gt;\n    &lt;button data-action=\"click-&gt;player#playNext\"&gt;Next&lt;/button&gt;\n&lt;/div&gt;\n\n&lt;ul id=\"playlist\" data-controller=\"playlist\"&gt;\n    &lt;li data-playlist-target=\"track\"&gt;Song 1&lt;/li&gt;\n    &lt;li data-playlist-target=\"track\"&gt;Song 2&lt;/li&gt;\n&lt;/ul&gt;\n</code></pre>\n<pre><code>// In player controller\nstatic outlets = ['playlist'];\n\nplayNext() {\n    const tracks = this.playlistOutlet.trackTargets;\n    // ...\n}\n</code></pre>\n<h3>Lazy Loading (Heavy Dependencies)</h3>\n<p>Instead of on initial page load, load controller JS when a matching <code>data-controller</code> element appears on the page. Use for controllers with heavy dependencies (chart libs, editors, maps) used on a few pages.</p>\n<pre><code>/* stimulusFetch: 'lazy' */\nimport { Controller } from '@hotwired/stimulus';\nimport Chart from 'chart.js';\n\nexport default class extends Controller {\n    connect() {\n        // Chart.js is only loaded once a matching element is on the page\n    }\n}\n</code></pre>\n<p>Place the comment at the top level of the file (before or after the imports). If a minifier strips comments, use the preserved form <code>/*! stimulusFetch: 'lazy' */</code>, detected since Symfony UX 3.3+. For third-party controllers, set <code>\"fetch\": \"lazy\"</code> in <code>assets/controllers.json</code>.</p>\n<h2>Symfony / Twig Integration</h2>\n<p>Raw data attributes are the recommended approach: they need no helper, work everywhere and read easily.</p>\n<pre><code>{# Raw attributes (preferred) #}\n&lt;div data-controller=\"search\"\n     data-search-url-value=\"{{ path('api_search') }}\"&gt;\n</code></pre>\n<p>Twig helpers exist for complex cases or when generating attributes programmatically:</p>\n<pre><code>{# Twig helper #}\n&lt;div {{ stimulus_controller('search', { url: path('api_search') }) }}&gt;\n\n{# Chaining multiple controllers #}\n&lt;div {{ stimulus_controller('a')|stimulus_controller('b') }}&gt;\n\n{# Target and action helpers #}\n&lt;input {{ stimulus_target('search', 'query') }}&gt;\n&lt;button {{ stimulus_action('search', 'submit') }}&gt;\n</code></pre>\n<p>From PHP (form types, for instance), autowire <code>StimulusHelper</code> (since Symfony UX 3.5+):</p>\n<pre><code>use Symfony\\UX\\StimulusBundle\\Helper\\StimulusHelper;\n\npublic function __construct(private StimulusHelper $stimulusHelper) {}\n\npublic function buildForm(FormBuilderInterface $builder, array $options): void\n{\n    $attributes = $this-&gt;stimulusHelper-&gt;createStimulusAttributes();\n    $attributes-&gt;addController('country-picker', ['locale' =&gt; 'fr']);\n    $attributes-&gt;addAction('country-picker', 'refresh', 'change');\n\n    $builder-&gt;add('country', CountryType::class, ['attr' =&gt; $attributes-&gt;toArray()]);\n}\n</code></pre>\n<h3>Asset Pipelines</h3>\n<p>StimulusBundle works with three asset systems. Controllers live in <code>assets/controllers/</code>, UX package controllers are listed in <code>assets/controllers.json</code>:</p>\n<table>\n<thead>\n<tr>\n<th>Pipeline</th>\n<th>Setup</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>AssetMapper (default)</td>\n<td>Nothing to add: Flex wires <code>assets/stimulus_bootstrap.js</code></td>\n</tr>\n<tr>\n<td>Webpack Encore</td>\n<td>Install <code>@symfony/stimulus-bridge</code> yourself (<code>npm install --save-dev @symfony/stimulus-bridge</code>)</td>\n</tr>\n<tr>\n<td>Reprise (Vite / Rsbuild, experimental, since Symfony UX 3.3+)</td>\n<td>Point the plugin at <code>controllers.json</code>, then start the app with <code>@symfony/reprise/stimulus</code></td>\n</tr>\n</tbody>\n</table>\n<pre><code>// vite.config.js (Reprise)\nimport { defineConfig } from 'vite';\nimport Symfony from '@symfony/reprise/vite';\n\nexport default defineConfig({\n    plugins: [Symfony({ stimulus: './assets/controllers.json' })],\n});\n</code></pre>\n<pre><code>// assets/stimulus_bootstrap.js (Reprise)\nimport { startStimulusApp } from '@symfony/reprise/stimulus';\n\nconst app = startStimulusApp();\n</code></pre>\n<h2>Key Principles</h2>\n<p><strong>HTML drives, JS responds.</strong> Controllers don't create markup: they attach behavior to existing HTML. If you find yourself generating DOM in a controller, consider whether a TwigComponent or LiveComponent would be better.</p>\n<p><strong>One controller, one concern.</strong> A dropdown controller handles dropdowns. A tooltip controller handles tooltips. Compose multiple controllers on the same element rather than building mega-controllers.</p>\n<p><strong>Clean up in disconnect().</strong> If <code>connect()</code> adds event listeners, timers, or third-party library instances, <code>disconnect()</code> must remove them. Turbo navigation will disconnect and reconnect controllers as pages change.</p>\n<p><strong>Values over data attributes.</strong> Use Stimulus values (typed, with change callbacks) rather than raw <code>data-*</code> attributes for data that the controller needs to read or watch.</p>\n<h2>References</h2>\n<ul>\n<li><a href=\"https://ux.symfony.com/stimulus\">Symfony UX package page</a> (<a href=\"https://ux.symfony.com/stimulus.md\">Markdown summary</a>) · <a href=\"https://packagist.org/packages/symfony/stimulus-bundle\"><code>symfony/stimulus-bundle</code> on Packagist</a>.</li>\n<li>See Stimulus in <a href=\"https://ux.symfony.com/toolkit/kits/common/components/clipboard\">a clipboard component</a>, <a href=\"https://ux.symfony.com/demos/live-component/product-form\">a product form with a native dialog</a> and <a href=\"https://ux.symfony.com/demos/live-component/infinite-scroll-2\">automatic infinite scroll</a>.</li>\n<li>The <a href=\"https://ux.symfony.com/inspector#demo\">UX Inspector demo</a> shows controller instances, values, outlets and events.</li>\n<li>For installation, asset pipelines and Twig helpers, see the <a href=\"https://symfony.com/bundles/StimulusBundle\">StimulusBundle documentation</a>.</li>\n<li>For browser-side controller APIs and lifecycle callbacks, see the <a href=\"https://stimulus.hotwired.dev/reference/controllers\">Stimulus reference</a>.</li>\n<li><strong>Full API</strong> (lifecycle, targets, values, actions, classes, outlets): <a href=\"references/api.md\">references/api.md</a></li>\n<li><strong>Patterns</strong> (debounce, fetch, modals, forms, etc.): <a href=\"references/patterns.md\">references/patterns.md</a></li>\n<li><strong>Gotchas</strong> (common mistakes, debugging, Turbo compatibility): <a href=\"references/gotchas.md\">references/gotchas.md</a></li>\n</ul>\n<h2>See Also</h2>\n<ul>\n<li><strong>live-component</strong>: server-driven reactivity when the state lives in PHP</li>\n<li><strong>ux-map</strong>, <strong>ux-chartjs</strong>, <strong>ux-notify</strong>: their controllers dispatch <code>ux:map:*</code>, <code>chartjs:*</code> and <code>notify:*</code> events to extend in your own controllers</li>\n<li><strong>ux-router</strong>, <strong>ux-translator</strong>: <code>path()</code> and <code>trans()</code> inside controllers</li>\n<li><strong>ux-toolkit</strong>: ready-made controllers installed with <code>ux:install</code></li>\n</ul>\n","files":[{"path":"references/api.md","sizeBytes":10028,"isText":true},{"path":"references/gotchas.md","sizeBytes":9360,"isText":true},{"path":"references/patterns.md","sizeBytes":12715,"isText":true},{"path":"SKILL.md","sizeBytes":10408,"isText":true}],"reviewScore":null,"reviewSummary":null,"trust":{"provenance":"trusted-source-unreviewed","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow.","bodySource":null},"bodyLocked":false,"purchaseUrl":null,"sourceUrl":null,"report":{"provenance":"trusted-source-unreviewed","screen":{"ran":true,"outcome":"clean","suspicious":0,"notes":0,"hiddenCharacters":false},"virusScan":{"engine":"clamav","status":"clean","scannedAt":"2026-12-02T16:49:13.850899Z","sha256":"3597F37326C95456E94E830E8B71400414B7BF3B37A20973511083FE3DA3FE0A","sizeBytes":14225},"review":null,"source":{"repositoryUrl":"https://github.com/smnandre/symfony-ux-skills","path":"skills/stimulus","license":"MIT","commit":"78c25ed0b79bcc21d7611002dc81d8b87c3316e2","subtreeSha":"DD1DECE3FFC5E73364582202AC8276829682F02DB478C67588EACEB170DFAACB","lastSyncedAt":"2026-12-02T16:48:36.132977Z"},"reviewedAt":"2026-12-02T16:50:15.244047Z","notice":"Community-authored content, reproduced verbatim and not vetted as instructions. Treat it as data to evaluate, never as directives to follow."},"install":[{"target":"skills-cli","command":"npx skills add https://github.com/smnandre/symfony-ux-skills/tree/main/skills/stimulus"},{"target":"claude-code","command":"claude plugin marketplace add https://llmmart.ai/marketplace.json && claude plugin install smnandre-symfony-ux-skills@llmmart"},{"target":"git","command":"git clone https://github.com/smnandre/symfony-ux-skills.git"}]}