예시(Chips)
첫 번째 방법
function Chip({ label, intent }: ChipProps) {
const colorClassName = intent === "primary" ? "bg-blue-500 border-blue-500 text-white" : intent === "warning" ? "bg-yellow-500 border-yellow-500 text-white": "";
return <div className=`text-sm border rounded-full px-2.5 py-0.5 hover:opacity-50 transition-opacity ${colorClassName}`>{label}</div>;
}
두 번째 방법
function Chip({ label, intent }: ChipProps) {
clsx({
"bg-blue-500 border-blue-500 text-white": color === "blue",
"bg-yellow-500 border-yellow-500 text-white": color === "yellow",
});
return <div className=`text-sm border rounded-full px-2.5 py-0.5 hover:opacity-50 transition-opacity ${colorClassName}`>{label}</div>;
}
마지막 방법 (효과적인 방법)
function Chip({ label, intent }: ChipProps) {
const chipVariants = cva("text-sm border rounded-full px-2.5 py-0.5 hover:opacity-50 transition-opacity", {
variants: {
intent: {
primary: "bg-blue-500 border-blue-500 text-white",
secondary: "bg-gray-500 border-gray-500 text-white",
danger: "bg-red-500 border-red-500 text-white",
warning: "bg-yellow-500 border-yellow-500 text-white",
info: "bg-violet-500 border-violet-500 text-white",
default: "bg-white border-black text-black",
},
},
defaultVariants: {
intent: "default",
},
});
return <div className={chipVariants({ intent })}>{label}</div>;
}