Append custom class names to styled component
See original GitHub issueIs it possible to have classes added via the className prop of a styled component be appended to the end of the class list instead of the start.
Take this button component as an example, which has a custom class added.
const Button = styled('button')(...);
<Button className="my-class">Click Me</Button>
Currently results in:
<button class="my-class css-6r523">Click Me</button>
I would like the my-class
to be AFTER the css-6r523
class, like so:
<button class="css-6r523 my-class">Click Me</button>
Is there any way to achieve this, either through React or Babel or something else?
Thanks.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How to add ClassNames to Styled Components - Medium
Same code but with dynamic classNames added. In the code above, we would like to target the element separately by using dynamic classNames....
Read more >Add Classes to Styled Component - Stack Overflow
I am trying to add class names to a React Component to make it easier for me to customize that component using Styled...
Read more >Advanced Usage - styled-components
styled -components generates an actual stylesheet with classes, and attaches those classes to the DOM nodes of styled components via the className prop....
Read more >Applying correctly className with Styled components
Applying classes. The problem is that styled(Comp) is effective only if Comp is already a styled component. To achieve this, you must add...
Read more >How to Control Class Names in Styled Components
Internally, styled-components generates two classes for each component— one is static and one is dynamic. The class name is generated by ...
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
Should be possible with CacheProvider and prepend option: https://emotion.sh/docs/@emotion/cache#prepend . Works pretty much similar to
@emtion/css/create-instance
(which you have mentioned by referring to create-emotion)This is a common misconception but the order in which you put your class names onto an element doesn’t matter. What matters is the order in which those classes are put into a stylesheet. This means that given this CSS and the following HTML you will always have a red background for both elements:
Check out the live demo: https://codesandbox.io/s/dank-https-gy6v0?file=/src/index.js
This is mentioned, among other things, in one of our documentation pages: https://emotion.sh/docs/composition