Skip to content

Installation

Terminal window
pnpm i class-variance-authority
Do I really have to write such a long package name?

Unfortunately, for a little bit longer, yes. Originally, the plan was to publish the package as cva, but this name was taken and marked as a “placeholder”.

On 2022/02/16, GitHub transferred NPM ownership of cva to Joe Bell. This shorter name will be used from v1 onwards.

In the meantime, you can always alias the package for your convenience…

  1. Alias the package with npm install

    Terminal window
    npm i cva@npm:class-variance-authority
  2. Then import like so:

    import { cva } from "cva";
    // …

If you’re a Tailwind user, here are some additional (optional) steps to get the most out of cva:

You can enable autocompletion inside cva using the steps below:

  1. Install the “Tailwind CSS IntelliSense” Visual Studio Code extension

  2. Add the following to your settings.json:

{
"tailwindCSS.classFunctions": ["cva", "cx"]
}

Although cva’s API is designed to help you avoid styling conflicts, there’s still a small margin of error.

If you’re keen to lift that burden altogether, check out the wonderful tailwind-merge package.

For bulletproof components, wrap your cva component with twMerge.

Example with tailwind-merge
import { cva, type VariantProps } from "class-variance-authority";
import { twMerge } from "tailwind-merge";
const buttonVariants = cva(["your", "base", "classes"], {
variants: {
intent: {
primary: ["your", "primary", "classes"],
},
},
defaultVariants: {
intent: "primary",
},
});
export interface ButtonVariants extends VariantProps<typeof buttonVariants> {}
export const button = (variants: ButtonVariants) =>
twMerge(buttonVariants(variants));