#helper function like classNames?

47 messages · Page 1 of 1 (latest)

uncut galleon
#

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..

cobalt mica
#

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 🤔

obsidian iris
#

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)

cobalt mica
obsidian iris
#

like what if we had:
class={[someClass, { classA: someCondition, classB: someOther }]}

cobalt mica
#

All three still works in practice, that's probably something that should get addressed eventually

cobalt mica
obsidian iris
#

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

cobalt mica
#

6M+ downloads per week, that thing shouldn't exist

obsidian iris
#

if we had it built into the class attribute it would be such a great win

cobalt mica
#

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)

plush pelican
obsidian iris
obsidian iris
charred pecan
#

and it's not backwards compatible for libraries which use JSX.IntrinsicElements and expect props.class to be undefined or a string

obsidian iris
#

clsx already supports classList-like resolution

obsidian iris
charred pecan
obsidian iris
charred pecan
#

i just wanted to point it out

#

it's probably fine tbh

plush pelican
#

Hmmmm I wonder why it’s been open as a PR for so long

obsidian iris
#

but iirc on his defense it is about compilation quirks

#

on how Solid optimizes classes for templates and how classList is compiled

plush pelican
#

Could we not facilitate this as a separate plugin? Something for SolidJS community to maintain?

cobalt mica
#

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'"

obsidian iris
#

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

timid karma
#

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..

obsidian iris
timid karma
#

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

obsidian iris
#

how does Svelte and Vue approah class composition

timid karma
#

Vue works similarly to solid now.
class for strings and :class for string templates and objects. (you can add them both to one el)

timid karma
obsidian iris