|
| 1 | +# Color Mode |
| 2 | + |
| 3 | +Most @chakra-ui/vue components are dark mode compatible. You can also toggle between dark |
| 4 | +and light modes in your chakra applications. |
| 5 | + |
| 6 | + |
| 7 | +We added some new helpers to the Chakra Color Mode to |
| 8 | +simplify the access of the current color mode and the toggle color mode functions. |
| 9 | + |
| 10 | +## Simplified Accessing Color Mode |
| 11 | + |
| 12 | +To enable toggling between Dark and Light modes within your apps, wrap your application in a |
| 13 | +`CColorModeProvider` component. Chakra UI globally provides computed variables for the current color mode: |
| 14 | +- `this.chakraColorMode` - The current mode. |
| 15 | +- `this.chakraToggleColorMode` - Function to toggle the Chakra UI Color mode |
| 16 | + |
| 17 | +Try toggling the current colorMode |
| 18 | + |
| 19 | +```vue live=true |
| 20 | +<template> |
| 21 | + <c-button @click="chakraToggleColorMode"> |
| 22 | + Chakra ColorMode: {{ chakraColorMode }} |
| 23 | + </c-button> |
| 24 | +</template> |
| 25 | +``` |
| 26 | + |
| 27 | + |
| 28 | +### Injecting color mode and toggle helpers |
| 29 | +The `CColorModeProvider` component also provides two variables in it's descendant's context using the |
| 30 | +[provide/inject](https://vuejs.org/v2/api/#provide-inject) API: |
| 31 | +- `$chakraColorMode` => This is a function that returns the current mode. Use it the computed property. |
| 32 | +- `$toggleColorMode` => This function toggles the current color mode value. |
| 33 | + |
| 34 | +Below is an example of how to use the above variables: |
| 35 | + |
| 36 | +In your main.js |
| 37 | +```js |
| 38 | +import Vue from 'vue' |
| 39 | +import Chakra, { CThemeProvider, CColorModeProvider, CReset } from '@chakra-ui/vue' |
| 40 | +import App from './App.vue' |
| 41 | + |
| 42 | +Vue.use(Chakra) |
| 43 | + |
| 44 | +new Vue({ |
| 45 | + el: '#app', |
| 46 | + render: (h) => h(CThemeProvider, [ |
| 47 | + h(CColorModeProvider, [ |
| 48 | + h(CReset), |
| 49 | + h(App) |
| 50 | + ]) |
| 51 | + ]) |
| 52 | +}).$mount() |
| 53 | +``` |
| 54 | + |
| 55 | +In any component that needs the Chakra color mode, you can now access it by injecting it into that |
| 56 | +component's state: |
| 57 | + |
| 58 | +_In your `App.vue` file._ |
| 59 | + |
| 60 | +```vue live=true |
| 61 | +<template> |
| 62 | + <c-box> |
| 63 | + <c-button @click="$toggleColorMode"> |
| 64 | + Chakra ColorMode: {{ colorMode }} |
| 65 | + </c-button> |
| 66 | + </c-box> |
| 67 | +</template> |
| 68 | +
|
| 69 | +<script lang="js"> |
| 70 | +
|
| 71 | +export default { |
| 72 | + name: 'App', |
| 73 | + inject: ['$chakraColorMode', '$toggleColorMode'], |
| 74 | + computed: { |
| 75 | + /** |
| 76 | + * In order to preserve reactivity, Chakra provides the color mode |
| 77 | + * inside the `$chakraColorMode` function. This function returns the current |
| 78 | + * color mode. |
| 79 | + */ |
| 80 | + colorMode () { |
| 81 | + return this.$chakraColorMode() |
| 82 | + } |
| 83 | + }, |
| 84 | +} |
| 85 | +</script> |
| 86 | +``` |
| 87 | + |
| 88 | +## Forcing a specific mode |
| 89 | + |
| 90 | +On some occasions, you might want Chakra components to look the same in both |
| 91 | +light and dark modes. To achieve this, wrap the component in `CLightMode` or |
| 92 | +`CDarkMode` component. Doing this will override the global `$chakraColorMode`. |
| 93 | + |
| 94 | +> Click the **"Toggle Mode"** button to see it in action. |
| 95 | +
|
| 96 | +```vue live=true |
| 97 | +<template> |
| 98 | + <c-stack should-wrap-children is-inline> |
| 99 | + <c-light-mode> |
| 100 | + <c-button size="sm" variant-color="blue"> |
| 101 | + Light Mode Always |
| 102 | + </c-button> |
| 103 | + </c-light-mode> |
| 104 | +
|
| 105 | + <c-dark-mode> |
| 106 | + <c-button size="sm" variant-color="blue"> |
| 107 | + Dark Mode Always |
| 108 | + </c-button> |
| 109 | + </c-dark-mode> |
| 110 | +
|
| 111 | + <c-button size="sm" variant-color="blue" @click="$toggleColorMode"> |
| 112 | + Toggle Color Mode |
| 113 | + </c-button> |
| 114 | + </c-stack> |
| 115 | +</template> |
| 116 | +
|
| 117 | +<script lang="js"> |
| 118 | +
|
| 119 | +export default { |
| 120 | + name: 'App', |
| 121 | + inject: ['$toggleColorMode'] |
| 122 | +} |
| 123 | +</script> |
| 124 | +``` |
| 125 | + |
| 126 | +## Computing values with `mode` utility function |
| 127 | +`@chakra-ui/vue@^0.10.0` exports a `mode` function that accepts two arguments for the `'light'` and `'dark'` mode respectively. |
| 128 | +When the current color mode is `'light'`, it returns the first argument. If the color mode is `'dark'`, it returns the second argument. |
| 129 | + |
| 130 | +```js |
| 131 | +import Vue from 'vue' |
| 132 | +import Chakra, { mode } from '@chakra-ui/vue' |
| 133 | + |
| 134 | +Vue.use(Chakra, { |
| 135 | + extendTheme: { |
| 136 | + baseStyles: { |
| 137 | + CIconButton: () => ({ |
| 138 | + /** |
| 139 | + * When the color mode is `light`, `mode` returns `'blackAlpha.700'`. |
| 140 | + * Otherwise it returns `'whiteAlpha.400'`. |
| 141 | + */ |
| 142 | + color: mode('blackAlpha.700', 'whiteAlpha.400') |
| 143 | + }) |
| 144 | + } |
| 145 | + } |
| 146 | +}) |
| 147 | +``` |
| 148 | + |
| 149 | +### Usage in templates |
| 150 | +The `mode` function is also made globally available inside your Vue application context so you do not have to import it inside your |
| 151 | +Vue SFCs. You can access it by invoking `this.$mode` in the template or in your script. |
| 152 | + |
| 153 | +```vue live=true |
| 154 | +<template> |
| 155 | + <c-box |
| 156 | + :bg="[ |
| 157 | + $mode('orange.600', 'yellow.100'), |
| 158 | + $mode('pink.600', 'green.100') |
| 159 | + ]" |
| 160 | + :color="$mode('white', 'blackAlpha.800')" |
| 161 | + @click="chakraToggleColorMode" |
| 162 | + > |
| 163 | + Box "{{ chakraColorMode }}" mode. Click me to toggle color mode. |
| 164 | + </c-box> |
| 165 | +</template> |
| 166 | +``` |
0 commit comments