You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note that `className` was deprecated in Solid 1.4 in favor of {""}`class`.
37
-
:::
32
+
Object whose keys are class names and whose values control whether each class is present.
33
+
34
+
## Behavior
35
+
36
+
- Each key is treated as a class name.
37
+
- Truthy values add the class and falsy values remove it.
38
+
- Updates are applied per class rather than replacing the whole `class` attribute.
39
+
-`classList` is a compile-time pseudo-attribute and does not work through prop spreads or `<Dynamic>`.
40
+
- If both `class` and `classList` are reactive, updates to `class` can overwrite classes managed by `classList`.
38
41
39
-
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):
42
+
## Examples
43
+
44
+
### Basic usage
40
45
41
46
```tsx
42
47
<div
@@ -47,23 +52,8 @@ Alternatively, the `classList` pseudo-attribute lets you specify an object, wher
47
52
/>
48
53
```
49
54
50
-
This example compiles to a render effect that dynamically calls [element.classList.toggle](https://developer.mozilla.org/en-US/docs/Web/API/DOMTokenList/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.
51
-
52
-
The value passed into `classList` can be any expression (including a signal getter) that evaluates to an appropriate object. Some examples:
55
+
### Dynamic class name
53
56
54
57
```tsx
55
-
// Dynamic class name and value
56
58
<divclassList={{ [className()]: classOn() }} />
57
-
58
-
// Signal class list
59
-
const [classes, setClasses] =createSignal({})
60
-
setClasses((c) => ({ ...c, active: true }))
61
-
<divclassList={classes()} />
62
-
```
63
-
64
-
While possible, mixing `class` and `classList` in Solid can lead to unexpected behavior.
65
-
The safest approach is to use a static string (or nothing) for `class` and keep `classList` reactive.
66
-
You can also use a static computed value for class, such as `class={baseClass()}`, however you must make sure it comes before any `classList` pseudo-attributes.
67
-
If both `class` and `classList` are reactive, changes to `class` will overwrite the entire `class` attribute, potentially undoing any updates made by `classList`.
68
-
69
-
Because classList is a compile-time pseudo-attribute, it does not work in a prop spread like `<div {...props} />` or in `<Dynamic>`.
Unlike [React's style attribute](https://reactjs.org/docs/dom-elements.html#style), Solid uses **element.style.setProperty** under the hood. This means you need to use the lower-case, dash-separated version of property names instead of the JavaScript camel-cased version, such as `background-color` rather than `backgroundColor`. This actually leads to better performance and consistency with SSR output.
0 commit comments