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.

Feature request: put props as classes

See original GitHub issue

Hi 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:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
geelencommented, Nov 22, 2016

But you don’t even need to do that, just use a data-attribute & normal CSS:

const Button = styled.div`
  background: #f5f5f5;
  color: #000;

  &[data-kind~="success"] {
    background: green;
    color: white;
  }

  &[data-kind~="error"] {
    background: red;
    color: white;
  }

  &[data-kind~="warning"] {
    background: orange;
    color: white;
  }

  &[data-kind~="warning"] {
    border-radius: 50%;
  }
`

<Button data-kind='success circle' />

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 ✨

3reactions
mxstbrcommented, Nov 22, 2016

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.

const Button = styled.button`
  ${props => {
    if (props.primary) {
      css`some: styles;`
      if (props.white) {
        css`some: styles;`
      } else {
        css`some: styles;`
      }
    }
  }}
`
Read more comments on GitHub >

github_iconTop 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 >

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