-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs(styled-system): color mode #33
base: main
Are you sure you want to change the base?
Changes from 4 commits
30a4898
59db92f
d50bc9b
91cfafa
1f59954
234a11d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,210 @@ | ||||||
--- | ||||||
title: 'Color Mode' | ||||||
description: Working with color mode (light and dark mode) in Chakra UI | ||||||
category: 'features' | ||||||
--- | ||||||
|
||||||
# Color Mode | ||||||
|
||||||
Chakra UI Vue 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` 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, operating systems (`system` for this guide) give user the option to choose from Light or Dark color mode. | ||||||
Some operating systems include also an automatic option that toggle color mode based on daylight, (day = light, night = dark). | ||||||
|
||||||
Browsers can access this value provided by the operating system, and subscribe to the changes. | ||||||
|
||||||
With Chakra UI Vue, you can customize the behavior of color mode. | ||||||
|
||||||
You can decide: | ||||||
- Where to store the current value, choosing from `localStorage`, `cookies`, or custom. | ||||||
- If the application color mode changes based on the system's color mode. | ||||||
- Whether the initial value (used on the first visit or after storage reset) is decided by the `system` or the developer. | ||||||
|
||||||
## Setup | ||||||
|
||||||
To get color mode working correctly, you need to do two things: | ||||||
|
||||||
1. Update your theme config to determine how Chakra UI should manage color mode | ||||||
updates. | ||||||
|
||||||
2. Add the `ColorModeScript` to your application, and set the initial color mode | ||||||
your application should start with to either `light`, `dark` or `system`. It | ||||||
is `light` by default. | ||||||
|
||||||
### 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 when user visit the page for first time (or after storage reset). Can be one of `dark`, `light` or `system`. Default is `light`. | ||||||
|
||||||
- `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`. | ||||||
|
||||||
```js | ||||||
// theme.js | ||||||
|
||||||
// 1. import `extendTheme` function | ||||||
import { extendTheme } from '@chakra-ui/vue-next' | ||||||
|
||||||
// 2. Add your color mode config | ||||||
const config = { | ||||||
initialColorMode: 'light', | ||||||
useSystemColorMode: false, | ||||||
} | ||||||
|
||||||
// 3. extend the theme | ||||||
const theme = extendTheme({ config }) | ||||||
|
||||||
export default theme | ||||||
``` | ||||||
|
||||||
> Remember to pass your custom `theme` in the `ChakraPlugin` options, otherwise your | ||||||
> color mode config won't be taken into consideration. | ||||||
|
||||||
#### Common Configurations | ||||||
|
||||||
To help you define your desired behavior, here we define all common usage of the theme config. | ||||||
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. | ||||||
|
||||||
> **Note** The `useColorMode` composable let you update color mode of your app in every cases. [Learn more](#changing-color-mode). | ||||||
|
||||||
```js | ||||||
// A. | ||||||
// System sets initial value. | ||||||
// App subscribes to system color mode changes. | ||||||
const config = { | ||||||
initialColorMode: 'system', | ||||||
useSystemColorMode: true, | ||||||
} | ||||||
|
||||||
// B. | ||||||
// System sets initial value. | ||||||
// App color mode is detached from system color mode changes. | ||||||
const config = { | ||||||
initialColorMode: 'system', | ||||||
useSystemColorMode: false, | ||||||
} | ||||||
|
||||||
// C. | ||||||
// You choose initial value. | ||||||
// App color mode is detached from system color mode changes. | ||||||
const config = { | ||||||
initialColorMode: 'dark', // 'dark' | 'light' | ||||||
useSystemColorMode: false, | ||||||
} | ||||||
|
||||||
// D. | ||||||
// You choose initial value. | ||||||
// App subscribes to system color mode changes. | ||||||
const config = { | ||||||
initialColorMode: 'dark', // 'dark' | 'light' | ||||||
useSystemColorMode: true, | ||||||
} | ||||||
``` | ||||||
|
||||||
#### Behavior of ColorMode | ||||||
|
||||||
The current hierarchy of how Chakra UI Vue resolves the color mode: | ||||||
|
||||||
- Get the color mode value in the specified storage (localStorage, cookie manager or custom) | ||||||
|
||||||
- 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 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"` | ||||||
|
||||||
> **Tip:** | ||||||
> 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. | ||||||
|
||||||
#### Difference between `initialColorMode='system'` and `useSystemColorMode` | ||||||
|
||||||
`initialColorMode` value is used to determine the value on first visit, or after a storage reset. | ||||||
|
||||||
`useSystemColorMode` is a boolean that indicates if Chakra UI must subscribes (and updates) based on the operating system's color mode changes. | ||||||
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. | ||||||
If `useSystemColorMode=false` color mode can only be changed via the `useColorMode` composable. | ||||||
|
||||||
### Adding the `ColorModeScript` | ||||||
|
||||||
The color mode script needs to be added before the content inside the `body` tag | ||||||
for local storage syncing to work correctly. | ||||||
|
||||||
This script will be added automatically by Chakra UI Vue. | ||||||
|
||||||
> To use `ColorModeScript` on a site with strict `Content-Security-Policy`, you | ||||||
> can use the `nonce` prop that will be passed to the `<script>` tag. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's remove this section. The user doesn't need to know about it. 👍🏽 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated |
||||||
|
||||||
## Changing Color Mode | ||||||
|
||||||
To manage color mode in your application, Chakra UI Vue exposes the `useColorMode` or | ||||||
`useColorModeValue` composables. | ||||||
|
||||||
### useColorMode | ||||||
|
||||||
`useColorMode` is a composable that gives you access to the current color mode, | ||||||
and a function to toggle the color mode. | ||||||
|
||||||
```js | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
<script setup lang="ts"> | ||||||
import { useColorMode } from "@chakra-ui/vue-next" | ||||||
|
||||||
const { colorMode, toggleColorMode } = useColorMode() | ||||||
</script> | ||||||
|
||||||
<template> | ||||||
<CButton @click="toggleColorMode"> | ||||||
Toggle {{ colorMode === "light" ? "Dark" : "Light" }} | ||||||
</CButton> | ||||||
</template> | ||||||
``` | ||||||
|
||||||
Calling `toggleColorMode` anywhere in your app tree toggles the color mode from | ||||||
`light` or `dark` and vice versa. | ||||||
|
||||||
### useColorModeValue | ||||||
|
||||||
`useColorModeValue` is a composable used to change any value or style based on | ||||||
the color mode. It takes 2 arguments: the value in light mode, and the value in | ||||||
dark mode. | ||||||
|
||||||
```js | ||||||
import { useColorModeValue } from "@chakra-ui/vue-next" | ||||||
|
||||||
// Here's the signature | ||||||
const value = useColorModeValue(lightModeValue, darkModeValue) | ||||||
``` | ||||||
|
||||||
Here's an example that changes the `background-color` and `color` using the | ||||||
`useColorModeValue` composable. | ||||||
|
||||||
```js | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Or html works |
||||||
<script setup lang="ts"> | ||||||
import { useColorMode, useColorModeValue } from "@chakra-ui/vue-next" | ||||||
|
||||||
const { toggleColorMode } = useColorMode() | ||||||
|
||||||
const bg = useColorModeValue('red.500', 'red.200') | ||||||
const color = useColorModeValue('white', 'gray.800') | ||||||
</script> | ||||||
|
||||||
<template> | ||||||
<CBox mb="4" :bg="bg" :color="color"> | ||||||
This box's style will change based on the color mode. | ||||||
</CBox> | ||||||
<CButton @click="toggleColorMode"> | ||||||
Toggle Mode | ||||||
</CButton> | ||||||
</template> | ||||||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is better since it's not React ?