Feature request: put props as classes
See original GitHub issueHi everyone! Today, each styled component that change values based on props generate and inject a new css, but what do you think to put/convert props to classNames automatically (at least, for me, it will cover a lot of uses case and generate only one css)?
Example:
const Button = styled.div`
background: #f5f5f5;
color: #000;
&.kind-success {
background: green;
color: white;
}
&.kind-error {
background: red;
color: white;
}
/* or maybe, use some kind of custom select,
then you only convert props to class that really are styled */
:props[kind=warning] {
background: orange;
color: white;
}
&.circle {
border-radius: 50%;
}
`
//Call
<Button kind='success' circle />
//output
<div class="base-hash-class kind-success circle" />
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Support for class props other than className · Issue #3179 ...
Currently styled-components only supports styling via the className prop requiring hacky workarounds to make use of the other props. Take for ...
Read more >Classes - JavaScript - MDN Web Docs
Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on...
Read more >ReactJS Class Based Components - GeeksforGeeks
Example: Program to demonstrate the use of props in class-based components. Open the App.js file and replace the code with the below code....
Read more >Data Fetching: getServerSideProps - Next.js
Fetch data on each request with `getServerSideProps`. ... function — it will not work if you add getServerSideProps as a property of the...
Read more >Strict Mode - React
You can also add a wrapper DOM node in your component and attach a ref directly to it. class MyComponent extends React.Component {...
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
But you don’t even need to do that, just use a data-attribute & normal CSS:
The
~=
selector is pro for stuff like this. I wrote a blog post about using them exclusively for styling. This is one of the main reasons Styled Components looks the way it does — it lets people use the ✨ full power of CSS ✨At that point you’re just reinventing JavaScript with some arbitrary constructs. That introduces an unnecessary learning curve, while also removing a lot of the power you get from JavaScript.