Skip to content

class-recipe

@lynstack/class-recipe maps a component’s variants, such as its tone and size, to its class names, for any CSS approach. You write the classes of each option once, in a recipe, and call the recipe with the component’s props:

import { cva } from "@lynstack/class-recipe";
export const button = cva({
base: "inline-flex items-center rounded-md font-medium",
variants: {
tone: {
neutral: "bg-gray-100 text-gray-900",
danger: "bg-red-600 text-white",
},
size: {
sm: "h-8 px-3 text-sm",
md: "h-10 px-4",
},
},
defaultVariants: { size: "md" },
});

PlaygroundChoose the variants. The classes a choice adds light up.

tone
size
button({ tone: "neutral" })→ "inline-flex items-center rounded-md font-medium bg-gray-100 text-gray-900 h-10 px-4"
You want to Use
Style one element, such as a button cva
Style several elements together, such as a card and its header sva
Join class names, as with clsx cx
Merge classes with a custom join, such as twMerge, or turn off the cache createRecipes
  • Fast. A recipe compiles its config once and caches the class name of each selection, so most calls are a lookup. It runs about 10 times as many calls per second as class-variance-authority (see Benchmarks).
  • Type-safe. Variant names, options, and slots are inferred from the config. An unknown option or slot is a type error, and a variant without a default is required, so a component cannot forget to choose it.
  • Pluggable. Bring your own join function, such as twMerge, to resolve conflicting classes. It runs once per selection, not on every call.
  • Small. ES modules only and tree-shakable. Its one dependency is @lynstack/recipe, the engine it is built on, and Bundlephobia shows its size.

Why class-recipe compares it with class-variance-authority and tailwind-variants. The glossary defines the terms these docs use.

Use it to style components with class names: Tailwind CSS, CSS Modules, or any stylesheet. To map variants to values other than class names, such as style objects, use @lynstack/recipe, the engine class-recipe is built on.