Skip to content

Commit a15aad9

Browse files
authored
Improve awkward styling and sentences in classList.mdx (#1185)
1 parent 382de57 commit a15aad9

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/routes/reference/jsx-attributes/classlist.mdx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ title: classList
33
order: 1
44
---
55

6-
Solid offers two ways to set the class of an element: class and classList attributes.
6+
Solid offers two attributes to set the class of an element: `class` and `classList`.
77

8-
First, you can set class=... like any other attribute. For example:
8+
First, `class` can be set like other attributes. For example:
99

1010
```tsx
1111
// Two static classes
@@ -27,7 +27,10 @@ Alternatively, the `classList` pseudo-attribute lets you specify an object, wher
2727

2828
```tsx
2929
<div
30-
classList={{ active: state.active, editing: state.currentId === row.id }}
30+
classList={{
31+
active: state.active,
32+
editing: state.currentId === row.id,
33+
}}
3134
/>
3235
```
3336

@@ -45,6 +48,9 @@ setClasses((c) => ({ ...c, active: true }))
4548
<div classList={classes()} />
4649
```
4750

48-
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.
51+
While possible, mixing `class` and `classList` in Solid can lead to unexpected behavior.
52+
The safest approach is to use a static string (or nothing) for `class` and keep `classList` reactive.
53+
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.
54+
If both `class` and `classList` are reactive, changes to `class` will overwrite the entire `class` attribute, potentially undoing any updates made by `classList`.
4955

5056
Because classList is a compile-time pseudo-attribute, it does not work in a prop spread like `<div {...props} />` or in `<Dynamic>`.

0 commit comments

Comments
 (0)