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

docs(color-mode): updates docsite to reflect current behavior #1062

Merged
merged 9 commits into from
Dec 30, 2022
109 changes: 79 additions & 30 deletions content/docs/styled-system/color-mode.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@ Chakra UI comes with built-in support for managing color mode in your apps.
By default, most of Chakra's components are dark mode compatible. In some
scenarios, you might need to make your component respond to color mode.

> **Tip:** Chakra stores the color mode in `localStorage` and appends a
> className to the `body` to ensure the color mode is persistent.
> **Tip:** Chakra stores the color mode in `localStorage` or in `cookies` and appends a
> className to the `body` to ensure the color mode is persistent.
> 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.

## Introduction to Color Mode

In the recent years, operative systems (`system` for this guide) give user the option to choose from Light or Dark color mode.
tresorama marked this conversation as resolved.
Show resolved Hide resolved
Some operative systems include also an automatic option that toggle color mode based on daylight, (day = light, night = dark).

Browser can access this value provided by the operative system, and subscribes to the changes.
tresorama marked this conversation as resolved.
Show resolved Hide resolved

With Chakra, you can customize the behavior of color mode.
tresorama marked this conversation as resolved.
Show resolved Hide resolved

You can decide:
- Where to store the current value, choosing from `localStorage`, `cookies`, or custom.
- If a change in the `system` color mode must change the app's color mode, or not.
tresorama marked this conversation as resolved.
Show resolved Hide resolved
- Whether the initial value (used on the first visit or after memory reset) is decided by the `system` or the developer.

## Setup

Expand All @@ -24,19 +39,14 @@ To get dark mode working correctly, you need to do two things:
your application should start with to either `light`, `dark` or `system`. It
is `light` by default.

> **Note:** When using `system` as initial color mode, the theme will change
> with the system preference. However, if another theme is manually selected by
> the user then that theme will be used on the next page load. To reset it to
> system preference, simply remove the `chakra-ui-color-mode` entry from
> localStorage.

### Updating the theme config

The theme config for color mode has 2 options:

- `initialColorMode`: The initial mode you'd like your app to start with
- `useSystemColorMode`: If `true`, your app will change color mode based on the
user's system preferences.
- `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`.

// TODO: useSystemColorMode default is undefined or false ???
- `useSystemColorMode`: If `true`, chakra subscribes to changes in `system` color mode. If set to `false` (or `undefined`), the app's color mode is detached from the `system` color mode. Default is `undefined`.
tresorama marked this conversation as resolved.
Show resolved Hide resolved

```jsx live=false
// theme.js
Expand Down Expand Up @@ -80,36 +90,75 @@ export default theme
> Remember to pass your custom `theme` to the `ChakraProvider`, otherwise your
> color mode config won't be taken into consideration.

#### Behavior of ColorMode
#### Common Configurations

The current hierarchy of how the color mode is defined is as follows:
To help you define you desired behavior, here we define all common usage of the theme config :
tresorama marked this conversation as resolved.
Show resolved Hide resolved

- if `useSystemColorMode` is true `system`-color will be used as default -
`initialColorMode` is the fallback if system color mode can't be resolved
> **Tip** If you prefer, you can use this [playground](https://codesandbox.io/s/chakra-ui-color-mode-test-f5fcwr?file=/src/uiLayer.chakra/customTheme.ts) to try different values.

- if `data-theme` prop is defined through e.g. `ColorModeScript` / after
modification/initial load of the colorMode this value will be used
> **Note** The hook `useColorMode` let you update color mode of your app in every cases. [Learn more](#changing-color-mode)

```tsx live=false
// A.
// System sets initial value.
// App subscribes to system color mode changes.
const config: ThemeConfig = {
initialColorMode: 'system',
useSystemColorMode: true,
}

// B.
// System sets initial value.
// App color mode is detached from system color mode changes.
const config: ThemeConfig = {
initialColorMode: 'system',
useSystemColorMode: false,
}

// C.
// You choose initial value.
// App color mode is detached from system color mode changes.
const config: ThemeConfig = {
initialColorMode: 'dark', // 'dark' | 'light'
useSystemColorMode: false,
}


// D.
// You choose initial value.
// App subscribes to system color mode changes.
const config: ThemeConfig = {
initialColorMode: 'dark', // 'dark' | 'light'
useSystemColorMode: true,
}
```

#### Behavior of ColorMode

The current hierarchy of how chakra resolves the color mode:
tresorama marked this conversation as resolved.
Show resolved Hide resolved

- if `colorModeManager` = `localStorage` and a value is defined for
`chakra-ui-color-mode` this will be used
- Get the color mode value in the specified storage (localStorage or cookie manager or custom)
tresorama marked this conversation as resolved.
Show resolved Hide resolved

- if `initialColorMode` = `system` system-color will be used as default -
`initialColorMode` is the fallback if system color mode isn't resolved
- If value doesn't exist, use the `initialColorMode` value specified.
- If the initial value is `system`, then we'll compute the color mode using
`matchMedia`
- Else, we use the initial value as is.

- if `initialColorMode` = `'light'|'dark'` the corresponding value will be used
- If user specifies `useSystemColorMode: true`, then we'll subscribe to color
mode changes from the operating system. It is no longer used to determine the
initial color mode. To achieve that, please use `initialColorMode: "system"`

We currently accept 3 different values for `initialColorMode`:
`light`,`dark`,`system`
// TODO : Keep this ??? Is still valid ??
- if `data-theme` prop is defined through e.g. `ColorModeScript` / after
tresorama marked this conversation as resolved.
Show resolved Hide resolved
modification/initial load of the colorMode this value will be used

#### Difference between `initialColorMode='system'` and `useSystemColorMode`

if `useSystemColorMode=true` we will always try to match the users
`system`-color and fallback to `initialColorMode`. With this behavior, the
colorMode toggle won't have any effect.
`initialColorMode` value is used to determine the value on first visit, or after a storage reset.

if `initialColorMode='system'` we will as well always try to match the users
`system`-color and fallback to `light`. After the user has toggled the value,
this value will be used.
`useSystemColorMode` is a boolean that indicates if chakra must subscribes (and update) to operative system's color mode changes.
tresorama marked this conversation as resolved.
Show resolved Hide resolved
If `useSystemColorMode=true` and operative system changes from `light` to `dark` due to daylight switch, the page automatically changes color mode, without user interaction.
If `useSystemColorMode=false` color mode can only be changed via the `useColorMode` hook.

### Adding the `ColorModeScript`

Expand Down