Skip to main content

Composing components

Sometimes a component is the sum of multiple independent variant sets. CVA provides two approaches for combining them: manual composition with cx, and the compose helper available in the cva package.

Manual composition with cx

The most flexible approach is to call each CVA component separately and concatenate the results with cx. This keeps the variant definitions fully independent while letting you present a unified prop surface to consumers:
components/card.ts
The card function accepts props from both box and cardBase, calls each CVA component with its relevant props, and merges the results:

The compose helper

The cva package (v1+) exports a compose function that merges CVA components automatically. Pass the components as arguments; the returned function accepts the union of all their variant props:
components/card.ts
compose is exported from the cva package. It is not available in the legacy class-variance-authority package.

When to use each approach

  • You are on the cva package (v1+)
  • The composed components have no overlapping variant keys
  • You want the merged type to be inferred automatically
  • You do not need to transform or filter props before passing them down
  • You are on the legacy class-variance-authority package
  • The components have overlapping variant keys that need to be split manually
  • You need to pass different prop subsets to each component
  • You want explicit control over how props are distributed