Composing components
Sometimes a component is the sum of multiple independent variant sets. CVA provides two approaches for combining them: manual composition withcx, 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
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
Use compose when…
Use compose when…
- You are on the
cvapackage (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
Use manual cx when…
Use manual cx when…
- You are on the legacy
class-variance-authoritypackage - 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
