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
{{ message }}
This repository was archived by the owner on Jan 23, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: content/docs/styled-system/color-mode.mdx
+80-35Lines changed: 80 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,33 +10,42 @@ Chakra UI comes with built-in support for managing color mode in your apps.
10
10
By default, most of Chakra's components are dark mode compatible. In some
11
11
scenarios, you might need to make your component respond to color mode.
12
12
13
-
> **Tip:** Chakra stores the color mode in `localStorage` and appends a
14
-
> className to the `body` to ensure the color mode is persistent.
13
+
> **Tip:** Chakra stores the color mode in `localStorage` or in `cookies` and appends a
14
+
> className to the `body` to ensure the color mode is persistent.
15
+
> In case you need to reset the color mode, you must delete the item from `localStorage` or `cookies`, so on next page load the value is initialized like the first time user visited the page.
16
+
17
+
## Introduction to Color Mode
18
+
19
+
In the recent years, operating systems (`system` for this guide) give user the option to choose from Light or Dark color mode.
20
+
Some operating systems include also an automatic option that toggle color mode based on daylight, (day = light, night = dark).
21
+
22
+
Browsers can access this value provided by the operating system, and subscribe to the changes.
23
+
24
+
With Chakra UI, you can customize the behavior of color mode.
25
+
26
+
You can decide:
27
+
- Where to store the current value, choosing from `localStorage`, `cookies`, or custom.
28
+
- If the application color mode changes based on the system's color mode.
29
+
- Whether the initial value (used on the first visit or after storage reset) is decided by the `system` or the developer.
15
30
16
31
## Setup
17
32
18
33
To get dark mode working correctly, you need to do two things:
19
34
20
-
1. Update your theme config to determine how Chakra should manage color mode
35
+
1. Update your theme config to determine how Chakra UI should manage color mode
21
36
updates.
22
37
23
38
2. Add the `ColorModeScript` to your application, and set the initial color mode
24
39
your application should start with to either `light`, `dark` or `system`. It
25
40
is `light` by default.
26
41
27
-
> **Note:** When using `system` as initial color mode, the theme will change
28
-
> with the system preference. However, if another theme is manually selected by
29
-
> the user then that theme will be used on the next page load. To reset it to
30
-
> system preference, simply remove the `chakra-ui-color-mode` entry from
31
-
> localStorage.
32
-
33
42
### Updating the theme config
34
43
35
44
The theme config for color mode has 2 options:
36
45
37
-
-`initialColorMode`: The initial mode you'd like your app to start with
38
-
-`useSystemColorMode`: If `true`, your app will change color mode based on the
39
-
user's system preferences.
46
+
-`initialColorMode`: The initial mode you'd like your app to start with when user visit the page for first time (or after storage reset). Can be one of `dark`, `light` or `system`. Default is `light`.
47
+
48
+
-`useSystemColorMode`: If `true`, Chakra UI subscribes to changes in `system` color mode. If set to `false`, the app's color mode is detached from the `system` color mode. Default is `false`.
40
49
41
50
```jsx live=false
42
51
// theme.js
@@ -80,36 +89,72 @@ export default theme
80
89
> Remember to pass your custom `theme` to the `ChakraProvider`, otherwise your
81
90
> color mode config won't be taken into consideration.
82
91
83
-
#### Behavior of ColorMode
92
+
#### Common Configurations
93
+
94
+
To help you define your desired behavior, here we define all common usage of the theme config.
95
+
In alternative, you can use this [playground](https://codesandbox.io/s/chakra-ui-color-mode-test-f5fcwr?file=/src/chakra-ui/chakra-ui.custom-theme.ts) to try different values.
96
+
97
+
> **Note** The hook `useColorMode` let you update color mode of your app in every cases. [Learn more](#changing-color-mode)
98
+
99
+
```tsx live=false
100
+
// A.
101
+
// System sets initial value.
102
+
// App subscribes to system color mode changes.
103
+
const config:ThemeConfig= {
104
+
initialColorMode: 'system',
105
+
useSystemColorMode: true,
106
+
}
84
107
85
-
The current hierarchy of how the color mode is defined is as follows:
108
+
// B.
109
+
// System sets initial value.
110
+
// App color mode is detached from system color mode changes.
111
+
const config:ThemeConfig= {
112
+
initialColorMode: 'system',
113
+
useSystemColorMode: false,
114
+
}
86
115
87
-
- if `useSystemColorMode` is true `system`-color will be used as default -
88
-
`initialColorMode` is the fallback if system color mode can't be resolved
116
+
// C.
117
+
// You choose initial value.
118
+
// App color mode is detached from system color mode changes.
119
+
const config:ThemeConfig= {
120
+
initialColorMode: 'dark', // 'dark' | 'light'
121
+
useSystemColorMode: false,
122
+
}
123
+
124
+
// D.
125
+
// You choose initial value.
126
+
// App subscribes to system color mode changes.
127
+
const config:ThemeConfig= {
128
+
initialColorMode: 'dark', // 'dark' | 'light'
129
+
useSystemColorMode: true,
130
+
}
131
+
```
132
+
133
+
#### Behavior of ColorMode
89
134
90
-
- if `data-theme` prop is defined through e.g. `ColorModeScript` / after
91
-
modification/initial load of the colorMode this value will be used
135
+
The current hierarchy of how Chakra UI resolves the color mode:
92
136
93
-
- if `colorModeManager` = `localStorage` and a value is defined for
94
-
`chakra-ui-color-mode` this will be used
137
+
- Get the color mode value in the specified storage (localStorage, cookie manager or custom)
95
138
96
-
- if `initialColorMode` = `system` system-color will be used as default -
97
-
`initialColorMode` is the fallback if system color mode isn't resolved
139
+
- If value doesn't exist, use the `initialColorMode` value specified.
140
+
- If the initial value is `system`, then we'll compute the color mode using
141
+
`matchMedia`
142
+
- Else, we use the initial value as is.
98
143
99
-
- if `initialColorMode` = `'light'|'dark'` the corresponding value will be used
144
+
- If user specifies `useSystemColorMode: true`, then we'll subscribe to color
145
+
mode changes from the operating system. It is no longer used to determine the
146
+
initial color mode. To achieve that, please use `initialColorMode: "system"`
100
147
101
-
We currently accept 3 different values for `initialColorMode`:
102
-
`light`,`dark`,`system`
148
+
> **Tip:**
149
+
> In case you need to reset the color mode, you must delete the item in the specified storage (localStorage, cookie manager or custom), so on next page load the value is initialized like the first time user visited the page.
103
150
104
151
#### Difference between `initialColorMode='system'` and `useSystemColorMode`
105
152
106
-
if `useSystemColorMode=true` we will always try to match the users
107
-
`system`-color and fallback to `initialColorMode`. With this behavior, the
108
-
colorMode toggle won't have any effect.
153
+
`initialColorMode` value is used to determine the value on first visit, or after a storage reset.
109
154
110
-
if `initialColorMode='system'` we will as well always try to match the users
111
-
`system`-color and fallback to `light`. After the user has toggled the value,
112
-
this value will be used.
155
+
`useSystemColorMode` is a boolean that indicates if Chakra UI must subscribes (and updates) based on the operating system's color mode changes.
156
+
If `useSystemColorMode=true` and operating system changes from `light` to `dark`, due to a manual or automatic switch, the page automatically changes color mode, without user interaction.
157
+
If `useSystemColorMode=false` color mode can only be changed via the `useColorMode` hook.
113
158
114
159
### Adding the `ColorModeScript`
115
160
@@ -180,7 +225,7 @@ Install `@chakra-ui/gatsby-plugin` in your project. You can read more in the
180
225
181
226
## Changing Color Mode
182
227
183
-
To manage color mode in your application, chakra exposes the `useColorMode` or
228
+
To manage color mode in your application, Chakra UI exposes the `useColorMode` or
184
229
`useColorModeValue` hooks.
185
230
186
231
### useColorMode
@@ -242,7 +287,7 @@ function StyleColorMode() {
242
287
243
288
## Forcing a specific mode
244
289
245
-
In some occasions, you might want Chakra components to look the same in both
290
+
In some occasions, you might want Chakra UI components to look the same in both
246
291
light and dark modes. To achieve this, wrap the component in a `LightMode` or
247
292
`DarkMode` component. Doing this will override the global `colorMode`.
248
293
@@ -281,7 +326,7 @@ preference of a user upfront so you can avoid rendering the initial color mode
281
326
and then changing it during hydration (so-called `flashing`).
282
327
283
328
If you don't use SSR or don't care about this, you don't need to pass anything.
284
-
Chakra will use `localStorageManager` by default.
329
+
Chakra UIwill use `localStorageManager` by default.
285
330
286
331
Here's how to do this in Next.js 9.3 or newer:
287
332
@@ -322,7 +367,7 @@ export function getServerSideProps({ req }) {
322
367
}
323
368
```
324
369
325
-
2. Import your wrapper component setting up Chakra:
370
+
2. Import your wrapper component setting up Chakra UI:
326
371
327
372
```jsx live=false
328
373
// setup your wrapper in the _app file (e.g: pages/_app.js)
0 commit comments