This repository was archived by the owner on Sep 20, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 9 files changed +82
-12
lines changed Expand file tree Collapse file tree 9 files changed +82
-12
lines changed Original file line number Diff line number Diff line change 1
1
<template >
2
- <div >
3
- <NuxtWelcome />
4
- </div >
2
+ <chakra .div
3
+ display =" flex"
4
+ min-h =" 600px"
5
+ justify-content =" center"
6
+ align-items =" center"
7
+ >
8
+ <chakra .button
9
+ @click =" toggleColorMode"
10
+ px =" 4"
11
+ py =" 3"
12
+ bg =" red"
13
+ color =" white"
14
+ font-weight =" bold"
15
+ >
16
+ Click me to toggle. ({{ colorMode }})
17
+ </chakra .button >
18
+ </chakra .div >
5
19
</template >
20
+
21
+ <script lang="ts" setup>
22
+ import { chakra , useColorMode } from " @chakra-ui/vue-next"
23
+
24
+ const { colorMode, toggleColorMode } = useColorMode ()
25
+ </script >
Original file line number Diff line number Diff line change
1
+ import { extendTheme } from "@chakra-ui/vue-next"
2
+
1
3
export default defineNuxtConfig ( {
2
4
modules : [ "@chakra-ui/nuxt-next" ] ,
5
+ chakra : {
6
+ extendTheme : extendTheme ( {
7
+ colors : {
8
+ $brand : "#f5f" ,
9
+ } ,
10
+ } ) ,
11
+ } ,
3
12
} )
Original file line number Diff line number Diff line change @@ -96,7 +96,22 @@ export function setupColorModeContext(
96
96
97
97
const managerValue = colorModeManager . get ( )
98
98
99
- if ( managerValue ) {
99
+ // If this is SSR, we check to see if the cookie has been specified.
100
+ // Otherwise we should bail.
101
+ if (
102
+ // Is SSR
103
+ typeof document !== "undefined" &&
104
+ document . documentElement . dataset . chakraUiSsr === "true"
105
+ // !initialColorMode
106
+ ) {
107
+ // Bail and
108
+ // Forcefully hydrate client to match ccolor mode
109
+ const ssrColorMode = document . documentElement . dataset . theme
110
+ if ( ssrColorMode ) {
111
+ console . debug ( "chakra-ui-ssr color mode is" , ssrColorMode )
112
+ setColorMode ( ssrColorMode as ColorMode )
113
+ }
114
+ } else if ( managerValue ) {
100
115
setColorMode ( managerValue )
101
116
} else if ( initialColorMode === "system" ) {
102
117
setColorMode ( "system" )
@@ -108,7 +123,7 @@ export function setupColorModeContext(
108
123
109
124
function setColorMode ( value : ColorMode | "system" ) {
110
125
const { setClassName, setDataset, getSystemTheme } = utils . value
111
- const resolved = value === "system" ? getSystemTheme ( ) : value
126
+ const resolved = value === "system" ? getSystemTheme ( ) ! : value
112
127
colorMode . value = resolved
113
128
114
129
setClassName ( resolved === "dark" )
Original file line number Diff line number Diff line change 1
1
export type ColorModeScriptProps = {
2
2
type ?: "localStorage" | "cookie"
3
3
initialColorMode ?: "light" | "dark" | "system"
4
- storageKey ?: string
4
+ storageKey ?: ColorModeConstants
5
5
nonce ?: string
6
6
}
7
7
8
+ export enum ColorModeConstants {
9
+ CookieStorageKey = "chakra-ui-color-mode" ,
10
+ LocalStorageKey = "chakra-ui-color-mode" ,
11
+ BaseStorageKey = "chakra-ui-color-mode" ,
12
+ }
13
+
8
14
const VALID_VALUES = new Set ( [ "dark" , "light" , "system" ] )
9
15
10
16
/**
@@ -20,7 +26,7 @@ export function getScriptSrc(props: ColorModeScriptProps = {}) {
20
26
const {
21
27
initialColorMode = "light" ,
22
28
type = "localStorage" ,
23
- storageKey : key = "chakra-ui-color-mode" ,
29
+ storageKey : key = ColorModeConstants . LocalStorageKey ,
24
30
} = props
25
31
26
32
// runtime safe-guard against invalid color mode values
@@ -41,6 +47,8 @@ export function getScriptSrc(props: ColorModeScriptProps = {}) {
41
47
export function mountColorModeScript ( props : ColorModeScriptProps = { } ) {
42
48
const { nonce } = props
43
49
50
+ if ( typeof document === "undefined" ) return
51
+
44
52
const script = document . createElement ( "script" )
45
53
script . id = "chakra-script"
46
54
script . nonce = nonce
Original file line number Diff line number Diff line change @@ -18,24 +18,29 @@ export function getColorModeUtils(options: UtilOptions = {}) {
18
18
19
19
const utils = {
20
20
setDataset : ( value : ColorMode ) => {
21
+ if ( ! globalThis ?. document ) return
21
22
const cleanup = preventTransition ? utils . preventTransition ( ) : undefined
22
23
document . documentElement . dataset . theme = value
23
24
document . documentElement . style . colorScheme = value
24
25
cleanup ?.( )
25
26
} ,
26
27
setClassName ( dark : boolean ) {
28
+ if ( ! globalThis ?. document ) return
27
29
document . body . classList . add ( dark ? classNames . dark : classNames . light )
28
30
document . body . classList . remove ( dark ? classNames . light : classNames . dark )
29
31
} ,
30
32
query ( ) {
33
+ if ( ! globalThis ?. document ) return
31
34
return window . matchMedia ( "(prefers-color-scheme: dark)" )
32
35
} ,
33
36
getSystemTheme ( fallback ?: ColorMode ) {
34
- const dark = utils . query ( ) . matches ?? fallback === "dark"
37
+ if ( ! globalThis ?. document ) return
38
+ const dark = utils . query ( ) ! . matches ?? fallback === "dark"
35
39
return dark ? "dark" : "light"
36
40
} ,
37
41
addListener ( fn : ( cm : ColorMode ) => unknown ) {
38
42
const mql = utils . query ( )
43
+ if ( ! globalThis ?. document || ! mql ) return
39
44
const listener = ( e : MediaQueryListEvent ) => {
40
45
fn ( e . matches ? "dark" : "light" )
41
46
}
Original file line number Diff line number Diff line change 1
1
export * from "./color-mode-provider"
2
+ export * from "./color-mode-script"
2
3
export * from "./storage-manager"
Original file line number Diff line number Diff line change 1
1
import { __DEV__ } from "@chakra-ui/utils"
2
+ import { ColorModeConstants } from "./color-mode-script"
2
3
import { ColorMode } from "./color-mode.utils"
3
4
4
- export const STORAGE_KEY = "chakra-ui-color-mode"
5
+ export const STORAGE_KEY = ColorModeConstants . BaseStorageKey
5
6
6
7
const hasSupport = ( ) => typeof Storage !== "undefined"
7
- export const storageKey = "chakra-ui-color-mode"
8
8
9
9
type MaybeColorMode = ColorMode | undefined
10
10
@@ -60,6 +60,7 @@ export function createCookieStorageManager(
60
60
return parseCookie ( document . cookie , key ) || init
61
61
} ,
62
62
set ( value ) {
63
+ if ( typeof document === "undefined" ) return
63
64
document . cookie = `${ key } =${ value } ; max-age=31536000; path=/`
64
65
} ,
65
66
}
Original file line number Diff line number Diff line change 1
- ## Project Stats
2
- ![ Alt] ( https://repobeats.axiom.co/api/embed/40b60a8e21b08d97af5849ea7a8c7d7f46956824.svg " Repobeats analytics image ")
1
+ # @chakra-ui/vue-next
2
+
3
+ Chakra UI on Vue 3
4
+
5
+ ## Installation
6
+
7
+ ``` sh
8
+ yarn add @chakra-ui/vue-next
9
+ # or
10
+ npm i @chakra-ui/vue-next
11
+ ```
You can’t perform that action at this time.
0 commit comments