Disallow or gracefully handle CSS selectors with hyphens (ie. ".main--red")
See original GitHub issueThe problem If you use hypens in your CSS selectors, it will generate a *.d.ts file that has compilation errors. For example, the following SCSS file:
.main {
border: 1px solid black;
&--red {
border-color: red;
}
}
becomes:
// This file is automatically generated.
// Please do not change this file!
export const main: string;
export const main--red: string; // ERROR ON THIS LINE
The solution At the very least, I think this tool should probably throw some kind of error. I’m not keen on the idea of making it transform into an identifier that doesn’t map 1-1 with the original CSS selector name.
EDIT: I’d be interested to know what conventions you guys follow if it isn’t BEM, ie. slight permutations on an element, such as it having an alternate color or skin.
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
hyphens - CSS: Cascading Style Sheets - MDN Web Docs
The hyphens CSS property specifies how words should be hyphenated when text wraps across multiple lines. It can prevent hyphenation entirely ...
Read more >Why hyphen (-) separated class names are widely used in CSS
When using attribute selectors like [class^="icon-"], [class*=" icon-"] to specifically and safely target the specific classname styles by ...
Read more >Keep your CSS selectors short
Keeping CSS selectors short helps with a lot of things: ... makes your code a lot more flexible and can gracefully handle breakages...
Read more >Why are dashes preferred for CSS selectors / HTML attributes
In CSS, one can use underscore(_) instead of hyphen(-) for CSS selectors(class, id, span, …), but preference is given based on its ease...
Read more >hyphens | CSS-Tricks
The hyphens property controls hyphenation of text in block level elements. You can prevent hyphenation from happening at all, allow it, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Use
css-loader
with option camelCase actually convert themain--red
to 2 set of keysmainRed
andmain--red
, which both link to the same style. I did not mean to modify the existing class, but it should not be exported as a type file because its invaild TypeScript. You can access to the style withstyles['main--red']
anyway without the type. for some people like to follow BEM pattern this should be acceptable or as a configuration. The camelCase convertioncss-loader
using is https://github.com/sindresorhus/camelcaseOur (current) philosophy is that we shouldn’t modify your classes in any way. If it’s something that TypeScript doesn’t allow, TypeScript should tell you—otherwise we’ll need to bloat up this loader with duplicated logic for detecting invalid identifiers.