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

Commit 29c9a61

Browse files
committed
feat(nuxt): full ssr compatibility
1 parent 00fe68e commit 29c9a61

File tree

4 files changed

+84
-86
lines changed

4 files changed

+84
-86
lines changed

packages/nuxt/src/module.ts

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import {
77
addPluginTemplate,
88
} from "@nuxt/kit"
99
import type * as NuxtSchema from "@nuxt/schema"
10-
import { ChakraPluginOptions } from "@chakra-ui/vue-next"
10+
import {
11+
ChakraPluginOptions,
12+
extendTheme as _extendTheme,
13+
} from "@chakra-ui/vue-next"
14+
import { mergeWith } from "@chakra-ui/utils"
1115

1216
/** Chakra UI Vue SSR Context State */
1317
export interface ChakraUISSRContext {
@@ -22,15 +26,31 @@ declare global {
2226
}
2327
}
2428

25-
export default defineNuxtModule<ChakraPluginOptions>({
29+
export type ChakraModuleOptions = Omit<ChakraPluginOptions, "colorModeManager">
30+
31+
const defaultModuleOptions: ChakraModuleOptions = {
32+
cssReset: true,
33+
isBaseTheme: false,
34+
}
35+
36+
export default defineNuxtModule<ChakraModuleOptions>({
2637
meta: {
2738
name: "@chakra-ui/nuxt-next",
2839
configKey: "chakra",
2940
compatibilty: ">=3.0.0",
3041
},
31-
setup(_options, nuxt) {
42+
setup(__options, nuxt) {
3243
console.log("chakra-ui-nuxt:module")
33-
const theme = _options.extendTheme
44+
const _options = mergeWith(
45+
defaultModuleOptions,
46+
__options
47+
) as ChakraModuleOptions
48+
49+
const extendTheme = _extendTheme(_options.extendTheme || {})
50+
const icons = _options.icons
51+
const isBaseTheme = _options.isBaseTheme
52+
const emotionCacheOptions = _options.emotionCacheOptions
53+
const cssReset = _options.cssReset
3454

3555
// Install emotion module
3656
installModule("@nuxtjs/emotion")
@@ -40,18 +60,23 @@ export default defineNuxtModule<ChakraPluginOptions>({
4060
nuxt.options.build.transpile.push("@chakra-ui")
4161
nuxt.options.build.transpile.push(runtimeDir)
4262
addServerPlugin(resolve(runtimeDir, "chakra.server"))
43-
// addPluginTemplate(resolve(templatesDir, "chakra.universal.t"))
44-
addPluginTemplate({
45-
src: resolve(templatesDir, "chakra.universal.t.js"),
46-
options: {
47-
theme: theme,
48-
cssReset: true,
49-
icons: {},
50-
emotionCacheOptions: {
51-
key: "chakra",
52-
},
53-
isBaseTheme: true,
54-
} as ChakraPluginOptions,
55-
})
63+
64+
// Resolve template and inject plugin
65+
addPlugin(resolve(runtimeDir, "chakra.factory.universal"))
66+
addPluginTemplate(
67+
{
68+
src: resolve(templatesDir, "chakra.universal.t.ts"),
69+
options: {
70+
extendTheme,
71+
cssReset,
72+
icons,
73+
emotionCacheOptions,
74+
isBaseTheme,
75+
} as ChakraPluginOptions,
76+
},
77+
{
78+
append: true,
79+
}
80+
)
5681
},
5782
})
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type * as NuxtAppTypes from "nuxt/app"
2+
import { defineNuxtPlugin } from "#imports"
3+
import Chakra, {
4+
chakra,
5+
cookieStorageManagerSSR,
6+
extendChakra,
7+
ColorModeConstants,
8+
extendTheme,
9+
ColorModeScriptProps,
10+
} from "@chakra-ui/vue-next"
11+
import { domElements } from "@chakra-ui/vue-system"
12+
import { parseCookies } from "h3"
13+
14+
type AllowedSSRColorMode = Exclude<
15+
ColorModeScriptProps["initialColorMode"],
16+
"system"
17+
>
18+
19+
export default defineNuxtPlugin((nuxtApp) => {
20+
const app = nuxtApp.vueApp
21+
domElements.forEach((tag) => {
22+
app.component(`chakra.${tag}`, chakra(tag))
23+
})
24+
})

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

Lines changed: 0 additions & 56 deletions
This file was deleted.

packages/nuxt/src/templates/chakra.universal.t.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { defineNuxtPlugin } from "#imports"
22
import Chakra, {
3-
chakra,
43
cookieStorageManagerSSR,
54
extendChakra,
65
ColorModeConstants,
76
ColorModeScriptProps,
8-
ChakraPluginOptions,
7+
extendTheme
98
} from "@chakra-ui/vue-next"
10-
import { domElements } from "@chakra-ui/vue-system"
119
import { parseCookies } from "h3"
1210

1311
type AllowedSSRColorMode = Exclude<
@@ -46,6 +44,7 @@ export default defineNuxtPlugin((nuxtApp) => {
4644
// SSR Color Mode Handling
4745
let ssrColorMode: AllowedSSRColorMode
4846
const event = nuxtApp.ssrContext?.event
47+
4948
if (event) {
5049
const parsedCookies = parseCookies(event)
5150
const colorMode = parsedCookies[ColorModeConstants.CookieStorageKey]
@@ -62,15 +61,25 @@ export default defineNuxtPlugin((nuxtApp) => {
6261

6362
// Variable merging
6463
const cssReset = <%= options.cssReset %>
65-
const icons = <%= JSON.stringify(options.icons, null, 2) %>
64+
const icons = <%= JSON.stringify(options.icons || "NULL", null, 2) %>
6665
const isBaseTheme = <%= options.isBaseTheme %>
67-
const emotionCacheOptions = <%= JSON.stringify(options.emotionCacheOptions || {}, null, 2) %>
66+
const emotionCacheOptions = <%= JSON.stringify(options.emotionCacheOptions || "NULL", null, 2) %>
6867

69-
const pluginOptions: ChakraPluginOptions = {
68+
const pluginOptions = {
7069
cssReset,
71-
extendTheme: extendedTheme,
72-
icons,
73-
emotionCacheOptions,
70+
extendTheme: extendTheme(extendedTheme, {
71+
config: {
72+
initialColorMode: isBrowser
73+
? window.$chakraSSRContext?.theme?.ssrColorMode
74+
: ssrColorMode,
75+
},
76+
}),
77+
...(icons !== "NULL" && {
78+
icons,
79+
}),
80+
...(emotionCacheOptions !== "NULL" && {
81+
emotionCacheOptions,
82+
}),
7483
isBaseTheme
7584
}
7685

@@ -84,8 +93,4 @@ export default defineNuxtPlugin((nuxtApp) => {
8493
),
8594
})
8695
)
87-
88-
domElements.forEach((tag) => {
89-
app.component(`chakra.${tag}`, chakra(tag))
90-
})
9196
})

0 commit comments

Comments
 (0)