CVA๋ JavaScript/TypeScript ๊ธฐ๋ฐ์ ์คํ์์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก, CSS ํด๋์ค ์ด๋ฆ์ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ฒด๊ณ์ ์ผ๋ก ๊ด๋ฆฌํ๊ณ ์ปดํฌ๋ํธ์ ๋ค์ํ ์คํ์ผ ๋ณํ(variants)์ ์ฝ๊ฒ ์ ์ํ ์ ์๋๋ก ๋์์ค๋๋ค. ํนํ Tailwind CSS์ ๊ฐ์ ์ ํธ๋ฆฌํฐ ํผ์คํธ CSS ํ๋ ์์ํฌ์ ํจ๊ป ์ฌ์ฉ๋ ๋ ์ ์ฉํ๋ฉฐ, React, Vue, Angular ๋ฑ ๋ค์ํ ํ๋ ์์ํฌ์ ํธํ๋ฉ๋๋ค.
CVA๋ ์ปดํฌ๋ํธ์ ์คํ์ผ ๋ณํ(์: ๋ฒํผ์ ํฌ๊ธฐ, ์์, ์ํ ๋ฑ)์ ํ์
์์ ํ๊ฒ ์ ์ํ๊ณ , ์ฝ๋์ ๊ฐ๋
์ฑ๊ณผ ์ ์ง๋ณด์์ฑ์ ๋์ด๋ ๋ฐ ์ด์ ์ ๋ง์ถ ๋๊ตฌ์
๋๋ค. ์๋ฅผ ๋ค์ด, ๋ฒํผ ์ปดํฌ๋ํธ์ "primary", "secondary" ๋ณํ์ด๋ "small", "large" ํฌ๊ธฐ๋ฅผ ๊ฐ๋จํ ์ ์ํ๊ณ , ์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํด๋์ค ์ด๋ฆ์ ๋์ ์ผ๋ก ์์ฑํ ์ ์์ต๋๋ค.cva.style
๊ฐ๋จํ ๋งํด, CVA๋ UI ์ปดํฌ๋ํธ์ ์คํ์ผ๋ง์ ๋ ๊ตฌ์กฐ์ ์ด๊ณ ์ ์ธ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๊ฒ ํด์ฃผ๋ ์ ํธ๋ฆฌํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
TypeScript์ Tailwind CSS๋ฅผ ์ฌ์ฉํด ๋ฒํผ ์ปดํฌ๋ํธ์ ๋ค์ํ ๋ณํ์ ์ ์ํ๋ ์ฝ๋ ์์ ์
๋๋ค.
import { cva } from 'class-variance-authority';
// CVA๋ก ๋ฒํผ ์คํ์ผ ๋ณํ ์ ์
const buttonVariants = cva(
'inline-flex items-center justify-center rounded-md font-medium', // ๊ธฐ๋ณธ ์คํ์ผ
{
variants: {
intent: {
primary: 'bg-blue-500 text-white hover:bg-blue-600',
secondary: 'bg-gray-500 text-white hover:bg-gray-600',
danger: 'bg-red-500 text-white hover:bg-red-600',
},
size: {
small: 'px-2 py-1 text-sm',
medium: 'px-4 py-2 text-base',
large: 'px-6 py-3 text-lg',
},
},
defaultVariants: {
intent: 'primary',
size: 'medium',
},
}
);
// React ์ปดํฌ๋ํธ์์ ์ฌ์ฉ
interface ButtonProps {
intent?: 'primary' | 'secondary' | 'danger';
size?: 'small' | 'medium' | 'large';
children: React.ReactNode;
}
const Button = ({ intent, size, children }: ButtonProps) => {
return (
<button className={buttonVariants({ intent, size })}>
{children}
</button>
);
};
// ์ฌ์ฉ ์์
const App = () => (
<div>
<Button intent="primary" size="small">Primary Small</Button>
<Button intent="secondary" size="medium">Secondary Medium</Button>
<Button intent="danger" size="large">Danger Large</Button>
<Button>Default Button</Button> {/* ๊ธฐ๋ณธ๊ฐ: primary, medium */}
</div>
);
export default App;- cva ํจ์: ๊ธฐ๋ณธ ์คํ์ผ๊ณผ ๋ณํ(variants)์ ์ ์ํฉ๋๋ค. ์ ์์์์๋ intent(๋ฒํผ ์์)์ size(๋ฒํผ ํฌ๊ธฐ)๋ฅผ ๋ณํ์ผ๋ก ์ค์ ํ์ต๋๋ค.
- ๋ณํ(variants): intent์ size์ ๋ฐ๋ผ Tailwind CSS ํด๋์ค๋ฅผ ๋์ ์ผ๋ก ์ ์ฉํฉ๋๋ค.
- ๊ธฐ๋ณธ๊ฐ(defaultVariants): ๋ณํ์ด ์ง์ ๋์ง ์์ ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ์ฉ๋ ์คํ์ผ์ ์ค์ ํฉ๋๋ค.
- ์ปดํฌ๋ํธ ์ฌ์ฉ: Button ์ปดํฌ๋ํธ๋ buttonVariants๋ฅผ ํธ์ถํด ๋์ ์ผ๋ก ํด๋์ค ์ด๋ฆ์ ์์ฑํ๊ณ , ์ด๋ฅผ
CVA๋ฅผ ์ฌ์ฉํ๋ฉด ์คํ์ผ ๋ณํ์ ์ฒด๊ณ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๊ณ , ํ์
์์ ์ฑ(TypeScript)์ ๋ณด์ฅํ์ฌ ๊ฐ๋ฐ์ ๊ฒฝํ์ ํฅ์์ํต๋๋ค.