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.

Nesting Emotion 10 classes for cascade

See original GitHub issue

Previously in Emotion 9 you were able to use Emotion class names to take advantage of cascade. You would wrap the emotion const in curly brackets and prefix it with a period, and then dollar sign. For example, you could do this:


const child = css`
  color: green;
`;
const parent = css`
  color: red;
  .${child} {
    color: yellow;
  }
`;

<div className={parent}>I am red</div>
<div className={child}>I am green</div>
<div className={parent}>
  <div className={child}>I am yellow</div>
</div>

How can I go about achieving this behavior in Emotion 10? That is my question.

The following is further information about what happens when you don’t use a period-dollar sign.

Now, the following was and is desirable: if no period are used in Emotion 9 or 10, the parent const will inherit the nested const styles. And furthermore, if that nested const then has overriding styles, those would ultimately be inherited by the parent.

const child = css`
  color: green;
`;
const parent = css`
  color: red;
`;
<div className={parent}>I am red</div>
<div className={child}>I am green</div>

const child = css`
  color: green;
`;
const parent = css`
  ${child}
`;
<div className={parent}>I am green</div>

const child = css`
  color: green;
`;
const parent = css`
  ${child} {
    color: yellow;
  }
`;
<div className={parent}>I am yellow</div>

const child = css`
  color: green;
`;
const parent = css`
  color: red;
  ${child}
`;
<div className={parent}>I am green</div>

const child = css`
  color: green;
`;
const parent = css`
  color: red;
  ${child} {
    color: yellow;
  }
`;
<div className={parent}>I am yellow</div>


Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:9
  • Comments:25 (4 by maintainers)

github_iconTop GitHub Comments

9reactions
BMCwebdevcommented, Feb 12, 2019

This isn’t totally safe, mind you. If you are rendering subcomponents, they will not always get the same hashed class name. Additionally it also seems that Emotion will tack on the subcomponents name to the end of the hashed class name to avoid clashing I assume. So, proceed with caution. What I would really like is for Emotion to return the functionality it had in V.9 - an example of which is found in their V9 documentation, last example on this page:

https://5bb1495273f2cf57a2cf39cc--emotion.netlify.com/docs/nested

6reactions
BMCwebdevcommented, Feb 12, 2019

The following will work, it just doesn’t work in CodePen as the output code there has more text tacked onto it, and it hashes the const names all over again. But if you test in your own code base, this does work. I don’t like doing it, it feels hacky. Would like Emotion to have something specific for these instances.


const cat = css`
  color: red;
`;
const dog = css`
  color: green;
  .css-${cat.name} {
    border-bottom: 1px solid currentColor;
  }
`;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nesting Emotion 10 classes for cascade - Stack Overflow
The following will work, it just doesn't work in CodePen as the output code there has more text tacked onto it, and it...
Read more >
Nested Selectors - Emotion
Sometimes it's useful to nest selectors to target elements inside the current class or React component. An example with an element selector is...
Read more >
[Solved]-Nesting Emotion 10 classes for cascade-Reactjs
Coding example for the question Nesting Emotion 10 classes for cascade-Reactjs.
Read more >
How to Make a List Component with Emotion - CSS-Tricks
So, let's say you have an .active class in one file and you want to ... import React from 'react'; import styled from...
Read more >
An Exploration of the Emotional Cascade Model in Borderline ...
The Emotional Cascade Model proposes that the emotional and behavioral dysregulation of individuals with borderline personality disorder (BPD) may be ...
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