-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Comments
class:list
directiveclass:list
directive
There already is a <a class:truthyClass={condition}> Adding the |
This is really the only problem I have in Svelte, missing the ability to have multiple classes in one directive. |
@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) |
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. |
I really like the Astro approach on this. Should be implemented in Svelte as well. |
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 likeclsx
allows an even more flexible approach, filtering out nullish and boolean values, and preserving intended class names.This is why…
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: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
The text was updated successfully, but these errors were encountered: