classList could also accept an array of strings
#1737
Replies: 9 comments 8 replies
|
You can achieve somewhat similar effect using join: <div class={['a', 'b'].join(' ')}>Text</div>
|
|
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; |
|
Totally support this. But maybe it'd be better to have another prop to this, like |
|
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. |
|
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 |
|
hi,take a look at this
|
|
Is there anything wrong with: <div classList={{"some fixed classes": true, [props.class]: true, "dynamic": props.someValue }}>
Hello world
</div>? |
|
Upvoted. Please consider the clsx syntax, it's a really nice DX. |
|
Was really confused that |
Uh oh!
There was an error while loading. Please reload this page.
Currently
classListattribute accepts an object with class names as keys andbooleanas 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 valuestruewithclassList:It would be handy if
classListwould also accept a simple array of strings. It would allow us to write this:All reactions