Tokens
Named design values for space, size, radius, color, and z-index
Tokens give style props typed names for shared design values such as space, size, radius, color, and z-index.
Create them with createTokens, then pass them to createTamagui as part of
the configuration object.
Getting tokens
For example, if you define some tokens:
After you pass that into createTamagui, you can access your tokens from anywhere using getTokens:
or
getTokens takes no options in v3. Tokens are keyed by their unprefixed name,
so size.small is the only spelling you need. The prefixed option that used to
return a $-keyed copy is removed.
getTokens() returns Variable objects, which Tamagui uses to manage values and CSS variable names:
Tamagui has some helpers that make working with variables easier, which are documented in Exports, namely getVariable which will return the CSS variable on web, but raw value on native, and getVariableValue which always returns the raw value.
v3 token behavior
The default v3 configs do not export true token keys. Component defaults still
resolve to the default size, and that default size is 4.
@tamagui/get-token also no longer performs runtime token stepping. getSize,
getSpace, and getRadius resolve the same key you pass in. The old
stepTokenUpOrDown, getTokenRelative, and options such as shift, bounds,
and excludeHalfSteps are removed.
If you need a smaller or larger named token, choose that token explicitly. If you need proportional sizing, multiply the resolved numeric value.
Color tokens as fallback values for themes
Color tokens are available as fallback values when you access a theme. So when you useTheme() and then access a value that isn’t in the theme, it will check for a tokens.color with the matching name.
Think of it this way:
- Tokens are static and are your base values.
- Themes are dynamic, they can change in your React tree, and live above tokens.
You can skip themes at first and build with style props and tokens alone, adding light and dark themes only when you need them. Avoid nested themes until you need them.
Using tokens with components
When using styled or any Tamagui component like View, you can access tokens directly. Just like with useTheme, it will first look for a theme value that matches, and if not it will fall back to a token.
Tokens apply automatically to matching properties. size tokens apply to width and height, radius to borderRadius:
How tokens apply to attributes
Size
width, height, minWidth, minHeight, maxWidth, maxHeight
zIndex
zIndex
Radius
borderRadius, borderTopLeftRadius, borderTopRightRadius, borderBottomLeftRadius, borderBottomRightRadius
Color
color, backgroundColor, borderColor, borderBottomColor, borderTopColor, borderLeftColor, borderRightColor
Space
All properties not matched by the above.
Specific tokens
You can also define custom token categories of your own:
And then access them using the “specific tokens” syntax:
This, like all token values, works the same with styled:
When creating custom tokens, you can use the px helper to ensure values get proper pixel units on web while remaining as raw numbers on native:
The predefined token categories like size, space, and radius automatically add
pixel units where appropriate, so you don’t need to use the px helper for them.