CSS Variables
Using token-aware CSS variables in Cerberus UI
Overview
CSS variables have become the defacto way to create shared values on the web. It's very useful to avoid prop interpolations, classname regeneration, and reduce runtime evaluation of token values.
Examples
Basic
Use the css
prop to create css variables
1<Box css={{ "--font-size": "18px" }}>2 <Text as="h3" css={{ fontSize: "calc(var(--font-size) * 2)" }}>Hello</Text>3 <Text css={{ fontSize: "var(--font-size)" }}>Hello</Text>4</Box>
Access tokens
Use the full token path to access semantic-tokens
1<Box css={{ "--color": "{colors.danger.text.initial}" }}>2 <Text css={{ color: "var(--color)" }}>Hello</Text>3</Box>
Here's an example of how to access size tokens
1<Box css={{ "--font": "{fonts.display}" }}>2 <Text as="h1" css={{ fontFamily: "var(--font)" }}>Hello</Text>3</Box>
Responsive Styles
Use the responsive syntax to make css variables responsive
1<Box css={{ "--font-size": { base: "18px", lg: "24px" } }}>2 <Text as="h3" css={{ fontSize: "calc(var(--font-size) * 2)" }}>Hello</Text>3 <Text css={{ fontSize: "var(--font-size)" }}>Hello</Text>4</Box>
Color Opacity Modifier
When accessing color tokens, you can use the opacity modifier to access the
color with an opacity. The requirement is to use the {}
syntax.
1<Box css={{ "--color": "{colors.red.500/40}" }}>2 <Text css={{ color: "var(--color)" }}>Hello</Text>3</Box>
Virtual Color
Variables can point to a virtual color via the colors.colorPalette.*
value.
This is useful for creating theme components.
1<Box2 colorPalette="success"3 css={{ "--color": "{colors.colorPalette.initial}" }}4>5 <Text css={{ color: "var(--color)" }}>Hello</Text>6</Box>
On this page