Scott Barrar
Scott Barrar

Reputation: 157

custom button component + tailwind styles being purged

I want to create a button component like this:

    function Button ({ color }) {
      return (
       <button  className={`hover:bg-${color}-300 bg-${color}-100>
     
       </button>

That way I can make all of my button colors (with hover variant) consistent by just doing:

<Button color="green" >

</Button>

The problem is that purge doesn't see that I want a green button so no styling occurs when I use purge.

Is there a better way to create my button component that will comply with CSS purge?

Upvotes: 1

Views: 549

Answers (2)

Scott Barrar
Scott Barrar

Reputation: 157

I ended up doing this:

  <button type={type} title={title} disabled={disabled} className={`
    active:drop-shadow-3xl
    select-none
    font-mono
    font-bold
    m-1.5
    p-1.5
    active:translate-y-[2px]
    border-2
    border-gray-400
    rounded-md
    ${className}`}
    onClick={click}>
      {children}
    </button>

and then add whatever inside of ${className}. its not a great answer but it is how I did it

Upvotes: 0

AMGAD AHMAD
AMGAD AHMAD

Reputation: 11

css get the the classes as them write in components but for this issue you need to use
safelist: [{ pattern: /bg-(red|green|blue|sky)-(500)/, }, ], in tailwind.config to make them extract all time.

Upvotes: 1

Related Questions