-
Hi, I really like twMerge and I use it in combination with clsx with this helper function (credits go to @shadcn) import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
} What I am kind a missing is class sorting similar to the official I assume that sorting the function arguments is kind a complicated and messy... BUT would it be possible to sort the Advantage would be that data compression would become significantly more efficient if there is more matching strings eg. classes in the same order. Would love your thoughts on this! Thanks for this great library! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Hey @pingustar! 👋 thanks, I'm happy to hear you like the library! I think sorting the output of the twMerge function within tailwind-merge isn't a good idea. Here are a couple reasons:
I think it's better to rely on Also, you can enable sorting of the export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs)).split(' ').sort().join(' ')
} |
Beta Was this translation helpful? Give feedback.
-
@dcastil Conflicting:
Sorting:
As we write more complex classes and deal with variables, would it be possible that we get an unexpected visual result due to the output not reflecting the correct sorting? Or is the conflict solving enough to keep us safe from this kind of error? Also, you mentioned this in your answer
Is it safe to use Sorry for pinging you and sorry if I made any wrong assumption. Thanks in advance |
Beta Was this translation helpful? Give feedback.
-
I'm surprised this thread isn't longer. I do see the performance issue and bundle size @dcastil described. And also the need to stay in sync with the plugin. Ultimately I would still like to see this feature in these sort of utilities and in this case tailwind-merge. As I said, I was 100% sure this would be included out of the box. I also noted the date the plugin was released; Monday, January 24, 2022. It's rather new and tailwind-merge was created about 2 years ago. Maybe there needs to be some sort of collaboration between the two projects, @dcastil ? Maybe the sort algorithm from the plugin is small with good performance. I don't know about others but I would be willing to trade-off a bit of performance and bytes for the sorting capability! |
Beta Was this translation helpful? Give feedback.
Hey @pingustar! 👋
thanks, I'm happy to hear you like the library!
I think sorting the output of the twMerge function within tailwind-merge isn't a good idea. Here are a couple reasons:
prettier-plugin-tailwindcss
would require me to add a lot of…