-
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
Open
carwack
wants to merge
6
commits into
main
Choose a base branch
from
docs/color-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
30a4898
feat(docs): add color mode docs
carwack 59db92f
refactor(docs): change hooks into composables
carwack d50bc9b
feat(docs): update code examples styling
carwack 91cfafa
Merge branch 'main' into docs/color-mode
codebender828 1f59954
refactor(docs): remove colormodescript section
carwack 234a11d
feat: fix pr comments
carwack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,200 @@ | ||||||
--- | ||||||
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. | ||||||
|
||||||
## 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> | ||||||
``` |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 ?