question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Disallow or gracefully handle CSS selectors with hyphens (ie. ".main--red")

See original GitHub issue

The 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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JiarongGucommented, Mar 8, 2019

Use css-loader with option camelCase actually convert the main--red to 2 set of keys mainRed and main--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 with styles['main--red'] anyway without the type. for some people like to follow BEM pattern this should be acceptable or as a configuration. The camelCase convertion css-loader using is https://github.com/sindresorhus/camelcase

1reaction
markdalgleishcommented, Mar 8, 2019

Our (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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found