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.

Css classes are not splitted to treat output CSS effectively

See original GitHub issue

After reading documentation and googling I nowhere found, how to make Emotion effective and split produced css code.

Version of Emotion: I have this issue on latest v 10.

Repro steps: I have component like this:

import styled from '@emotion/styled'

const StyledInput = styled.input`
  box-sizing: border-box;
  border: slim;
  border-color: red;
  color: ${(props: any) => myfunction(props.color || 'black')};
  width: 100%;
`

And I use it two times like this:

<StyledInput color="red" />
<StyledInput color="green" />

Current CSS output:

.class1 {
  box-sizing: border-box;
  border: slim;
  border-color: red;
  color: red;
  width: 100%;
}
.class2 {
  box-sizing: border-box;
  border: slim;
  border-color: green;
  color: red;
  width: 100%;
}

Expected CSS output:

.commonClass {
  box-sizing: border-box;
  border: slim;
  border-color: red;
  width: 100%;
}
.class1 {
  color: red;
}
.class2 {
  color: red;
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Andaristcommented, Feb 21, 2020

Sub and SubInRed are treated as completely separate components - each has its own unique class name and thus they SubInRed won’t be targeted by Title’s styles.

Technically we maybe could change this behavior, but I’m not convinced that we should. You could raise a separate issue about this so we could discuss this - we are working on v11 so this potentially could make it into that release, although keep in mind that we probably won’t decide to support this.

0reactions
tomchentwcommented, Feb 21, 2020

@Andarist my expectation would be :

  1. SubInRed will inherit the class name from Sub
  2. Hence, the adjacent selector in Title will turn on Sub’s display attribute.

Instead, I got:

  1. SubInRed will NOT inherit any class name from Sub.
  2. The adjacent selector in Title will not affect SubInRed.

Previously this will work with the library where I came from (linaria). But from reading your comment above in this issue, I think this was the expected behavior by design. <- I just want to confirm this with you.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it a good practice to split CSS? [closed] - Stack Overflow
If you working for development, yes, you need to separate CSS files because you can modify code very easily. If you are on...
Read more >
Methods to Organize CSS
Class names are descriptive property names, not describing the semantic nature of the element, which can sometimes complicate development.
Read more >
How To Optimize CSS for Peak Site Performance - Kinsta
There is a better option: Split CSS into separate files with clear levels of responsibility and document accordingly. Removing unnecessary ...
Read more >
Organizing your CSS - Learn web development | MDN
Consistency can be applied in all sorts of ways, such as using the same naming conventions for classes, choosing one method of describing...
Read more >
TailwindCSS: Adds complexity, does nothing.
This is not a bad thing. I have written, and used utility classes a great deal myself, especially when I'm writing CSS without...
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