classList
could also accept an array of strings
#1737
Replies: 6 comments 6 replies
-
You can achieve somewhat similar effect using join: <div class={['a', 'b'].join(' ')}>Text</div>
|
Beta Was this translation helpful? Give feedback.
-
You could use a simple tool to move such an array into the object required to classList: const cl = <
ClassNames extends ReadonlyArray<string | false | null | undefined>,
ClassList = { [key in ClassNames[number] & string]: true }
>(classNames: ClassNames) => classNames.reduce((obj, className) => {
if (typeof className == "string") {
obj[className] = true;
}
return obj;
}, {} as Record<string, true>) as ClassList; |
Beta Was this translation helpful? Give feedback.
-
Totally support this. But maybe it'd be better to have another prop to this, like |
Beta Was this translation helpful? Give feedback.
-
IMO I'd expect
Nobody does this, because it's not convenient. Instead you use clsx or similar tool. It would be so awesome if classList worked as it named. |
Beta Was this translation helpful? Give feedback.
-
Personally I think classList is a benefit and very valuable utility in Solid when used correctly. From reading the above a lot of people seem to have trouble writing it out. I've found the following pattern works well for me: return (
<div
class="base"
classList={{ [props.class as string]: !!props.class }}
>
Hello world
</div>
); It's clean and concise. Can anyone think of downsides to it? Also, I do think that it might be worth considering return (
<div classList={[ "base", props.class ]}>
Hello world
</div>
);
// Possibly also an array with an object as well?
return (
<div classList={[
"base",
{ "active": active() && !loading }
]}>
Hello world
</div>
); Leaning on Solid's transformers means not having to ship out all of |
Beta Was this translation helpful? Give feedback.
-
hi,take a look at this
|
Beta Was this translation helpful? Give feedback.
-
Currently
classList
attribute accepts an object with class names as keys andboolean
as values. This is very convenient, and it works perfectly.However, there are cases where I just need to pass an array of class names to the element. Currently, there are two ways to to this: (1) concatenate strings with
class
; and (2) create an object with all valuestrue
withclassList
:It would be handy if
classList
would also accept a simple array of strings. It would allow us to write this:Beta Was this translation helpful? Give feedback.
All reactions