File size: 2,572 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
---
title: Handling dynamic styling in recipes
description: Learn how to create recipes with dynamic styling
publishedAt: "2025-01-03"
collection: theming
---

Suppose that you need to change the padding of a button based on some pressed
state.

```tsx
const App = () => {
  const [isPressed, setPressed] = useState(false)
  // How do style the button separately based on the pressed state?
  return <Button>Click Me</Button>
}
```

You might be tempted to do something like this:

```tsx {9-12}
import { defineRecipe } from "@chakra-ui/react"

export const buttonRecipe = defineRecipe({
  base: {
    display: "flex",
  },
  variants: {
    size: {
      sm: ({ isPressed }) => ({
        padding: isPressed ? "8" : "4",
        fontSize: "12px",
      }),
    },
  },
})
```

This doesn't work because Chakra doesn't support functions in recipes. We
require recipes to be serializable.

There are two ways to handle this:

## Using `data-*` attributes

First, apply the dynamic values to the component using the `data-*` attribute.

```tsx /data-pressed={isPressed || undefined}/
const App = () => {
  const [isPressed, setPressed] = useState(false)
  return <Button data-pressed={isPressed || undefined}>Click Me</Button>
}
```

Next, style the recipe using the `data-*` attribute.

```tsx {10-12}
export const buttonRecipe = defineRecipe({
  base: {
    display: "flex",
  },
  variants: {
    size: {
      sm: {
        padding: "4",
        fontSize: "12px",
        "&[data-pressed]": {
          padding: "8",
        },
      },
    },
  },
})
```

## Using `compoundVariants`

Compound variants allow you to create style overrides based on variant
combinations.

- First, create a `isActive` variant
- Next, create a `compoundVariants` array that contains the style overrides

```ts {15-17, 20-24}
import { defineRecipe } from "@chakra-ui/react"

export const buttonRecipe = defineRecipe({
  base: {
    display: "flex",
  },
  variants: {
    size: {
      sm: {
        padding: "4",
        fontSize: "12px",
      },
    },
    isPressed: {
      true: {},
      false: {},
    },
  },
  compoundVariants: [
    {
      size: "sm",
      isPressed: true,
      css: {
        padding: "8px",
        fontSize: "12px",
      },
    },
  ],
})
```

Then, you can pass the `isPressed` variant to the component as props.

```tsx /isPressed={isPressed}/
<Button visual="solid" isPressed={isPressed}>
  Click Me
</Button>
```

:::note

If you use TypeScript, don't forget to run the `npx @chakra-ui/cli typegen`
command to generate the types for the recipe.

:::