File size: 2,785 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
---
title: Utilities
description:
  The utility API is a way to create your own CSS properties, map existing
  properties to a set of values or tokens.
---

Here are the properties you need to define or customize a utility:

- `shorthand`: The shorthand or alias version of the property
- `values`: The possible values the property can have. Could be a token
  category, or an enum of values, string, number, or boolean.
- `transform`: A function that converts the value to a valid css object

## Creating a custom utility

Let's say you want to create new property `br` that applies a border radius to
an element.

```tsx title="components/ui/provider.tsx"
import { createSystem, defaultConfig, defineConfig } from "@chakra-ui/react"

const customConfig = defineConfig({
  utilities: {
    extend: {
      br: {
        values: "radii",
        transform(value) {
          return { borderRadius: value }
        },
      },
    },
  },
})

const system = createSystem(defaultConfig, customConfig)
```

Now, you can use the `br` property in components.

```tsx title="app.tsx"
import { Box } from "@chakra-ui/react"

function App() {
  return <Box br="sm" />
}
```

### Using enum values

Let's say we want to create a new property `borderX` that applies a limited set
of inline border to an element and automatically applies the border color.

```tsx title="components/ui/provider.tsx"
import { createSystem, defaultConfig, defineConfig } from "@chakra-ui/react"

const customConfig = defineConfig({
  utilities: {
    extend: {
      borderX: {
        values: ["1px", "2px", "4px"],
        shorthand: "bx",
        transform(value, { token }) {
          return {
            borderInlineWidth: value,
            borderColor: token("colors.red.200"),
          }
        },
      },
    },
  },
})

const system = createSystem(defaultConfig, customConfig)
```

Now, you can use the `borderX` or `bx` property in components.

```tsx title="app.tsx"
import { Box } from "@chakra-ui/react"

function App() {
  return <Box borderX="sm" />
}
```

### Using mapped values

```tsx title="components/ui/provider.tsx"
import { createSystem, defaultConfig, defineConfig } from "@chakra-ui/react"

const customConfig = defineConfig({
  utilities: {
    extend: {
      borderX: {
        values: { small: "2px", medium: "5px" },
        shorthand: "bx",
        transform(value, { token }) {
          return {
            borderTopWidth: value,
            borderTopColor: token("colors.gray.400"),
          }
        },
      },
    },
  },
})

const system = createSystem(defaultConfig, customConfig)
```

Now, you can use the `borderX` or `bx` property in components.

```tsx title="app.tsx"
import { Box } from "@chakra-ui/react"

function App() {
  return <Box borderX="sm" />
}
```