classList
import {Aside} from '~/components/configurable/Aside'
Solid offers two ways to set the class of an element: class and classList attributes.
First, you can set class=... like any other attribute. For example:
1 2 3 4 5 6 7 8 |
|
Alternatively, the classList
pseudo-attribute lets you specify an object, where each key is a class and the value is treated as a boolean representing whether to include that class. For example (matching the last example):
1 2 3 |
|
This example compiles to a render effect that dynamically calls element.classList.toggle to turn each class on or off, only when the corresponding boolean changes. For example, when state.active
becomes true [false], the element gains [loses] the active
class.
The value passed into classList
can be any expression (including a signal getter) that evaluates to an appropriate object. Some examples:
1 2 3 4 5 6 7 |
|
It's also possible, but dangerous, to mix class and classList. The main safe situation is when class is set to a static string (or nothing), and classList is reactive. (class could also be set to a static computed value as in class={baseClass()}
, but then it should appear before any classList pseudo-attributes.) If both class and classList are reactive, you can get unexpected behavior: when the class value changes, Solid sets the entire class attribute, so will overwrite any toggles made by classList.
Because classList is a compile-time pseudo-attribute, it does not work in a prop spread like <div {...props} />
or in <Dynamic>
.