You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All colors found in `colors.modes` will be referenced by their key in the
37
-
context object `rawColors.modes`. The above example will have two modes: `light`
38
-
and `dark`.
36
+
In your components, using e.g. `sx={{ color: 'text' }}` will automatically pick up the current color mode, with no adaptation necessary, even if you add more color modes later.
39
37
40
38
## Initial colors
41
39
@@ -46,101 +44,7 @@ color mode changes change. This is to allow for reference like
46
44
-`colors.primary`, when the color mode is set to its initial mode
47
45
-`colors.modes.dark.primary`, when the color mode is set to `dark`
48
46
49
-
Because of this, we store those initial colors with the other modes. They will
50
-
be accessible as
51
-
52
-
-`rawColors.modes.__default` (if `initialColorModeName` is undefined)
53
-
-`rawColors.modes.light` (using the value of `initialColorModeName`).
54
-
55
-
```js
56
-
{
57
-
config: {
58
-
initialColorModeName:'light',
59
-
}
60
-
rawColors: {
61
-
primary:'#07c',
62
-
modes: {
63
-
// __default: {}, no '__default' here as initialColorModeName is defined
64
-
light: { primary:'#07c' },
65
-
dark: { primary:'#0cf' }
66
-
}
67
-
}
68
-
}
69
-
```
70
-
71
-
## Colors Object
72
-
73
-
The `colors` object contains Custom CSS Properties
74
-
75
-
```js
76
-
{
77
-
colors: {
78
-
text:'var(--theme-ui-colors-text)',
79
-
background:'var(--theme-ui-colors-background)',
80
-
primary:'var(--theme-ui-colors-primary)',
81
-
}
82
-
}
83
-
```
84
-
85
-
## rawColors Object
86
-
87
-
If you need to pass original value somewhere where CSS Properties (e.g. WebGL
88
-
Canvas) won't work, use `rawColors`
89
-
90
-
```js
91
-
{
92
-
rawColors: {
93
-
text:'#000',
94
-
background:'#fff',
95
-
primary:'#07c',
96
-
}
97
-
}
98
-
```
99
-
100
-
### Use specific modes
101
-
102
-
#### With the `sx` prop
103
-
104
-
```jsx
105
-
exportdefault (props) => (
106
-
<div
107
-
sx={(theme) => ({
108
-
color:theme.rawColors.modes?.dark?.text
109
-
bg:theme.rawColors.modes?.dark?.bg
110
-
})}
111
-
/>
112
-
)
113
-
114
-
```
115
-
116
-
#### With Theme UI context
117
-
118
-
Use the [`useThemeUI`](/hooks#usethemeui) hook to access the context object
By default, this works by setting CSS Custom Properties for each theme color, then when the color mode is changed, updating the properties. This makes color modes SSR-safe, since the generated CSS for your components doesn’t rely on knowing the user’s color mode to render. See below how to access the raw color values or disable the use off Custom Properties.
144
48
145
49
## Setting the color mode
146
50
@@ -167,9 +71,11 @@ export default (props) => {
167
71
}
168
72
```
169
73
170
-
## Applying colors
74
+
See our [guide to color mode toggles](/guides/color-mode-toggles) for more.
75
+
76
+
## Applying page-wide colors
171
77
172
-
The ThemeProvider component will automatically apply color mode styles to the
78
+
The `ThemeProvider` component will automatically apply color mode styles (by setting `color` and `background-color`) to the
173
79
`<html>` element.
174
80
175
81
```jsx
@@ -181,62 +87,26 @@ export default (props) => (
181
87
)
182
88
```
183
89
184
-
- To disable this behavior, add the `useRootStyles:false` flag to your theme.
90
+
To disable this behavior, add the `useRootStyles: false` flag to your theme.
185
91
186
-
## Gatsby plugin
92
+
If you’d like to apply your theme color to the browser, [see our guide to the theme color meta tag](/guides/theme-color-meta-tag).
93
+
94
+
### Gatsby plugin
187
95
188
96
For use in a Gatsby site, install and use `gatsby-plugin-theme-ui` to add the
189
97
ThemeProvider to the root of your application. The plugin will also help prevent
190
98
the flash of colors that can happen during page load when a user has a
191
99
non-default color mode set.
192
100
193
-
```sh
194
-
npm i gatsby-plugin-theme-ui
195
-
```
196
-
197
101
This plugin will look for a `src/gatsby-plugin-theme-ui/index.js` file to import
198
102
and pass to the ThemeProvider.
199
103
200
-
```js filename=gatsby-config.js
201
-
module.exports= {
202
-
plugins: ['gatsby-plugin-theme-ui'],
203
-
}
204
-
```
205
-
206
-
See the [Gatsby plugin docs](/packages/gatsby-plugin) for more info.
104
+
See the [Gatsby plugin docs](/packages/gatsby-plugin) for more info & examples.
207
105
208
-
## Advanced
106
+
## Configuration
209
107
210
108
Theme UI includes a few advanced configuration options for color modes.
under the hood to help prevent the flash of color on load. If you’re targeting
216
-
browsers that don't support custom properties you can turn off this setting.
217
-
This will cause the colors to flash on initial page load.
218
-
219
-
```js
220
-
// example theme colors
221
-
{
222
-
config: {
223
-
useCustomProperties:false,
224
-
},
225
-
colors: {
226
-
text:'#000',
227
-
background:'#fff',
228
-
primary:'#07c',
229
-
modes: {
230
-
dark: {
231
-
text:'#fff',
232
-
background:'#000',
233
-
primary:'#0cf',
234
-
}
235
-
}
236
-
}
237
-
}
238
-
```
239
-
240
110
### Responding to the `prefers-color-scheme` media query
241
111
242
112
The `useColorSchemeMediaQuery` option on the theme configuration initializes a
@@ -264,9 +134,7 @@ enabled by default.
264
134
}
265
135
```
266
136
267
-
- To enable the color mode to update when a user's current
268
-
`prefers-color-scheme` media query value changes, set
269
-
`useColorSchemeMediaQuery` to `'system'`.
137
+
To enable the color mode to update when a user's current `prefers-color-scheme` media query value changes, set `useColorSchemeMediaQuery` to `'system'`.
270
138
271
139
### Disable persisting color mode on `localStorage`
272
140
@@ -294,9 +162,11 @@ configuration.
294
162
### Set a custom color mode for printing
295
163
296
164
By default, when printing a webpage, browsers will use the current color mode
297
-
enabled. (This means if a user is currently using a dark or colored-background
298
-
mode, their printed page will share that styling). If you’d like to set a color
299
-
mode to be used on printing, set that color mode with the configuration option
165
+
enabled. This means if a user is currently using a dark or colored-background
166
+
mode, their printed page will share that styling.
167
+
168
+
If you’d like to set a color
169
+
mode to be used on printing, such as light mode, set that color mode with the configuration option
300
170
`printColorModeName`, set to one of your `colors.modes` names, the
301
171
`initialColorModeName` value, or the string `'initial'`.
302
172
@@ -321,3 +191,113 @@ no additional client-side JavaScript for printing.
0 commit comments