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

Commit aabb225

Browse files
committed
feat: synchronize color mode
1 parent a1ffdaa commit aabb225

File tree

6 files changed

+68
-15
lines changed

6 files changed

+68
-15
lines changed

packages/nuxt/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# @chakra-ui/nuxt-next
2+
3+
Nuxt.js Module for Chakra UI Vue
4+
5+
## Installation
6+
7+
```sh
8+
yarn add @chakra-ui/vue-next
9+
# or
10+
npm i @chakra-ui/vue-next
11+
```
12+
13+
## Todo
14+
- [ ] Install emotion
15+
- [ ] Install plugin programmatically

packages/nuxt/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"build:types": "tsup src --dts-only"
3030
},
3131
"dependencies": {
32+
"@chakra-ui/c-color-mode": "workspace:*",
3233
"@chakra-ui/vue-next": "workspace:*",
3334
"@emotion/css": "^11.10.5",
3435
"@emotion/server": "^11.10.0",

packages/nuxt/src/module.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,24 @@ import {
66
installModule,
77
} from "@nuxt/kit"
88
import type * as NuxtSchema from "@nuxt/schema"
9+
import { ChakraPluginOptions } from "@chakra-ui/vue-next"
910

10-
export default defineNuxtModule({
11+
export default defineNuxtModule<ChakraPluginOptions>({
1112
meta: {
12-
name: "@chakra-ui/nuxt-nect",
13+
name: "@chakra-ui/nuxt-next",
1314
configKey: "chakra",
1415
compatibilty: ">=3.0.0",
1516
},
16-
setup(_, nuxt) {
17+
setup(_options, nuxt) {
1718
console.log("chakra-ui-nuxt:module")
18-
/**
19-
* Register emotion plugin
20-
*/
19+
const theme = _options.extendTheme
2120

21+
// Install emotion module
2222
installModule("@nuxtjs/emotion")
2323
const { resolve } = createResolver(import.meta.url)
2424
const runtimeDir = resolve("./runtime")
2525
nuxt.options.build.transpile.push(runtimeDir)
2626
addServerPlugin(resolve(runtimeDir, "chakra.server"))
27-
addPlugin(resolve(runtimeDir, "chakra.client"))
27+
addPlugin(resolve(runtimeDir, "chakra.universal"))
2828
},
2929
})

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

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

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NitroApp } from "nitropack"
2+
import { H3Event, parseCookies } from "h3"
23

34
/**
45
* Why are we declaring types for `defineNitroPlugin`?
@@ -21,6 +22,19 @@ export function defineNitroPlugin(def: NitroAppPlugin): NitroAppPlugin {
2122
return def
2223
}
2324

24-
export default defineNitroPlugin((nitroApp) => {
25+
const CHAKRA_UI_COLOR_MODE_COOKIE_KEY = "chakra-ui-color-mode"
26+
27+
export default defineNitroPlugin((app) => {
2528
console.log("chakra-ui-nuxt:server_runtime")
29+
app.hooks.hook("render:html", (html, { event: rawEvent }) => {
30+
const event = new H3Event(rawEvent.node.req, rawEvent.node.res)
31+
const parsedCookies = parseCookies(event)
32+
const colorMode = parsedCookies[CHAKRA_UI_COLOR_MODE_COOKIE_KEY]
33+
34+
if (colorMode) {
35+
html.htmlAttrs.push(`data-theme="${colorMode}"`)
36+
html.htmlAttrs.push(`style="color-scheme: ${colorMode};"`)
37+
html.htmlAttrs.push(`data-chakra-ui-ssr="true"`)
38+
}
39+
})
2640
})
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type * as NuxtAppTypes from "nuxt/app"
2+
import { defineNuxtPlugin } from "#imports"
3+
import Chakra, {
4+
chakra,
5+
ChakraPluginOptions,
6+
cookieStorageManagerSSR,
7+
extendChakra,
8+
ColorModeConstants,
9+
} from "@chakra-ui/vue-next"
10+
import { domElements } from "@chakra-ui/vue-system"
11+
12+
export default defineNuxtPlugin((nuxtApp) => {
13+
console.log("chakra-ui-nuxt:plugin_context")
14+
15+
const app = nuxtApp.vueApp
16+
17+
app.use(
18+
Chakra,
19+
extendChakra({
20+
cssReset: true,
21+
colorModeManager: cookieStorageManagerSSR(
22+
ColorModeConstants.CookieStorageKey
23+
),
24+
})
25+
)
26+
27+
domElements.forEach((tag) => {
28+
app.component(`chakra.${tag}`, chakra(tag))
29+
})
30+
})

0 commit comments

Comments
 (0)