Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit c2d48d3

Browse files
committed
feat(ssr): parse nuxt ssr context
1 parent f53daf9 commit c2d48d3

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

packages/nuxt/src/module.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ import {
88
import type * as NuxtSchema from "@nuxt/schema"
99
import { ChakraPluginOptions } from "@chakra-ui/vue-next"
1010

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+
1124
export default defineNuxtModule<ChakraPluginOptions>({
1225
meta: {
1326
name: "@chakra-ui/nuxt-next",

packages/nuxt/src/runtime/chakra.server.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ export default defineNitroPlugin((app) => {
3535
html.htmlAttrs.push(`data-theme="${colorMode}"`)
3636
html.htmlAttrs.push(`style="color-scheme: ${colorMode};"`)
3737
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+
)
3847
}
3948
})
4049
})

packages/nuxt/src/runtime/chakra.universal.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,36 @@ import type * as NuxtAppTypes from "nuxt/app"
22
import { defineNuxtPlugin } from "#imports"
33
import Chakra, {
44
chakra,
5-
ChakraPluginOptions,
65
cookieStorageManagerSSR,
76
extendChakra,
87
ColorModeConstants,
8+
extendTheme,
9+
ColorModeScriptProps,
910
} from "@chakra-ui/vue-next"
1011
import { domElements } from "@chakra-ui/vue-system"
12+
import { parseCookies } from "h3"
13+
14+
type AllowedSSRColorMode = Exclude<
15+
ColorModeScriptProps["initialColorMode"],
16+
"system"
17+
>
1118

1219
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+
}
1432

1533
const app = nuxtApp.vueApp
34+
const isBrowser = typeof document !== "undefined"
1635

1736
app.use(
1837
Chakra,
@@ -21,6 +40,13 @@ export default defineNuxtPlugin((nuxtApp) => {
2140
colorModeManager: cookieStorageManagerSSR(
2241
ColorModeConstants.CookieStorageKey
2342
),
43+
extendTheme: extendTheme({
44+
config: {
45+
initialColorMode: isBrowser
46+
? window.$chakraSSRContext?.theme?.ssrColorMode
47+
: ssrColorMode,
48+
},
49+
}),
2450
})
2551
)
2652

0 commit comments

Comments
 (0)