Break long class names into multiple lines? [Prettier Plugin] #7763
Replies: 35 comments 38 replies
-
on the plus side, breaks you put in will not be changed at least, though I would love some line length enforcement, seems very strange that it's the only thing in my files which will expand over the max width |
Beta Was this translation helpful? Give feedback.
-
if you are using frontend framework like React. the easiest way is by using clsx, you can separate the class by putting a comma, because you call a function that accept multiple arguments. example on React: import clsx from "clsx"
export default function App(){
return (
<div className={clsx("w-full h-full", "flex items-center justify-center")}>
// other components or
</div>
)
} optional: Enhance it with tailwind merge to solve style conflict, create a new util files like this one // @ utils/twclsx.ts
import clsx, { ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export const twclsx(...args: ClassValue[]) => twMerge(clsx(...args)) |
Beta Was this translation helpful? Give feedback.
-
You could use @emotion/css or styled-components or even const using the template literals principals |
Beta Was this translation helpful? Give feedback.
-
Would love addition to prettier for this, it's especially painful in vim/neovim where horizontal scrolling is rough, I manually do it like so: <main
className={`
flex
h-screen
w-screen
flex-col
items-center
justify-center
bg-gray-800
`}
> |
Beta Was this translation helpful? Give feedback.
-
Is there any desire for something like this to get merged into tailwind? This would be a really exciting feature for me and my team to take advantage of |
Beta Was this translation helpful? Give feedback.
-
Currently I'm running a self written "minifier" using node-html-parser and magic-string. The minifier removes all newlines and unnecessary whitespace in any inline css class declarations at preprocessor/bundler time. Would be nice to see something similar as an official tailwind option. Here running as svelte preprocessor e.g.: import parser from "node-html-parser";
import MagicString from "magic-string";
/** @type {import('node-html-parser').default} */
const parse = parser.parse;
const regEx = /\s{2,}|[\n\r]+/g;
/** @type {(search: string) => RegExp} */
function createRegEx(search) {
return new RegExp(search.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"), "g");
}
/** @type {(node: Node, changes: string[][]) => void} */
function traverse(node, changes) {
const childNodes = node.childNodes;
if (childNodes && childNodes.length) {
for (let i = 0; i < childNodes.length; i++) {
/** @type {HTMLElement & { rawAttrs?: string }} */
const childNode = childNodes[i];
if (
childNode.rawAttrs &&
childNode.rawAttrs.match &&
childNode.rawAttrs.match(regEx)
) {
changes.push([
childNode.rawAttrs,
childNode.rawAttrs.replace(regEx, " "),
]);
}
traverse(childNode, changes);
}
}
}
/** @type {(filename: string, content: string, changes: string[][]) => { code: string map: string }} */
function applyChanges(filename, content, changes) {
const s = new MagicString(content);
for (let i = 0; i < changes.length; i++) {
const [source, replace] = changes[i];
const regex = createRegEx(source);
const [...result] = content.matchAll(regex);
for (let j = 0; j < result.length; j++) {
const match = result[j];
if (!isNaN(match.index)) {
s.overwrite(match.index, match.index + source.length, replace);
}
}
}
var map = s.generateMap({
source: filename,
file: `${filename}.map`,
includeContent: true,
});
return {
code: s.toString(),
map,
};
}
/** @type {ReturnType<import('svelte-preprocess').default>} */
export const minifyHtmlAttributes = {
markup({ content, filename }) {
let code = content;
let map = undefined;
try {
const html = parse(content);
const changes = [];
traverse(html, changes);
const result = applyChanges(filename, content, changes);
if (result) {
code = result.code;
map = result.map;
}
} catch (error) {
console.error(error);
}
return { code, map };
},
}; |
Beta Was this translation helpful? Give feedback.
-
this would result prettier git diff! 🚀 |
Beta Was this translation helpful? Give feedback.
-
It makes sense to add line breaks to the plugin for the following reasons:
The expected result would be to group |
Beta Was this translation helpful? Give feedback.
-
Any news on this? |
Beta Was this translation helpful? Give feedback.
-
would definitely be useful |
Beta Was this translation helpful? Give feedback.
-
It would be really helpful if there is an option. |
Beta Was this translation helpful? Give feedback.
-
It would be very useful, any news? |
Beta Was this translation helpful? Give feedback.
-
Would be really nice |
Beta Was this translation helpful? Give feedback.
-
Waiting for this feature. |
Beta Was this translation helpful? Give feedback.
-
Waiting for this feature as well |
Beta Was this translation helpful? Give feedback.
-
After more deeper development and investigation, I think I find a best solution for now.
Try overall features on Gitpod. |
Beta Was this translation helpful? Give feedback.
-
I'm also interested in this! |
Beta Was this translation helpful? Give feedback.
-
This is one of the main downsides of working with TailwindCSS in modern web development. |
Beta Was this translation helpful? Give feedback.
-
has anyone found a good way to integrate these proposed sollutions into neovim? |
Beta Was this translation helpful? Give feedback.
-
Instead of doing this long class names. We can just put that in json file and we can parse it as an object and add it to as string. This enables the our own naming conventions into the bunch of class names |
Beta Was this translation helpful? Give feedback.
-
Just to add another voice, having the option to format classes over multiple lines would effectively eliminate the one downside (and initial hurdle for many newcomers) that Tailwind has: the line length. As others have mentioned it goes against common formatting conventions to leave the length unrestricted, but it's not just an aesthetic thing. It also makes the styles less legible compared to the ideas cited in this thread, especially the ones where the classes are also grouped, for example by breakpoint. Even though I love Tailwind enough to use it on basically every project, and have been for some time, the fact that this isn't a thing yet still rubs me the wrong way and is always at the back of my mind. This plugin seems like the perfect place to add this functionality. |
Beta Was this translation helpful? Give feedback.
-
This is not integrated in the tailwind plugin to date, currently the best solution is still shift + scroll and a UW monitor or eyeballing it with template strings. |
Beta Was this translation helpful? Give feedback.
-
Hello everyone! There is a prettier plugin that I released this year. Maybe this will help. |
Beta Was this translation helpful? Give feedback.
-
https://www.npmjs.com/package/eslint-plugin-readable-tailwind |
Beta Was this translation helpful? Give feedback.
-
sadly non of this options have worked for me. my vscode solution is to quickly toggle word wrapping in file with alt+z
|
Beta Was this translation helpful? Give feedback.
-
There are some way to solve this problem. I hope this helps you. |
Beta Was this translation helpful? Give feedback.
-
Any update ? |
Beta Was this translation helpful? Give feedback.
-
Would love to use a template string for multiline. I think a more nuanced whitespace setting would be good. RIght now it's too strict, if it could strip only spaces but leave newlines alone it would allow for things like <div className={`
tw-inline-flex tw-items-center tw-justify-center tw-gap-2 tw-border-2 tw-border-transparent tw-text-center
tw-font-body tw-font-semibold tw-transition hover:tw-text-current hover:tw-shadow-active disabled:tw-shadow-none
`} /> |
Beta Was this translation helpful? Give feedback.
-
Just gonna leave these here: |
Beta Was this translation helpful? Give feedback.
-
Still nothing? :o |
Beta Was this translation helpful? Give feedback.
-
Hey guys, it's not uncommon for class names to become very long, it's pretty hard to maintain and read. I somehow always break those class names into multiple lines manually, semantically even (by breakpoints). Are there any proper ways of dealing with this problem? In ideal case I could imagine being able to configure line breaks for classes in prettier plugin. Does it make any sense?
Beta Was this translation helpful? Give feedback.
All reactions