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

Commit 00fe68e

Browse files
committed
fix: theme replacer
1 parent c2d48d3 commit 00fe68e

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

packages/nuxt/src/module.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createResolver,
55
addServerPlugin,
66
installModule,
7+
addPluginTemplate,
78
} from "@nuxt/kit"
89
import type * as NuxtSchema from "@nuxt/schema"
910
import { ChakraPluginOptions } from "@chakra-ui/vue-next"
@@ -35,8 +36,22 @@ export default defineNuxtModule<ChakraPluginOptions>({
3536
installModule("@nuxtjs/emotion")
3637
const { resolve } = createResolver(import.meta.url)
3738
const runtimeDir = resolve("./runtime")
39+
const templatesDir = resolve("./templates")
40+
nuxt.options.build.transpile.push("@chakra-ui")
3841
nuxt.options.build.transpile.push(runtimeDir)
3942
addServerPlugin(resolve(runtimeDir, "chakra.server"))
40-
addPlugin(resolve(runtimeDir, "chakra.universal"))
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+
})
4156
},
4257
})
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { defineNuxtPlugin } from "#imports"
2+
import Chakra, {
3+
chakra,
4+
cookieStorageManagerSSR,
5+
extendChakra,
6+
ColorModeConstants,
7+
ColorModeScriptProps,
8+
ChakraPluginOptions,
9+
} from "@chakra-ui/vue-next"
10+
import { domElements } from "@chakra-ui/vue-system"
11+
import { parseCookies } from "h3"
12+
13+
type AllowedSSRColorMode = Exclude<
14+
ColorModeScriptProps["initialColorMode"],
15+
"system"
16+
>
17+
18+
const extendedTheme = <%= (function() {
19+
// keep a list of serialized functions
20+
const functions = []
21+
22+
// json replacer - returns a placeholder for functions
23+
const jsonReplacer = (key, val) => {
24+
if (typeof val === 'function') {
25+
functions.push(val.toString())
26+
return "{func_" + (functions.length - 1) + "}"
27+
}
28+
return val
29+
};
30+
31+
// regex replacer - replaces placeholders with functions
32+
const funcReplacer = (match, id) => {
33+
return functions[id]
34+
}
35+
36+
const result = JSON
37+
.stringify(options.extendTheme || {}, jsonReplacer, 2)
38+
.replace(/"\{func_(\d+)\}"/g, funcReplacer)
39+
40+
return result
41+
})() %>
42+
43+
44+
export default defineNuxtPlugin((nuxtApp) => {
45+
46+
// SSR Color Mode Handling
47+
let ssrColorMode: AllowedSSRColorMode
48+
const event = nuxtApp.ssrContext?.event
49+
if (event) {
50+
const parsedCookies = parseCookies(event)
51+
const colorMode = parsedCookies[ColorModeConstants.CookieStorageKey]
52+
if (colorMode) {
53+
ssrColorMode = colorMode as AllowedSSRColorMode
54+
} else {
55+
// TODO: Replace with options color mode
56+
ssrColorMode = "light"
57+
}
58+
}
59+
60+
const app = nuxtApp.vueApp
61+
const isBrowser = typeof document !== "undefined"
62+
63+
// Variable merging
64+
const cssReset = <%= options.cssReset %>
65+
const icons = <%= JSON.stringify(options.icons, null, 2) %>
66+
const isBaseTheme = <%= options.isBaseTheme %>
67+
const emotionCacheOptions = <%= JSON.stringify(options.emotionCacheOptions || {}, null, 2) %>
68+
69+
const pluginOptions: ChakraPluginOptions = {
70+
cssReset,
71+
extendTheme: extendedTheme,
72+
icons,
73+
emotionCacheOptions,
74+
isBaseTheme
75+
}
76+
77+
// Install plugin
78+
app.use(
79+
Chakra,
80+
extendChakra({
81+
...pluginOptions,
82+
colorModeManager: cookieStorageManagerSSR(
83+
ColorModeConstants.CookieStorageKey
84+
),
85+
})
86+
)
87+
88+
domElements.forEach((tag) => {
89+
app.component(`chakra.${tag}`, chakra(tag))
90+
})
91+
})

0 commit comments

Comments
 (0)