Semantic Tokens
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 should reference to an existing (primitive) token.
To reference a value in a semantic token, use the token reference
{}
syntax.
1import {2 createCerberusConfig,3 createCerberusPreset,4} from '@cerberus/panda-preset'5
6export default createCerberusConfig({7 presets: [createCerberusPreset()],8
9 include: ['./app/**/*.{ts,tsx}'],10 exclude: [],11
12 theme: {13 tokens: {14 colors: {15 red: { value: "#EE0F0F" },16 },17 },18 semanticTokens: {19 colors: {20 invalid: { value: "{colors.red}" },21 },22 },23 },24})
Using Semantic Tokens
After defining semantic tokens, we recommend using the Panda CLI to generate theme typings for your tokens.
1npm panda codegen
This will provide autocompletion for your tokens in your editor.
1<Box color="invalid">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.
1import {2 createCerberusConfig,3 createCerberusPreset,4} from '@cerberus/panda-preset'5
6export default createCerberusConfig({7 presets: [createCerberusPreset()],8
9 include: ['./app/**/*.{ts,tsx}'],10 exclude: [],11
12 theme: {13 tokens: {14 danger: {15 red: { value: "red.500" },16 darkRed: { value: "red.600" },17 green: { value: "green.500" },18 darkGreen: { value: "green.600" },19 },20 },21 semanticTokens: {22 colors: {23 danger: {24 value: { base: "{colors.red}", _dark: "{colors.darkRed}" },25 },26 success: {27 value: { base: "{colors.green}", _dark: "{colors.darkGreen}" },28 },29 },30 },31 },32})
The conditions used in semantic tokens must be an at-rule or parent selector condition.
Semantic Token Nesting
Semantic tokens can be nested to create a hierarchy of tokens. This is useful when you want to group tokens together.
Use the
DEFAULT
key to define the default value of a nested token.
1import {2 createCerberusConfig,3 createCerberusPreset,4} from '@cerberus/panda-preset'5
6export default createCerberusConfig({7 presets: [createCerberusPreset()],8
9 include: ['./app/**/*.{ts,tsx}'],10 exclude: [],11
12 theme: {13 semanticTokens: {14 colors: {15 bg: {16 DEFAULT: { value: "{colors.gray.100}" },17 primary: { value: "{colors.teal.100}" },18 secondary: { value: "{colors.gray.100}" },19 },20 },21 },22 },23})
This allows the use of the bg
token in the following ways:
1<Box bg="bg">2 <Box bg="bg.primary">Hello World</Box>3 <Box bg="bg.secondary">Hello World</Box>4</Box>
On this page