This repository was archived by the owner on Sep 20, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +50
-2
lines changed Expand file tree Collapse file tree 3 files changed +50
-2
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,19 @@ import {
8
8
import type * as NuxtSchema from "@nuxt/schema"
9
9
import { ChakraPluginOptions } from "@chakra-ui/vue-next"
10
10
11
+ /** Chakra UI Vue SSR Context State */
12
+ export interface ChakraUISSRContext {
13
+ theme ?: {
14
+ ssrColorMode : "light" | "dark"
15
+ }
16
+ }
17
+
18
+ declare global {
19
+ interface Window {
20
+ $chakraSSRContext : ChakraUISSRContext
21
+ }
22
+ }
23
+
11
24
export default defineNuxtModule < ChakraPluginOptions > ( {
12
25
meta : {
13
26
name : "@chakra-ui/nuxt-next" ,
Original file line number Diff line number Diff line change @@ -35,6 +35,15 @@ export default defineNitroPlugin((app) => {
35
35
html . htmlAttrs . push ( `data-theme="${ colorMode } "` )
36
36
html . htmlAttrs . push ( `style="color-scheme: ${ colorMode } ;"` )
37
37
html . htmlAttrs . push ( `data-chakra-ui-ssr="true"` )
38
+ html . head . push (
39
+ `<script data-chakra-ui-ssr-context>window.$chakraSSRContext=${ JSON . stringify (
40
+ {
41
+ theme : {
42
+ ssrColorMode : colorMode ,
43
+ } ,
44
+ }
45
+ ) } </script>`
46
+ )
38
47
}
39
48
} )
40
49
} )
Original file line number Diff line number Diff line change @@ -2,17 +2,36 @@ import type * as NuxtAppTypes from "nuxt/app"
2
2
import { defineNuxtPlugin } from "#imports"
3
3
import Chakra , {
4
4
chakra ,
5
- ChakraPluginOptions ,
6
5
cookieStorageManagerSSR ,
7
6
extendChakra ,
8
7
ColorModeConstants ,
8
+ extendTheme ,
9
+ ColorModeScriptProps ,
9
10
} from "@chakra-ui/vue-next"
10
11
import { domElements } from "@chakra-ui/vue-system"
12
+ import { parseCookies } from "h3"
13
+
14
+ type AllowedSSRColorMode = Exclude <
15
+ ColorModeScriptProps [ "initialColorMode" ] ,
16
+ "system"
17
+ >
11
18
12
19
export default defineNuxtPlugin ( ( nuxtApp ) => {
13
- console . log ( "chakra-ui-nuxt:plugin_context" )
20
+ let ssrColorMode : AllowedSSRColorMode
21
+ const event = nuxtApp . ssrContext ?. event
22
+ if ( event ) {
23
+ const parsedCookies = parseCookies ( event )
24
+ const colorMode = parsedCookies [ ColorModeConstants . CookieStorageKey ]
25
+ if ( colorMode ) {
26
+ ssrColorMode = colorMode as AllowedSSRColorMode
27
+ } else {
28
+ // TODO: Replace with options color mode
29
+ ssrColorMode = "light"
30
+ }
31
+ }
14
32
15
33
const app = nuxtApp . vueApp
34
+ const isBrowser = typeof document !== "undefined"
16
35
17
36
app . use (
18
37
Chakra ,
@@ -21,6 +40,13 @@ export default defineNuxtPlugin((nuxtApp) => {
21
40
colorModeManager : cookieStorageManagerSSR (
22
41
ColorModeConstants . CookieStorageKey
23
42
) ,
43
+ extendTheme : extendTheme ( {
44
+ config : {
45
+ initialColorMode : isBrowser
46
+ ? window . $chakraSSRContext ?. theme ?. ssrColorMode
47
+ : ssrColorMode ,
48
+ } ,
49
+ } ) ,
24
50
} )
25
51
)
26
52
You can’t perform that action at this time.
0 commit comments