Replies: 17 comments 22 replies
-
I have zero experience on how to build this plugin and what is possible or not but I add my 2 cents: would it be possible to see which variables are used to form the
So the plugin will look at which variables are used for the className value and check |
Beta Was this translation helpful? Give feedback.
-
I also want to suggest a simple solution to implement though it requires a bit more work for the user. There could be a comment which enables sorting in a subsequent string. Something like the I already use the inline comment { "tailwindCSS.experimental.classRegex": ["\\/\\*\\s?tw:\\s?\\*\\/\\s?'(.*)'"] } which then looks like this in JS code const classNameHiddenOnHover = isDatepickerOpen
? /*tw:*/ 'opacity-0'
: /*tw:*/ 'group-hover:opacity-0 group-focus-within:opacity-0' A comment like this could also be used to enable class sorting in arbitrary places. |
Beta Was this translation helpful? Give feedback.
-
Leaving a more complex use case here with ternaries and maps to be considered too: const buttonClass = [
'block transition',
`${full ? 'w-full' : 'w-fit'} ${
{
small: 'h-9',
medium: 'h-12',
large: 'h-14',
}[size]
} rounded-full ${
{
neutral: 'border-0',
primary: 'border border-blue-500',
secondary: 'border border-blue-500',
text: 'border-0',
}[variant]
} ${
{
neutral: 'bg-gray-900/5',
primary: 'bg-blue-500',
secondary: 'bg-transparent',
text: 'bg-transparent',
}[variant]
} ${
{
small: 'py-2 px-3',
medium: 'py-3 px-6',
large: 'py-4 px-10',
}[size]
}`,
].join(' ') I haven't put a lot of thought into it, but I'm not sure if the |
Beta Was this translation helpful? Give feedback.
-
@gustavopch The plugin actually already sorts the strings in your example if you put it inside a |
Beta Was this translation helpful? Give feedback.
-
Is it possible to use "Tagged Template Literals" in ES6 to give a signal to this plugin? For example above, maybe we can use it like this: |
Beta Was this translation helpful? Give feedback.
-
Another idea: const klass = classNames("flex flex-col min-h-0");
return <div className={klass} />; Edit: There exists already an idea for that #6382 😉 |
Beta Was this translation helpful? Give feedback.
-
It would be nice if we could annotate structures to sort. I would like to be able to sort the following: const baseClasses = 'rounded-md font-medium focus:outline-none'
const variantsLookup = {
primary: 'bg-cyan-500 text-white shadow-lg hover:bg-cyan-400 focus:bg-cyan-400 focus:ring-cyan-500',
secondary: 'bg-slate-200 text-slate-800 shadow hover:bg-slate-300 focus:bg-slate-300 focus:ring-slate-500',
danger: 'bg-red-500 text-white shadow-lg uppercase tracking-wider hover:bg-red-400 focus:bg-red-400 focus:ring-red-500',
text: 'text-slate-700 uppercase tracking-wider hover:underline hover:text-slate-600 hover:bg-slate-900/5 focus:text-slate-600 focus:ring-slate-500',
}
const sizesLookup = {
small: 'px-3 py-1.5 text-sm focus:ring-2 focus:ring-offset-1',
medium: 'px-5 py-3 focus:ring-2 focus:ring-offset-2',
large: 'px-8 py-4 text-lg focus:ring focus:ring-offset-2',
}
export const Button = (props) => {
const { variant, size, ...rest } = props
return <button {...rest} className={`${baseClasses} ${variantsLookup[variant]} ${sizesLookup[size]}`} />
} |
Beta Was this translation helpful? Give feedback.
-
We're using Class Variance Authority package for building tailwind css design system. Any chance sorting for this? const buttonStyles = cva(
"flex px-3.5 py-1.5 rounded font-medium focus:outline-none text-white justify-center items-center transition-colors",
{
variants: {
intent: {
primary: "bg-blue-500 hover:bg-blue-600",
},
fullWidth: {
true: "w-full",
},
},
defaultVariants: {
intent: "primary",
},
}
);
export function Button({ intent, fullWidth, ...props }) {
return <button className={buttonStyles({ intent, fullWidth })} {...props} />;
} |
Beta Was this translation helpful? Give feedback.
-
Any updates? |
Beta Was this translation helpful? Give feedback.
-
damn, hoping for this to work someday |
Beta Was this translation helpful? Give feedback.
-
At work we agreed a clean DX for this could be with a tagged literal. This is inspired from writing GROQ queries, for anyone that uses SanityCMS. They provide a So with this DX in mind it would be neat to have something like a import { tw } from '@tailwindcss'
const tailwindClass = tw`p-10 m-10`; // get full tw prettier and intellisense here I was able to get this half working by coping how Sanity does this, but prettier sorting doesn't work... Here are the manual steps to achieve this
//my-helpers/tw-tag.ts
export function tw(strings: TemplateStringsArray, ...keys: any[]): string {
const lastIndex = strings.length - 1;
return (
strings.slice(0, lastIndex).reduce((acc, str, i) => {
return acc + str + keys[i];
}, '') + strings[lastIndex]
);
}
import { tw } from '..my-helpers/tw-tag';
const tailwindClass = tw`bg-slate-400 md:bg-slate-400 p-10`;
{
"tailwindCSS.experimental.classRegex": [
"tw`([^`]*)", // tw`...`
]
} The final challenge is for tailwind prettier to auto sort the string which I'm not sure how to do... |
Beta Was this translation helpful? Give feedback.
-
please add reorder in |
Beta Was this translation helpful? Give feedback.
-
I have another use case where I don't have a className variable, because I'm passing classes to a complex third party component. Messing with
|
Beta Was this translation helpful? Give feedback.
-
I've edited the opening post to say as much, but the Prettier plugin can in fact now achieve what was asked in this request by allowing you to define sorting for arbitrary attributes, function calls and template literals. You can read the readme for the instructions. |
Beta Was this translation helpful? Give feedback.
-
How can we sort classes in projects which uses windstitch? |
Beta Was this translation helpful? Give feedback.
-
How would I be able to auto-sort this one? const buttonVariants = {
variant: {
default: 'bg-gray-600',
white: 'bg-white',
ghost: 'bg-blue-500'
},
text: {
default: 'text-white uppercase',
white: 'text-black uppercase',
ghost: 'uppercase'
}
}
const buttonStyles = cva(['h-14 flex-row justify-center items-center w-full'], {
variants: buttonVariants,
defaultVariants: {
variant: 'default',
text: 'default'
}
}) |
Beta Was this translation helpful? Give feedback.
-
Does I'm using the
The autocomplete works good, it just doesn't sort. |
Beta Was this translation helpful? Give feedback.
-
EDIT: I would like to point out to anyone arriving into this post that this issue has been solved - the Prettier plugin now lets you enable sorting for arbitrary attributes, function calls and template literals. You can read the readme for more instructions.
Is there any way to make the plugin sort classes inside variables? Consider the following code:
After formatting, this is the result:
I can imagine trying to sort all string variables by default would not be feasible, but some kind of workaround would be nice.
klass
is a pretty common variable name for CSS classes from what I've seen, so trying to sort strings assigned toklass
(as well as strings inside an array assigned toklass
, likeklass = ["some class strings", bool ? "this" : "that"]
) could be one possibility. I imagine this would be one thing that could use a config option if provided though, like a variable name string/regex that is used for matching what variables to try to sort.Beta Was this translation helpful? Give feedback.
All reactions