Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svelte 5: Add class:list directive #12610

Closed
brandonmcconnell opened this issue Jul 25, 2024 · 6 comments · Fixed by #14714
Closed

Svelte 5: Add class:list directive #12610

brandonmcconnell opened this issue Jul 25, 2024 · 6 comments · Fixed by #14714

Comments

@brandonmcconnell
Copy link
Contributor

brandonmcconnell commented Jul 25, 2024

Describe the problem

One of the most common packages many repos install once they pick up steam is clsx or a similar library to help manage their classes. While svelte already allows for easy CSS class name interpolation, a convention like clsx allows an even more flexible approach, filtering out nullish and boolean values, and preserving intended class names.

<!-- without clsx -->
<a class="staticClass {condition ? 'truthyClass' : 'falsyClass'}">…</a>

<!-- with clsx -->
<a class={clsx('staticClass', condition && 'truthyClass')}>…</a>

This is why…

  • over 4,000 different public svelte projects on GitHub alone also install clsx (proof 🍮)
  • nearly 1,000 different public svelte projects on GitHub alone also install classnames (proof 🍮)

Describe the proposed solution

Building this functionality into svelte directly using a directive like Astro's class:list directive would bring this capability to all svelte users, allowing svelte developers to add and manage classes more effectively, like this:

<!-- with class:list -->
<a class:list={['staticClass', condition && 'truthyClass']}>…</a>

Prior Art

Astro is the most recent and prominent prior art here, but users have been using clsx and others like it for years. This would provide a unified and built-in way to help manage classes for svelte developers.

Svelte has always been a "batteries included" framework, and I think the addition of a directive like this would follow that same trend.

Below is a list of all the issues I found related to Astro's implementation and refinement of class:list. That team started with a custom solution, but due to some gotchas, they ended up refactoring to build upon clsx itself.

Importance

would make my life easier

@brandonmcconnell brandonmcconnell changed the title Add class:list directive Svelte 5: Add class:list directive Jul 25, 2024
@brunnerh
Copy link
Member

There already is a class: directive, so this is not going to work.

<a class:truthyClass={condition}>

Adding the clsx functionality has been requested before, but the only thing I see missing is multiple classes in one class: directive, which is covered by this issue.

@henrikvilhelmberglund
Copy link

This is really the only problem I have in Svelte, missing the ability to have multiple classes in one directive.
I think instead of class:list it should be classes: or just class: reworked to allow for both single and multiple classes.

@ottomated
Copy link
Contributor

@brunnerh one thing missing from that is ternary classes, i.e.

<div
  class:border-red={someExpensiveFn()}
  class:border-green={!someExpensiveFn()}
/>

vs

<div class={clsx([someExpensiveFn() ? 'border-red' : 'border-green'])} />

(and yes, this can be done with string interpolation, but it causes performance issues as the entire className has to be updated rather than just the classes in question toggled)

@aholland
Copy link

aholland commented Aug 1, 2024

The shorthand class directive would be really neat if only it automatically mapped camelCase to kebab-case. I could rename my classes, but there are css frameworks to consider.

@diegoulloao
Copy link

I really like the Astro approach on this. Should be implemented in Svelte as well. +1

@KelechiOdom10

This comment was marked as duplicate.

dummdidumm added a commit that referenced this issue Nov 13, 2024
#7170 / #12610 / #7294

todos:
- language-tools support (syntax highlighting & intellisense)
- playground syntax highlighting?
- ssr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants