tailwind-variants is a library that extends Tailwind CSS with a powerful, first-class variant API, enabling developers to define component styles based on states, sizes, and other properties. It supports features like slots, composition, compound variants, and automatic conflict resolution via `tailwind-merge`. The library is fully typed for enhanced developer experience and is framework-agnostic. Currently at version 3.2.2, it sees active development with frequent minor and patch releases, addressing bugs and introducing new utilities like `cx` and `cnMerge`. A key differentiator is its dual build approach (original with `tailwind-merge` and a lighter-weight `/lite` version without it) and its performance optimizations, offering significant speed improvements in v3.
npm install tailwind-variantsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates defining a reusable button component with multiple variants for size, color, and full-width state, along with compound variants and default values. It showcases how to apply these variants to generate dynamic Tailwind CSS class strings.
Remove `responsiveVariants` and `withTv` usage. For responsive variants, add them manually to your class names.
If you require automatic conflict resolution, explicitly install `tailwind-merge`: `npm install tailwind-merge`. For custom configurations, use `cnMerge`.
Choose the appropriate import: `import { tv } from 'tailwind-variants'` for default behavior with `tailwind-merge`, or `import { tv } from 'tailwind-variants/lite'` for a smaller bundle without conflict resolution capabilities. Adjust usage to avoid passing `tailwind-merge` config to `/lite` imports.Replace all instances of `cnBase` with `cx`. The API remains identical.
If you were calling `cn` with configuration arguments, migrate to `cnMerge`. For simple merging with default config, `cn` usage remains largely the same, just without the `()` call for configuration.
Always check the compatibility notes in the `tailwind-variants` README or documentation when installing or upgrading. Ensure `tailwind-merge` and `tailwindcss` versions align with the recommended `tailwind-variants` version.
Ensure you are using `import { tv } from 'tailwind-variants'` (or `/lite`) for ESM modules. If in a mixed environment, check your build configuration to ensure ESM is handled correctly.If you need conflict resolution, ensure `tailwind-merge` is installed (`npm install tailwind-merge`) and that you are importing from the default `tailwind-variants` entry point (not `/lite`). If using `/lite`, handle conflicts manually or switch to the full build.
For custom `tailwind-merge` configurations, use the `cnMerge` function introduced in v3.2.2. If no custom config is needed, simply call `cn` without arguments: `cn('px-2', 'px-4')` instead of `cn('px-2', 'px-4')()`.Remove `withTV` from your `tailwind.config.js` or any other configuration files. `tailwind-variants` v1.0.0+ no longer requires this transformer due to changes in Tailwind CSS v4.