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
CSS/SCSS indenting #3012
Comments
How popular is this style? I've never run into it (but the apps I've worked on used sass or stylus rather than BEM or similar methodologies, so I think it was more common to express stuff via nesting). I mostly do component-based styling these days, so I don't have a good idea of what common CSS formatting in the community looks like. |
Yep it's fairly popular in large-scale CSS frameworks, and in books about CSS. Here are some examples:
Finally, there doesn't seem to be anyone against more indentation that I've found (some people conflate formatting with the bad practice of making overly specific CSS selectors but of course those are unrelated issues), but maintaining the indentation manually is a hassle -- and solving manual formatting is the whole point of Prettier! So where to from here? |
Some random things to discuss: 1. At work, we have one "BEM root" per file. For example, a file could be called 2. Actually, our classes look more like 3. I think Facebook uses Prettier to format their CSS these days, so I'm pinging @vjeux. Would this break havoc on your CSS? 4. Should this be behind an option? 5. It might be worth doing a PR now to see what things would look like, but with the obvious risk of it never being merged. Your call. |
|
|
Thanks for considering this! |
I don't think that relying on naming convention is a good idea here, because BEM is BEM, but some projects using full BEM, some are camelCase version of BEM and so on, most people using Harry Roberts version. In fact, original BEM naming is less popular nowadays that its successors. Thus, we will have hard time figuring out where is block, element and modifier. Or at least it will require ability to configure what will be considered a block, element and modified. Not to mention that some methodologies are extended. For instance, Harry Roberts suggested As an opposite solution, Prettier can ensure that nesting is consistent. For instance, it will fix this: .block {
/* styles */
}
.block__foo {
/* styles */
} To this (since it can not be nested that deep anyway) .block {
/* styles */
}
.block__foo {
/* styles */
} But will not touch .block {
/* styles */
}
.block__foo {
/* styles */
} because nesting isn't compromised. Also, as a side note, modifiers tend to be putted on same level as block or element, since they aren't their sub-part in DOM, they just modifies them: .block {
/* styles */
}
.block__foo {
/* styles */
}
.block__foo--modifier {
/* styles */
} |
I personally find this style harder to scan, and I have also never encountered it until this issue... |
|
Being behind a flag, I don't feel there's any harm in adding this feature. I'd like to address whether there's a want for this throughout the dev community though (rather than just our individual preferences and opinions). Sitepoint undertake a CSS survey every year, and here's a couple of excerpts from this year's results: So, the vast majority of people see value in indentation (either pre-processor style, or manually). Of the remaining ~quarter of people, I wonder how many of them don't indent because it's cumbersome to do so manually. I wouldn't be surprised if more people preferred indentation if their tooling did if for them. There's also a few of us mentioning their alternative bespoke naming schemas, so I wanted to know whether there's actually any weight in any of them: BEM's the clear winner when it comes to strict methodologies, but people are also using camelCased and snake_cased alternatives too, all of which would be compatible with this PR, as far as I can tell. Those not using a methodology, well, we probably can't help those people one way or the other 😂 So, yes I do think the community wants this. Most of us are indenting, and most of us are using compatible naming schemas. |
I think that might be misinterpreting the results. 24% are indenting. 44% are using a pre-processor. We don't know if they would indent the code if they weren't using a pre-processor (I don't, for example). |
You nest flat against the left hand side? Eg. parent {
child { /* styles */ }
} I've never seen that before. EDIT: Anyway, I agree you could interpret it that way too, but by putting this behind a flag - we're not stepping on your toes. No matter how you interpret the pre-processor block in those results, there's still a large portion of people with an appetite for this feature. |
With pre-processor: .block {
.modifier {
}
} Without:
|
Thanks, so with Pre-processor users (which includes me, most of the time!) Prettier is able to help and provide automated indentation. So let's try looking at the Sitepoint stats another way... 44% use nesting in Pre-processors, but really the portion of the stats that we're talking about are the others: those who use plain CSS, and those who don't use Pre-proccesor nesting (maybe to make searching for selectors easier, from what I've heard), and that leaves the stats Formatting issues and white-space are subtle things to discuss and articulate, but I think we can agree that everyone indents Sass for readability (yuss!), and 46% is about half the community who indent while using the others who could benefit from Prettier standardisation. Anyway, I don't know how to progress this further without repeating myself because I'm all out of data and examples 😀 Ultimately now it's your choice about whether this is a useful feature or not, and the PR has the beginnings of an implementation, so I'll keep quiet until I get some feedback. Thanks for considering this. |
Quickly popping my head in to say:
Thanks! |
I agree that indenting to simulate nesting is a moderately common convention (about 25% of devs, per the poll above—which jives with my sense from the community). It might be worthwhile as an option, but I don’t think it should be the default/only behavior. |
We use this style at work to see related CSS together. Initially it was a bit strange to read- like looking at JSX (from Facebook React) but now it's great, and it helps developers communicate relationships between CSS |
One advantage that indenting has, and that aligning flush with the left doesn't, is it helps keep class names consistent and will reduce unintentional errors. The indenting would clearly show if you'd made a spelling mistake or if you'd named a child differently to its parent. You'd be expecting an indentation whereas it would be flush left, immediately you could visually identify the error. It's like a nesting error in JavaScript that you can fix there instead of at run-time. CSS codebases have naming conventions so helping expose them makes sense. |
I must admit that this is the first I have ever heard of indenting CSS in this way, and I am not a fan right now anyway. I have nothing against others liking such things of course (and perhaps I become one of them at a future date), but it should be optional. |
This is a suggestion for a
changeoption in formatting SCSS/CSS.When reading JavaScript we all know how beneficial indentation can be to express nesting blocks, but in CSS there are also defacto and popular ways of expressing nesting that I think Prettier could automate.
BEM codifies some defacto standards in naming conventions for CSS, in which classes are named as,
Here nesting is expressed in a naming convention but also whitespace, and when I'm reading CSS I often find that classNames have hierarchy expressed in naming conventions (with SCSS of course programmers can also nest selectors and use
&
but that's a separate issue).Would you be open to accepting a patch to indent CSS/SCSS blocks based on the previous selector?
I think the formatting rule could be that if the previous selector is a prefix of the current selector then we consider that to be nesting and the block is nested a level. Any name that doesn't match would be formatted as Prettier currently does it (pushed left-most). Selectors would need to be contiguous to be indented. Of course there might be some details to work out but I just want to know if there's interest in this type of formatting for CSS/SCSS?
Just to be more specific...
Input:
Current Output:
Expected behavior:
Output:
The text was updated successfully, but these errors were encountered: