This is my react component
const ButtonComponent = ({ children, onClick, disabled, className = "" }) => {
console.log("class name is ", className);
const buttonClassName = `
bg-green-700
flex
hover:bg-green-500
transition
duration-300
ease-in-out
text-white
h-auto
text-base
p-2
rounded-[5px]
m-2
${
disabled
? "bg-teal-200 text-teal-800 cursor-not-allowed"
: "cursor-pointer"
}
${className}
`;
return (
<button onClick={onClick} className={buttonClassName} disabled={disabled}>
{children}
</button>
);
};
export default ButtonComponent;
The console.log is working properly, but in the JSX, the className prop is not giving right value.
2
Answers
I think the problem is with tailwind css, tailwind doesn’t build classes if you don’t use them directly in the className property. If you are sure to use classes in your project use the safelist directive in the tailwind config file or use the full name of the classes in the conditionals.
This post talks about it
https://www.vincentschmalbach.com/tailwind-css-and-dynamic-or-conditional-class-names-in-vue/
Is for vue but explain the problem.
If the className is being set correctly but the styles don’t appear as expected, ensure there’s no CSS specificity or conflict issue. For example, other styles in your stylesheet might be overriding the ones you set through your className prop due to higher specificity or later source order.
Try this.
I hope to help you this.