|
| 1 | +--- |
| 2 | +title: 'CSS Variables' |
| 3 | +description: Working with color mode (light and dark mode) in Chakra UI |
| 4 | +--- |
| 5 | + |
| 6 | +Chakra UI now converts theme tokens (colors, font sizes, etc) to CSS Custom |
| 7 | +Properties (also known as "CSS variables"). |
| 8 | + |
| 9 | +CSS variables are now supported in all modern browsers, and we think they are |
| 10 | +useful to: |
| 11 | + |
| 12 | +- avoid prop interpolations; |
| 13 | +- avoid classname regeneration from |
| 14 | + [emotion](https://emotion.sh/docs/introduction); |
| 15 | +- reduce runtime evaluation of token values in Theme context; |
| 16 | +- use theme token outside Chakra's component (embedded forms, markdown content, |
| 17 | + etc.) |
| 18 | + |
| 19 | +## Overview |
| 20 | + |
| 21 | +There are three main components you should know about when using CSS variables: |
| 22 | + |
| 23 | +- How Chakra converts tokens to custom properties |
| 24 | +- How to consume them in and outside Chakra's component |
| 25 | +- Where we attach the custom properties to |
| 26 | + |
| 27 | +## Converting theme tokens to CSS variables |
| 28 | + |
| 29 | +By default, Chakra UI converts theme tokens defined in your theme (or our |
| 30 | +default theme) to CSS variables internally so you don't have to. |
| 31 | + |
| 32 | +Given a theme object that looks like this: |
| 33 | + |
| 34 | +```jsx live=false |
| 35 | +const theme = { |
| 36 | + fontSizes: { |
| 37 | + lg: '18px', |
| 38 | + }, |
| 39 | + colors: { |
| 40 | + gray: { |
| 41 | + 100: '#fafafa', |
| 42 | + 200: '#f7f7f7', |
| 43 | + }, |
| 44 | + }, |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +When you pass this theme to the `ChakraProvider`, we'll automatically generate |
| 49 | +CSS variables that look like: |
| 50 | + |
| 51 | +```css |
| 52 | +:root { |
| 53 | + --chakra-fontSizes-lg: '18px'; |
| 54 | + --chakra-colors-gray-100: '#fafafa'; |
| 55 | + --chakra-colors-gray-200: '#f7f7f7'; |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +> Note: **The generated custom properties are prefixed with `chakra-*`** to |
| 60 | +> avoid conflicts with third party CSS. |
| 61 | +
|
| 62 | +## Consuming CSS Variables |
| 63 | + |
| 64 | +When using Chakra's components, we manage the conversion of theme tokens to |
| 65 | +their respective CSS variable. |
| 66 | + |
| 67 | +```jsx live=false |
| 68 | +// You type this |
| 69 | +<c-box color="gray.100" /> |
| 70 | + |
| 71 | +// Chakra generates something like |
| 72 | +.css-box { |
| 73 | + color: "var(--chakra-colors-gray-100)" |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +> Note: We do this automatically when you pass style props or use the `sx` prop. |
| 78 | +
|
| 79 | +### Styling non-chakra components |
| 80 | + |
| 81 | +In certain scenarios, you might need to style components that are not managed by |
| 82 | +Chakra. In this case, you can use the raw CSS variable values. |
| 83 | + |
| 84 | +```jsx live=false |
| 85 | +// let's say you have an embedded form |
| 86 | +<FormiumForm /> |
| 87 | +``` |
| 88 | + |
| 89 | +You can write custom CSS to style the components |
| 90 | + |
| 91 | +```css |
| 92 | +.formium-form { |
| 93 | + color: var(--chakra-colors-gray-700); |
| 94 | + background-color: var(--chakra-colors-gray-50); |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +or wrap the component in `<c-box/>` and style it with convenience. |
| 99 | + |
| 100 | +```jsx live=false |
| 101 | +<c-box :sx="{ '.formium': { bg: 'gray.50', color: 'gray.700' } }"> |
| 102 | + <FormiumForm /> |
| 103 | +</c-box> |
| 104 | +``` |
| 105 | + |
| 106 | +## Attaching the CSS variables |
| 107 | + |
| 108 | +By default, Chakra attaches the generated CSS variables to the `:root` element. |
| 109 | + |
| 110 | +> The :root pseudo-class represents the top level of the HTML document. |
| 111 | +
|
| 112 | +To change the root element that Chakra attaches the CSS variables to, pass the |
| 113 | +`cssVarsRoot` prop to `ChakraProvider` and set its value to the CSS selector of |
| 114 | +the element you want. |
| 115 | + |
| 116 | +```jsx live=false |
| 117 | +<ChakraProvider cssVarsRoot='#app'> |
| 118 | + <App /> |
| 119 | +</ChakraProvider> |
| 120 | +``` |
| 121 | + |
| 122 | +## Creating scoped, theme-aware CSS variables |
| 123 | + |
| 124 | +> This is experimental and might be removed in future versions |
| 125 | +
|
| 126 | +When using the `sx` prop or the `chakra(...)` factory, you can create variables |
| 127 | +that reference theme tokens. This makes it possible to change property values |
| 128 | +based on breakpoint, or light/dark mode with ease. |
| 129 | + |
| 130 | +```jsx live=false |
| 131 | +<c-box |
| 132 | + :sx="{ |
| 133 | + // "colors.gray.100" is shorthand for `var(--chakra-colors-gray.100)` |
| 134 | + '--banner-color': 'colors.gray.100', |
| 135 | + '.external-banner': { |
| 136 | + bg: 'var(--banner-color)', |
| 137 | + '.button': { |
| 138 | + borderColor: 'var(--banner-color)', |
| 139 | + }, |
| 140 | + }, |
| 141 | + }" |
| 142 | +> |
| 143 | + <ExternalBanner /> |
| 144 | +</c-box> |
| 145 | +``` |
0 commit comments