How to conditionally render css with clsx?

5.1k views Asked by At

I have a button component that's supposed to change colors based on its status (passed in by props). The colors are stored in a css file. I read that the clsx npm is a good one to use, but I don't quite understand its documentation. How do I use clsx to conditionally render my button?

  import  colors from './colors.css'

  <MyButton
        className={cx( )} //<------ ???
      />

and my colors.css file:

.accept {
  background-color: green
}

.reject {
  background-color: red
}

.warning {
  background-color: orange
}
1

There are 1 answers

0
Keit Oliveira On

You can try this

<MyButton className={clsx({ 'your-class-name': yourConditional })} />

If your class is a variable, you can add inside [].

<MyButton className={clsx({ [yourClassName]: yourConditional })} />