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.

The render sequence is wrong when passing CSS classes to child

See original GitHub issue

Let’s assume we have ComponentA and ComponentB in different files.

// file1.tsx
const useStyles = makeStyles()(({ palette, breakpoints }) => ({
  extra: {
    color: "red"
  }
}));

const ComponentA = () => {
  const { classes, cx } = useStyles();
  return <ComponentB extraClasses={classes.extra} />;
}
// file2.tsx
const useStyles = makeStyles()(({ palette, breakpoints }) => ({
  root: {
    color: "blue",
    [breakpoints.up("sm")]: {
      color: "yellow"
    }
  }
}));

export const ComponentB = ({ extraClasses }: { extraClasses?: string }) => {
  const { classes, cx } = useStyles();
  return <div className={clsx(classes.root, extraClasses)}>Hi</div>;
};

Ideally when using JSS, the extraClass in ComponentA will override the root class in ComponentB which can produce the right result on screen, which is that the text should be in “red” color.

Problems:

  1. If using clsx to merge classes, then two classes will be generated but in wrong sequence. The root class in ComponentB will always override extraClass in componentA and display “blue” color which is wrong.

  2. If using cx to merge classes, then one merged class will be generated in right sequence, but if there’s any breakpoint related css in ComponentB, then it will not work well because the css priority is always breakpoint first, so it will display “yellow” color which is also wrong.

Thanks for building this amazing package and this is the only issue we have so far.

Hope you can provide a fix or let us know if there’s any workaround. Our project is huge so finding them and changing them one by one is kind of impossible…

Cheers CJ

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

6reactions
garronejcommented, Oct 16, 2021

Everything should work in v1.1.0

2reactions
garronejcommented, Oct 16, 2021

Thank you for you appreciation 😊

Read more comments on GitHub >

github_iconTop Results From Across the Web

Specific targeting of CSS classes with Styled Components not ...
I have discovered the issue is that the CSS styles are not being ... below I am passing in classNames to the relevant...
Read more >
Specificity - CSS: Cascading Style Sheets - MDN Web Docs
The specificity algorithm is basically a three-column value of three categories or weights - ID, CLASS, and TYPE - corresponding to the three ......
Read more >
Demystifying styled-components - Josh W Comeau
This explains why we can't "pre-generate" the classes! We have to wait until the component is rendered before we know what CSS will...
Read more >
Top 18 Most Common AngularJS Developer Mistakes - Toptal
CSS and HTML are not the biggest concern anymore, in fact, there is no longer just a single concern. The front-end developer needs...
Read more >
Selectors Level 4 - W3C
The children of such pseudo-elements can simultaneously be children of other elements, too. However, at least in CSS, their rendering must ...
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