Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit 8bebe25

Browse files
authored
Merge pull request #1062 from tresorama/docs/color-mode
2 parents 7a3a61c + a62f8ae commit 8bebe25

File tree

2 files changed

+81
-36
lines changed

2 files changed

+81
-36
lines changed

content/docs/styled-system/color-mode.mdx

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,42 @@ Chakra UI comes with built-in support for managing color mode in your apps.
1010
By default, most of Chakra's components are dark mode compatible. In some
1111
scenarios, you might need to make your component respond to color mode.
1212

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.
1530

1631
## Setup
1732

1833
To get dark mode working correctly, you need to do two things:
1934

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
2136
updates.
2237

2338
2. Add the `ColorModeScript` to your application, and set the initial color mode
2439
your application should start with to either `light`, `dark` or `system`. It
2540
is `light` by default.
2641

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-
3342
### Updating the theme config
3443

3544
The theme config for color mode has 2 options:
3645

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`.
4049

4150
```jsx live=false
4251
// theme.js
@@ -80,36 +89,72 @@ export default theme
8089
> Remember to pass your custom `theme` to the `ChakraProvider`, otherwise your
8190
> color mode config won't be taken into consideration.
8291
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+
}
84107

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+
}
86115

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
89134

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:
92136

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)
95138

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.
98143

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"`
100147

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.
103150
104151
#### Difference between `initialColorMode='system'` and `useSystemColorMode`
105152

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.
109154

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.
113158

114159
### Adding the `ColorModeScript`
115160

@@ -180,7 +225,7 @@ Install `@chakra-ui/gatsby-plugin` in your project. You can read more in the
180225

181226
## Changing Color Mode
182227

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
184229
`useColorModeValue` hooks.
185230

186231
### useColorMode
@@ -242,7 +287,7 @@ function StyleColorMode() {
242287
243288
## Forcing a specific mode
244289
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
246291
light and dark modes. To achieve this, wrap the component in a `LightMode` or
247292
`DarkMode` component. Doing this will override the global `colorMode`.
248293
@@ -281,7 +326,7 @@ preference of a user upfront so you can avoid rendering the initial color mode
281326
and then changing it during hydration (so-called `flashing`).
282327
283328
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 UI will use `localStorageManager` by default.
285330

286331
Here's how to do this in Next.js 9.3 or newer:
287332
@@ -322,7 +367,7 @@ export function getServerSideProps({ req }) {
322367
}
323368
```
324369
325-
2. Import your wrapper component setting up Chakra:
370+
2. Import your wrapper component setting up Chakra UI:
326371
327372
```jsx live=false
328373
// setup your wrapper in the _app file (e.g: pages/_app.js)

content/docs/styled-system/theme.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ parts of the Chakra UI system.
418418
| Property | Description | Default |
419419
| ------------------------- | ----------------------------------------------------------------------------------------------------------- | -------- |
420420
| `cssVarPrefix` **(v1.4)** | The prefix to use for the generated CSS custom properties | `chakra` |
421-
| `initialColorMode` | The initial color mode your application should start with. <br reset/> Can be either `light` or `dark` mode | `light` |
421+
| `initialColorMode` | The initial color mode your application should start with. <br reset/> Can be `light`, `dark` or `system` | `light` |
422422
| `useSystemColorMode` | If `true`, the chakra system will update color mode <br reset/> based on your system preferences | `false` |
423423

424424
You can leverage the `extendTheme` function to override a specific theme config

0 commit comments

Comments
 (0)