I am trying to convert the below code to cva..
{items.map((item, index) => {
const isActive = item.key === SOMETHING;
return (
<div
key={index}
className={clsx(
`cursor-pointer`,
{
"text-white": isActive && variant === "inverted",
"text-stone-950": isActive && variant === "default",
"text-[#9b9b9b]": !isActive,
"hover:text-white": variant === "inverted",
"hover:text-stone-950": variant === "default",
},
)}
>...</div>
});
I am trying this way –
const textContentCva = cva("", {
variants: {
variant: {
inverted: "hover:text-white",
default: "hover:text-stone-950",
},
},
});
But not sure how to pass the isActive variable.
I can create activeInverted, activeDefault variant but that doesn’t seem good.
Could anyone please tell how should I convert it to use cva?
2
Answers
What I have understand so far is that you are trying to do as below:
You could use CSS variables in CVA class names.
We can rationalize the conditions:
variant
changes the "accent" color.isActive
changes the default inactive text color#9b9b9b
to the accent color.In CVA, you can have a boolean
true
variant value but not a booleanfalse
. For thefalse
condition, we can use thedefaultVariants
to apply the appropriate classes.