---
title: "style.recipe()"
description: "Create a factory that selects compiled styles by option, with the configuration of class-variance-authority."
image: "https://stillwind.dev/og.png"
---

> Documentation Index
> Fetch the complete documentation index at: https://stillwind.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# style.recipe()

`style.recipe(base?, config?)` creates a factory with the configuration format
of `cva` from [Class Variance Authority](https://cva.style/). With `slots`, it
also accepts the slot configuration of
[tailwind-variants](https://www.tailwind-variants.org/). The compiler generates
the CSS for every option at build time. Calling the factory selects the
compiled classes for the chosen options and returns props for an element.

`style.recipe()` exists mainly to migrate existing CVA and tailwind-variants
components. For new
components, see [Reusable styles](https://stillwind.dev/docs/guides/reusable-styles).

## Example

### Source

```tsx
import { style } from "stillwind/react";

export const button = style.recipe("inline-flex rounded-md", {
  variants: {
    size: {
      small: "px-2 py-1",
      large: "px-4 py-2",
    },
  },
  defaultVariants: {
    size: "small",
  },
  compoundVariants: [
    {
      size: "large",
      class: "font-medium",
    },
  ],
});

export const props = button({ size: "large" });
```

### Compiled code

```ts
import { stillwindClassNameObjectRecipe } from "stillwind/runtime";

export const button = stillwindClassNameObjectRecipe("sw_6tzpq", {
  variants: {
    size: {
      small: "sw_gzwci",
      large: "sw_w6696",
    },
  },
  defaultVariants: {
    size: "small",
  },
  compoundVariants: [
    {
      size: "large",
      class: "sw_v1iw3",
    },
  ],
});

export const props = button({ size: "large" });
```

### Generated CSS


```css
.sw_6tzpq {
  display: inline-flex;
  border-radius: var(--radius-md);
}

.sw_gzwci {
  padding-inline: calc(var(--spacing) * 2);
  padding-block: calc(var(--spacing) * 1);
}

.sw_w6696 {
  padding-inline: calc(var(--spacing) * 4);
  padding-block: calc(var(--spacing) * 2);
}

.sw_v1iw3 {
  --tw-font-weight: var(--font-weight-medium);
  font-weight: var(--font-weight-medium);
}
```

## Configuration

Write the configuration as an object literal in the call:

| Field              | Value                                                                              |
| ------------------ | ---------------------------------------------------------------------------------- |
| `variants`         | Each option name maps each value to a style                                        |
| `defaultVariants`  | The value that each option uses when the call passes `undefined`                   |
| `compoundVariants` | Entries that add `class` when every listed option matches                          |
| `slots`            | Each slot name maps to the style of that element part                              |
| `compoundSlots`    | Entries that add `class` to every slot in `slots` when every listed option matches |

`class` and `className` cannot be option names, because the factory uses them
for classes from other stylesheets. An option with the values `true` and
`false` accepts booleans.

Use `VariantProps<typeof button>` from your framework's entrypoint to type the
options.

## Style values

The base and each option value accept utility strings, arrays, objects with
variant keys, `true`, `false`, `null`, `undefined`, and custom properties.

They do not accept `&&`, ternaries, stored [`style()`](https://stillwind.dev/docs/macros/style)
results, refs, animations, or `class` fields. To choose a style at runtime,
pass a different option to the factory.
[invalid-recipe-input](https://stillwind.dev/docs/lints/invalid-recipe-input) reports these values, and
[invalid-recipe-config](https://stillwind.dev/docs/lints/invalid-recipe-config) reports a malformed
configuration.

A custom property value can be an expression. The factory evaluates it once,
when the factory is created. Custom properties stay in the inline style, so a
later option can replace them.

## Calling the factory

| Option value | Result                                |
| ------------ | ------------------------------------- |
| `undefined`  | Uses the value from `defaultVariants` |
| `null`       | Adds nothing for that option          |
| A value      | Adds the style of that value          |

The result combines, in order: the base, the selected values in the order of
the keys of `variants`, every matching compound variant in array order, and the
`class` or `className` from the call. The option values are not passed to the
element. Each call returns a new props object.

The factory takes a [`style.forward()`](https://stillwind.dev/docs/macros/style#forward-props-to-a-components-element)
value as its second argument. The forwarded classes come last, and the
forwarded inline style applies on top of the result:

```tsx
export function Button({ variant, className, style: inline }: ButtonProps) {
  return <button {...button({ variant }, style.forward({ className, style: inline }))} />;
}
```

The factory does not merge utilities at runtime. When two selected styles set
the same property, the compiler's CSS order decides which one wins.

The props that the factory returns cannot be passed to `style()` as a base.

## Slots

A component often styles several elements, such as a root, an icon, and a label.
`slots` names these parts. Each recipe with slots also has a `base` slot, which
receives the first argument of `style.recipe()`:

```tsx
import { style } from "stillwind/react";

export const card = style.recipe("flex items-center rounded-md", {
  slots: {
    icon: "size-4",
    label: "text-sm",
  },
  variants: {
    size: {
      small: {
        base: "p-2",
        icon: "size-3",
      },
      large: "p-4",
    },
  },
  compoundVariants: [
    {
      size: "large",
      class: {
        label: "text-lg",
      },
    },
  ],
  compoundSlots: [
    {
      slots: ["icon", "label"],
      size: "small",
      class: "gap-1",
    },
  ],
});

export function Card({ size, className }: CardProps) {
  const { base, icon, label } = card({ size, className });
  return (
    <div {...base()}>
      <span {...icon()} />
      <span {...label()}>Title</span>
    </div>
  );
}
```

With `slots`, an option value or a compound `class` or `className` value can be
an object whose keys all name slots. Each key adds its style to that slot. Any
other value applies to the `base` slot. A slot name cannot be empty, cannot
start with `--` or end with `:`, and cannot be `class` or `className`, because
those keys have other meanings in a style value.

Calling the factory returns one function for each slot. Each slot function
returns props for its element. A slot receives, in order:

1. For the `base` slot, the first argument of `style.recipe()`.
2. Its value in `slots`.
3. Its part of each selected option value.
4. Its part of each matching compound variant.
5. The `class` of each matching `compoundSlots` entry that lists it.
6. For the `base` slot, the classes and the `style.forward()` value passed to
   the factory.
7. The classes and the `style.forward()` value passed to the slot function.

A slot function also accepts option values, which replace the values passed to
the factory for that slot only, as in `icon({ size: "large" })`.

`style.recipe()` does not support the `extend` field of tailwind-variants.
[invalid-recipe-config](https://stillwind.dev/docs/lints/invalid-recipe-config) reports it and unknown
slot names.

## Generated code

The compiler creates the factory with a helper from `stillwind/runtime`, such as
`stillwindClassNameObjectRecipe` for React, together with the compiled class names.
Factory calls stay ordinary JavaScript.
The development build checks the selected values and the classes from other
stylesheets.

See [Migrating from Tailwind CSS](https://stillwind.dev/docs/guides/migrating-from-tailwind#cva) to
convert existing `cva` calls.

Source: https://stillwind.dev/docs/macros/recipe/index.mdx
