-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Smarter formatting of HTML class attribute #7865
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
Conversation
13a4c68
to
a82a950
Compare
foo | ||
bar | ||
foo | ||
bar |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this looks not exactly good, but this is not a real-life example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code from this github PR page
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like there is no good way to format this real example. Probably we should simply retain new lines in the class
attribute, only re-indenting them if needed. That would be pity, the idea with prefixes looked good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(from my perspective at least, the output in @fisker 's playground is great)
class="foo" | ||
class=" | ||
foo | ||
" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this is a printWidth: 1
test.
0f30eae
to
cd0956e
Compare
function getClassPrefix(className) { | ||
let idx = className.search(/--|__/); | ||
if (idx === -1) { | ||
idx = className.indexOf("_"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about classes with a leading _
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
changelog_unreleased/html/pr-7865.md
Outdated
flex-column flex-lg-row | ||
justify-content-start justify-content-lg-between | ||
align-items-start align-items-lg-center | ||
" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid we will get issues with requests to format each class on each line/don't touch, maybe we put information why we prefer it to docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid we will get issues with requests to format each class on each line/don't touch, maybe we put information why we prefer it to docs?
For me I prefer one name per line, also consider the following example:
<div class="custom-btn my-very-v-button--mini site-btn my-very-v-button--primary my-very-v-button--ghost">my button</div>
After running prettier, I got:
<div
class="
custom-btn
my-very-v-button--mini
site-btn
my-very-v-button--primary my-very-v-button--ghost
"
>
my button
</div>
Given 80
as print width, the 3 names starting with my-very-v-button
are actually able to fit into one line, but they are not, also it looks better to at least put them next to each other since they share the same prefix, i.e. switch the site-btn
line with the last line.
So, my suggestion is let's don't group names with same prefix into one line, just let each name stands in it's own line(which is good for Git diff also), anyway an element or component that has more than 10 class names is really edge case, in that case I should consider my way of using class names, there must be some parts that can be improved to avoid too many class names on the same element.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the 3 names starting with my-very-v-button are actually able to fit into one line, but they are not
Right, because Prettier never reorders anything. However, one of the points of the current proposal is that in real projects, classes usually are already sorted by prefix, and when they are not, it's for a reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid we will get issues with requests to format each class on each line/don't touch, maybe we put information why we prefer it to docs?
I added an explanation to the changelog entry. Do you think we also need to write something in the docs about this?
All classnames in this PR page |
5877f1c
to
272c88c
Compare
…ve classes with the same prefix on one line
Co-Authored-By: Jed Fox <git@twopointzero.us>
272c88c
to
338bcc2
Compare
Any progress on this PR? Do we have a plan to merge it sometime? Up to now it's the only issue blocks me from upgrading Prettier to higher version. |
bump /cc @pburkindine |
Despite the low interest from the community let's give this a go... |
This is going to be a nightmare for Tailwind CSS users |
I like the idea 👍. Given that we would never write 20 css instructions into a single line in a css file, I don't know how we accepted "minifying" the class attribute to begin with. Yes, it is verbose. But 20 class names is also very complex. I love the idea of grouping classes by prefix. |
Is there a way of disabling this? |
It's not configurable. If #10803 gets implemented, it'll be possible to make a plugin to override this behavior. |
We should have the option to disable this feature... this is still useful for some users though. |
This is being really annoying, disabling prettier for now. P.S. I use TailwindCSS |
Yeah, this is not working for us and the tailwind css classes. First I thought I could maybe refactor the problematic instances to have fewer classes, but that doesn’t work either. My colleague is already giving me a hard time for automatically formatting with 80 character lines. 😞 |
I'm going to downgrade to 2.2 for now. I just want to add another problem that this feature introduces. you get this result which evaluates to false:
Also if you try a workaround your test is not passing again:
This is the main reason that i will downgrade. Apart from that, thank you for your effort and have a nice day. |
That looks like a bug in -let classes = classAttribute ? classAttribute.split(' ') : []
+let classes = classAttribute ? classAttribute.split(/\s+/gm) : [] |
As a tailwind user, I would need this feature to be smarter. There simply aren't enough prefixes to be grouped. I would want classes to be grouped by domain (positioning, flexbox, spacing, ...), with one line per group. Otherwise (as second best option), I'd really just want prettier to leave my classes alone and respect the newlines I add for my own grouping. Auto-indenting these groups is of course desired. |
Please open an issue with example(s) where is bad, no need to write here, it was merged |
Break classes onto multiple lines, but try keeping consecutive classes with the same prefix on one line.
fixes #7863
docs/
directory)changelog_unreleased/*/pr-XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.✨Try the playground for this PR✨