In a Stimulus controller, what do static classes and properties such as this.warningClass give you over hard-coding CSS class names in JavaScript?
answer
- logical name, real classes in HTML
- data-countdown-warning-class
- singular: first class only
- plural: array for classList.add(...)
- singular throws when the attribute is missing
basics
~10 sstatic classes names logical styles; the view supplies the real CSS classes in data-<identifier>-<name>-class on the controller element, and the controller reads them as this.warningClass, this.warningClasses or hasWarningClass, so styling stays in the template.
solid answer
~30 sWith `static classes = ["warning"]`, the countdown controller refers to a logical name while the view decides the actual classes: `data-countdown-warning-class="text-red-700 font-bold"` on the controller's element. Stimulus generates `this.warningClass` (the first class in the attribute, throwing if the attribute is missing), `this.warningClasses` (an array of all of them, empty when missing) and `this.hasWarningClass`. The controller then calls `this.element.classList.add(...this.warningClasses)`. The point is separation: the same controller works with Tailwind utilities on one page and a BEM class on another, designers change styling in ERB without touching JavaScript, and class names are not buried in controller code.
code
erb · 6 lines<%= tag.div data: { controller: "countdown",
countdown_seconds_value: @attempt.seconds_left,
countdown_warning_class: "text-red-700 font-bold",
countdown_expired_class: "opacity-50" } do %>
<span data-countdown-target="display"></span>
<% end %>go deeper
Recall static classes, the data-identifier-name-class attribute on the controller element and the three generated properties.
Explain singular versus plural behaviour, which one throws, and how to apply multi-class values with classList.
Keep styling decisions in views so one controller serves several designs, and keep the state those styles represent in values or server HTML.
Set the convention that controllers carry behaviour only, with data, URLs and styling all supplied by the server-rendered markup.
## The problem static classes solves A controller that changes appearance has to name CSS classes somewhere. Writing `this.element.classList.add("text-red-700")` inside the countdown controller ties behaviour to one stylesheet: another screen with different styling needs a different controller, and anyone restyling the warning has to edit JavaScript. **Stimulus CSS classes** move those names into the HTML and give the controller **logical names** instead. ## Declaring and supplying classes ```javascript static classes = ["warning", "expired"] ``` The view then supplies the real classes on the controller's element: ```html <div data-controller="countdown" data-countdown-warning-class="text-red-700 font-bold" data-countdown-expired-class="opacity-50"> ``` - The attribute pattern is `data-<identifier>-<logical-name>-class`, with camelCase names written in kebab-case (`noTime` becomes `data-countdown-no-time-class`). - The attribute must be on the element that carries `data-controller`; Stimulus reads it from there only. - The value may hold one class or several, separated by spaces. ## The generated properties | property | returns | when the attribute is missing | |---|---|---| | `this.warningClass` | the **first** class in the attribute | throws `Missing attribute "data-countdown-warning-class"` | | `this.warningClasses` | an array of every class | an empty array | | `this.hasWarningClass` | whether the attribute is present | `false` | Two details trip people up: 1. **The singular property returns one class.** With `"text-red-700 font-bold"`, `this.warningClass` is just `text-red-700`, so `classList.add(this.warningClass)` drops `font-bold`. Use `classList.add(...this.warningClasses)` whenever the view may list several. 2. **Only the singular property throws.** The Stimulus reference says accessing a class property without its attribute throws; in the 3.2.2 source only the singular getter does, while the plural one quietly returns `[]`. Guard optional styles with `hasWarningClass`. ## Using them in the countdown The countdown's value callback toggles the warning style once fewer than five minutes remain: ```javascript secondsValueChanged(seconds) { const warn = seconds <= this.warnAtValue this.warningClasses.forEach((c) => this.element.classList.toggle(c, warn)) } ``` Nothing in the controller says red or bold. A practice-mode page can pass `data-countdown-warning-class="bg-amber-100"` and reuse the same file. ## What it is not - **It is not a style applicator.** Declaring `static classes` changes nothing on screen; the controller still decides when to add or remove the classes. - **It is not state.** The classes describe how a state looks. Whether the timer is in its warning phase belongs in a value, or in the DOM the server rendered, so a cache restore or a morph shows the same result. - **It does not create CSS.** The classes must exist in your stylesheet like any other class used in a view. ## The Rails side Because the class names live in the view, the decision can move with the view: - a partial for the exam header can pass `countdown_warning_class: "text-red-700 font-bold"`, while the practice-mode partial passes `"text-amber-700"` to the same controller; - a helper or view component can compute the classes from the attempt (strict exams warn earlier and louder) without the JavaScript knowing that rule exists; - a design change touches ERB only, so it never needs a JavaScript rebuild or a controller review. ## Classes, values and inline styles | need | use | |---|---| | which CSS classes represent a state | `static classes` | | the data the controller works from | `static values` | | a one-off computed dimension, such as a progress bar width | an inline style set by the controller | Inline styles are fine for genuinely computed numbers; anything a designer would want to change belongs in a class attribute. ## Why interviewers ask The question tests whether a candidate treats Stimulus controllers as reusable behaviour attached to server-rendered HTML. Hard-coded class names are a small smell, but they are the same smell as hard-coded URLs and copy: configuration that belongs to the view. Values carry the data, classes carry the styling, and the controller carries only behaviour.
- A designer adds a second class to data-countdown-warning-class and it never appears. Why?The controller uses the singular `this.warningClass`, which returns only the first class in the attribute. Switch to `classList.add(...this.warningClasses)` or toggle each entry of the plural array.
- How should a controller handle a style that only some pages define?Check `this.hasExpiredClass` before using it, or use the plural property, which returns an empty array when the attribute is missing. The singular property throws a missing-attribute error in that case.
- this.warningClass returns every class listed in the attribute
- Class attributes can be placed on targets inside the controller
- Declaring static classes adds those classes to the element automatically
- this.warningClasses throws when the attribute is missing