Using CVA in Next.js
The future state of component/application level styling will no longer rely on CSS/SCSS files (other than the necessary CSS files that store Tailwind configuration and drive theming). Tailwind will be used in tandem with CVA (Class Variance Authority) to easily map style options to the respective Tailwind classes.
CVA Intro/Overview
CVA (Class Variance Authority) is a JavaScript based, framework agnostic, variant management library. With CVA you can configure a few options that allow for variability across your component or HTML elements. The reserved property keywords CVA uses are variants, compoundVariants, and defaultVariants.
variants- base set of variations used by CVA to match a set of classes based on options passeddefaultVariants- the default settings to target from thevariantsobjectcompoundVariants- if needed, CVA allows you to define objects with properties to run checks on - if the properties are matched on the provided class will be applied as well.
Based on the options provided, a string will be then be returned with the correct styles pulled based on those passed settings.
The cva function accepts two arguments:
- An array of static classes to always apply to the element using this CVA mapping.
- The configuration object containing
variants, etc.
Below is an example configuration and walk through of some examples of the output class string and the reasons behind that final output.
| Example Configuration | Usage Examples |
|---|---|
The | No options passed
One option passed, other variant default
Both options passed
Both options passed and match was found on compoundVariants
|
Using CVA in the NextJS App
TODO