Support Array<string | number | boolean | null | undefined>
in classList
#2036
samuelgozi
started this conversation in
Ideas
Replies: 1 comment
-
I want ahead and created a PR for this: |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The problem
Many times I find myself needing to add multiple classes to an element nad I want all of them to be on.
In that case I have two options:
Using the regular
class
syntax:Using the
classList
syntax:In this case its seems obvious that using the regular class syntax is cleaner and takes less space. But working with dynamically named classes, lets say for example when working with css modules, then both options become ugly.
Using the regular
class
syntax:Using the
classList
syntax:In this scenario both options feel bad. The
class
syntax is hard to type, and you need to "manage" spaces, while theclassList
syntax feels redundant.The Proposed solution
I think that adding support for arrays
Array<string | boolean | null | undefined>
toclassList
can help in this case as well as in other cases. Such syntax exist in Vue for a while, and svelte handles this case by allowing multipleclass
attributes on an element.In the following examples strings, or values that coerce to false are not added to the element.
Here are a few examples that show the strengths of this syntax:
Multiple dynamic classes
Class A or Class B, if A wasn't set.
Toggle class conditionally. This is an alternative to the object syntax that shines when combined with other classes.
This syntax even feels nice when working with multiple string classes:
Beta Was this translation helpful? Give feedback.
All reactions