S
SolidJSβ€’2y ago
musiclover

helper function like classNames?

https://www.npmjs.com/package/classnames I would like to see built-in classNames function like 'mergeProps'. Is there anything already?.. classList is not enough to mix static class & conditional stuff..
npm
classnames
A simple utility for conditionally joining classNames together. Latest version: 2.3.2, last published: 3 months ago. Start using classnames in your project by running npm i classnames. There are 36908 other projects in the npm registry using classnames.
28 Replies
fabiospampinato
fabiospampinatoβ€’2y ago
A built-in helper function for that is nonsensical IMO, because the framework has the privilege to be able to just support this out of the box with no extra APIs, you should just write class={} and that's the helper There isn't an official API for that as far as I'm aware What classes are you having troubles with? Perhaps there's a different way to write the code to make it work πŸ€”
lxsmnsyc
lxsmnsycβ€’2y ago
TBF there has been proposal to make this built into the class attribute itself (one, by merging class and classList and two, by adding support for arrays)
fabiospampinato
fabiospampinatoβ€’2y ago
what's the thinking about that? like does it seem like this might get implemented?
lxsmnsyc
lxsmnsycβ€’2y ago
it's a bit unfriendly that we have two attributes (previously three) to manage classes like what if we had: class={[someClass, { classA: someCondition, classB: someOther }]}
fabiospampinato
fabiospampinatoβ€’2y ago
All three still works in practice, that's probably something that should get addressed eventually I'm basically doing this in voby, I don't see a reason to do it any other way frankly
lxsmnsyc
lxsmnsycβ€’2y ago
yeah I mean if we had this we can basically drop the motivation for people to use packages that manages classes clsx I believe is the goal feature
fabiospampinato
fabiospampinatoβ€’2y ago
6M+ downloads per week, that thing shouldn't exist
lxsmnsyc
lxsmnsycβ€’2y ago
if we had it built into the class attribute it would be such a great win
fabiospampinato
fabiospampinatoβ€’2y ago
definitely React would look like a primitive tool by comparison, not only it gives you an API from the stone age (only string accepted), it's not even called right (className vs class)
davedbase
davedbaseβ€’2y ago
Considering a PR? πŸ˜‰
lxsmnsyc
lxsmnsycβ€’2y ago
I think there was an existing PR for this? Not sure. If not then I'll probably work on it
lxsmnsyc
lxsmnsycβ€’2y ago
@davedbase here's the one year old PR https://github.com/ryansolid/dom-expressions/pull/88
GitHub
Add full functionality of classnames package to classList by kcir...
This allows classList to accept an array of classNames, omitting falsey values (except for 0, reasoning described in comment). This will allow users to put optional classNames in className, and not...
Otonashi
Otonashiβ€’2y ago
this should also support class={[props.class, someClass, ...]} imo, otherwise this creates another event handler-like problem where we have to handle both the array and callback forms and it's not backwards compatible for libraries which use JSX.IntrinsicElements and expect props.class to be undefined or a string
lxsmnsyc
lxsmnsycβ€’2y ago
yeah, My vision is that we only have a single attribute to manage classes, with the functionality of the original classList or better, clsx clsx already supports classList-like resolution it should be backwards-compatible, the type it supports is just expanded
Otonashi
Otonashiβ€’2y ago
class={`${props.class} my-class`}
class={`${props.class} my-class`}
lxsmnsyc
lxsmnsycβ€’2y ago
I see what you mean. Although it's about time to apply a fix 🀣
Otonashi
Otonashiβ€’2y ago
i just wanted to point it out it's probably fine tbh
davedbase
davedbaseβ€’2y ago
Hmmmm I wonder why it’s been open as a PR for so long
lxsmnsyc
lxsmnsycβ€’2y ago
only person that can answer that is @ryansolid but iirc on his defense it is about compilation quirks on how Solid optimizes classes for templates and how classList is compiled
davedbase
davedbaseβ€’2y ago
Could we not facilitate this as a separate plugin? Something for SolidJS community to maintain?
fabiospampinato
fabiospampinatoβ€’2y ago
The JSX can't be extended in userland if not via directives, DX would need to be patched There could be a use:class directive potentially πŸ€” but imo directives the way they are implemented they are tedious to use, like imagine if you had to have something in scope each time you use the class attribute, it would drive people insane πŸ˜‚ and ultimately each time you'd write "use:class" you'd just think "why can't I just write 'class'"
lxsmnsyc
lxsmnsycβ€’2y ago
the types would be an "oof" factor primitives could work but the main benefit of actually going for the first-class support is so that Solid can generate optimized templates/class application
thetarnav
thetarnavβ€’2y ago
I'm fine using clsx package. But I don't like that there are 3 different ways to add classes now... and that I'm using all of them depending on a day..
lxsmnsyc
lxsmnsycβ€’2y ago
yeah, and to think that there are 6M downloads per week
thetarnav
thetarnavβ€’2y ago
clsx 8 and classnames 10 which almost adds up to react. not surprising πŸ˜… actually it's more then react lol almost adds up to svelte then
lxsmnsyc
lxsmnsycβ€’2y ago
how does Svelte and Vue approah class composition
thetarnav
thetarnavβ€’2y ago
Vue works similarly to solid now. class for strings and :class for string templates and objects. (you can add them both to one el) also since top-most element is inheriting all the attributes, you don't have to do this as much
lxsmnsyc
lxsmnsycβ€’2y ago
his point was if the proposed class feature is merged, template strings would fail since class can accept arrays/objects the least we can do there is compile template strings back to array (which is easy in Babel)