File size: 2,943 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 131 | ---
title: Semantic Tokens
description: Leveraging semantic tokens for design decisions in your app.
---
## Overview
Semantic tokens are tokens that are designed to be used in a specific context. A
semantic token consists of the following properties:
- `value`: The value of the token or a reference to an existing token.
- `description`: An optional description of what the token can be used for.
## Defining Semantic Tokens
In most cases, the value of a semantic token references to an existing token.
> To reference a value in a semantic token, use the token reference `{}` syntax.
```js title="theme.ts"
import { createSystem, defaultConfig, defineConfig } from "@chakra-ui/react"
const config = defineConfig({
theme: {
tokens: {
colors: {
red: { value: "#EE0F0F" },
},
},
semanticTokens: {
colors: {
danger: { value: "{colors.red}" },
},
},
},
})
export default createSystem(defaultConfig, config)
```
## Using Semantic Tokens
After defining semantic tokens, we recommend using the Chakra CLI to generate
theme typings for your tokens.
```bash
npx @chakra-ui/cli typegen ./src/theme.ts
```
This will provide autocompletion for your tokens in your editor.
```tsx
<Box color="danger">Hello World</Box>
```
## Conditional Token
Semantic tokens can also be changed based on the conditions like light and dark
modes.
For example, if you want a color to change automatically based on light or dark
mode.
```js title="theme.ts"
import { createSystem, defaultConfig, defineConfig } from "@chakra-ui/react"
const config = defineConfig({
theme: {
semanticTokens: {
colors: {
danger: {
value: { base: "{colors.red}", _dark: "{colors.darkred}" },
},
success: {
value: { base: "{colors.green}", _dark: "{colors.darkgreen}" },
},
},
},
},
})
export default createSystem(defaultConfig, config)
```
:::info
The conditions used in semantic tokens must be an at-rule or parent selector
[condition](/docs/styling/conditional-styles#reference).
:::
## Semantic Token Nesting
Semantic tokens can be nested to create a hierarchy of tokens. This is useful
when you want to group tokens together.
:::info
Use the `DEFAULT` key to define the default value of a nested token.
:::
```js title="theme.ts"
import { createSystem, defaultConfig, defineConfig } from "@chakra-ui/react"
const config = defineConfig({
theme: {
semanticTokens: {
colors: {
bg: {
DEFAULT: { value: "{colors.gray.100}" },
primary: { value: "{colors.teal.100}" },
secondary: { value: "{colors.gray.100}" },
},
},
},
},
})
export default createSystem(defaultConfig, config)
```
This allows the use of the `bg` token in the following ways:
```tsx
<Box bg="bg">
<Box bg="bg.primary">Hello World</Box>
<Box bg="bg.secondary">Hello World</Box>
</Box>
```
|