|
| 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