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

Commit c0b05ff

Browse files
committed
feat(nuxt): enable component auto-import
1 parent 8e94e28 commit c0b05ff

File tree

4 files changed

+101
-21
lines changed

4 files changed

+101
-21
lines changed

examples/nuxt-app/app.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<chakra.div
2+
<c-h-stack
33
display="flex"
44
min-h="600px"
55
justify-content="center"
@@ -13,13 +13,13 @@
1313
color="white"
1414
font-weight="bold"
1515
>
16-
Click me to toggle. ({{ colorMode }})
16+
Click me to toggle | {{ colorMode }}
1717
</chakra.button>
1818

1919
<chakra.button px="4" py="3" bg="$brand" font-weight="bold">
2020
Extended Component
2121
</chakra.button>
22-
</chakra.div>
22+
</c-h-stack>
2323
</template>
2424

2525
<script lang="ts" setup>

packages/c-modal/src/c-alert-dialog.tsx

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import {
88
CModalContentProps,
99
} from "./c-modal"
1010

11+
import {
12+
CModalBody,
13+
CModalCloseButton,
14+
CModalFooter,
15+
CModalHeader,
16+
CModalOverlay,
17+
} from "./c-modal"
18+
1119
export interface CAlertDialogProps
1220
extends Omit<CModalProps, "initialFocusRef" | "closeModal" | "handleEscape"> {
1321
leastDestructiveRef: CModalProps["initialFocusRef"]
@@ -72,10 +80,27 @@ export const CAlertDialogContent = defineComponent({
7280
},
7381
})
7482

75-
export {
76-
CModalBody as CAlertDialogBody,
77-
CModalCloseButton as CAlertDialogCloseButton,
78-
CModalFooter as CAlertDialogFooter,
79-
CModalHeader as CAlertDialogHeader,
80-
CModalOverlay as CAlertDialogOverlay,
81-
} from "./c-modal"
83+
export const CAlertDialogBody = defineComponent({
84+
name: "CAlertDialogBody",
85+
extends: CModalBody,
86+
})
87+
88+
export const CAlertDialogCloseButton = defineComponent({
89+
name: "CAlertDialogCloseButton",
90+
extends: CModalCloseButton,
91+
})
92+
93+
export const CAlertDialogFooter = defineComponent({
94+
name: "CAlertDialogFooter",
95+
extends: CModalFooter,
96+
})
97+
98+
export const CAlertDialogHeader = defineComponent({
99+
name: "CAlertDialogHeader",
100+
extends: CModalHeader,
101+
})
102+
103+
export const CAlertDialogOverlay = defineComponent({
104+
name: "CAlertDialogOverlay",
105+
extends: CModalOverlay,
106+
})

packages/c-modal/src/c-drawer.tsx

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,23 @@ import {
1919
import { createContext } from "@chakra-ui/vue-utils"
2020
import {
2121
chakra,
22-
ComponentWithProps,
23-
DeepPartial,
2422
HTMLChakraProps,
2523
SystemStyleObject,
2624
useStyles,
2725
useTheme,
2826
} from "@chakra-ui/vue-system"
2927

30-
import { CModal, CModalProps, modalProps, useModalContext } from "./c-modal"
28+
import {
29+
CModal,
30+
CModalBody,
31+
CModalCloseButton,
32+
CModalFooter,
33+
CModalHeader,
34+
CModalOverlay,
35+
CModalProps,
36+
modalProps,
37+
useModalContext,
38+
} from "./c-modal"
3139
import { MotionDirective, useMotions } from "@vueuse/motion"
3240
import { useId } from "@chakra-ui/vue-composables"
3341

@@ -232,10 +240,27 @@ export const CDrawerContent = defineComponent({
232240
},
233241
})
234242

235-
export {
236-
CModalBody as CDrawerBody,
237-
CModalCloseButton as CDrawerCloseButton,
238-
CModalFooter as CDrawerFooter,
239-
CModalHeader as CDrawerHeader,
240-
CModalOverlay as CDrawerOverlay,
241-
} from "./c-modal"
243+
export const CDrawerBody = defineComponent({
244+
name: "CDrawerBody",
245+
extends: CModalBody,
246+
})
247+
248+
export const CDrawerCloseButton = defineComponent({
249+
name: "CDrawerCloseButton",
250+
extends: CModalCloseButton,
251+
})
252+
253+
export const CDrawerFooter = defineComponent({
254+
name: "CDrawerFooter",
255+
extends: CModalFooter,
256+
})
257+
258+
export const CDrawerHeader = defineComponent({
259+
name: "CDrawerHeader",
260+
extends: CModalHeader,
261+
})
262+
263+
export const CDrawerOverlay = defineComponent({
264+
name: "CDrawerOverlay",
265+
extends: CModalOverlay,
266+
})

packages/nuxt/src/module.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import {
55
addServerPlugin,
66
installModule,
77
addPluginTemplate,
8+
addComponent,
89
} from "@nuxt/kit"
910
import type * as NuxtSchema from "@nuxt/schema"
1011
import {
1112
ChakraPluginOptions,
1213
extendTheme as _extendTheme,
1314
} from "@chakra-ui/vue-next"
1415
import { mergeWith } from "@chakra-ui/utils"
16+
import * as ChakraComponents from "@chakra-ui/vue-next"
1517

1618
/** Chakra UI Vue SSR Context State */
1719
export interface ChakraUISSRContext {
@@ -26,6 +28,26 @@ declare global {
2628
}
2729
}
2830

31+
function getChakraComponents() {
32+
const Components = []
33+
const _ChakraComponents = ChakraComponents as Record<string, any>
34+
for (const component in _ChakraComponents) {
35+
/**
36+
* Group of strict checks to make sure that
37+
* we only generate types for components.
38+
*/
39+
if (
40+
component.startsWith("C") &&
41+
_ChakraComponents[component]?.name &&
42+
_ChakraComponents[component]?.setup &&
43+
typeof _ChakraComponents[component]?.setup === "function"
44+
) {
45+
Components.push(_ChakraComponents[component])
46+
}
47+
}
48+
49+
return Components
50+
}
2951
export type ChakraModuleOptions = Omit<ChakraPluginOptions, "colorModeManager">
3052

3153
const defaultModuleOptions: ChakraModuleOptions = {
@@ -59,9 +81,17 @@ export default defineNuxtModule<ChakraModuleOptions>({
5981
const templatesDir = resolve("./templates")
6082
nuxt.options.build.transpile.push("@chakra-ui")
6183
nuxt.options.build.transpile.push(runtimeDir)
62-
addServerPlugin(resolve(runtimeDir, "chakra.server"))
84+
85+
for (const Component of getChakraComponents()) {
86+
addComponent({
87+
name: Component.name,
88+
export: Component.name,
89+
filePath: "@chakra-ui/vue-next",
90+
})
91+
}
6392

6493
// Resolve template and inject plugin
94+
addServerPlugin(resolve(runtimeDir, "chakra.server"))
6595
addPlugin(resolve(runtimeDir, "chakra.factory.universal"))
6696
addPluginTemplate(
6797
{

0 commit comments

Comments
 (0)