|
| 1 | +# Nuxt Integration Guide |
| 2 | + |
| 3 | +This guide explains how to integrate Vue UI into a Nuxt application. It assumes you already have a fresh Nuxt project. If not, you can create one using the following command: |
| 4 | + |
| 5 | +```bash |
| 6 | +npm create nuxt@latest |
| 7 | +``` |
| 8 | + |
| 9 | +Before going further, you need to install the required dependencies. |
| 10 | + |
| 11 | +```bash [pnpm] |
| 12 | +pnpm add reka-ui tailwind-variants @vueuse/core @vueuse/shared @vueuse/integrations colortranslator defu fuse.js ohash embla-carousel embla-carousel-auto-height embla-carousel-auto-scroll embla-carousel-autoplay embla-carousel-class-names embla-carousel-fade embla-carousel-vue embla-carousel-wheel-gestures vue-component-type-helpers |
| 13 | +``` |
| 14 | + |
| 15 | +## Configuring Nuxt |
| 16 | + |
| 17 | +To ensure everything works seamlessly, you need to adjust the Nuxt configuration for dependency optimization, component resolution, and TypeScript paths. |
| 18 | + |
| 19 | +Add the following configuration to your `nuxt.config.ts` file: |
| 20 | + |
| 21 | +```ts [nuxt.config.ts] |
| 22 | +import { fileURLToPath } from 'node:url' |
| 23 | +import tailwindcss from '@tailwindcss/vite' |
| 24 | + |
| 25 | +export default defineNuxtConfig({ |
| 26 | + // ... |
| 27 | + |
| 28 | + future: { |
| 29 | + compatibilityVersion: 4 |
| 30 | + }, |
| 31 | + |
| 32 | + components: { |
| 33 | + dirs: [ |
| 34 | + '~/ui/components', |
| 35 | + ], |
| 36 | + }, |
| 37 | + |
| 38 | + imports: { |
| 39 | + dirs: [ |
| 40 | + 'ui/composables', |
| 41 | + ] |
| 42 | + }, |
| 43 | + |
| 44 | + typescript: { |
| 45 | + tsConfig: { |
| 46 | + compilerOptions: { |
| 47 | + paths: { |
| 48 | + '@/ui': [ |
| 49 | + '../app/ui', |
| 50 | + ], |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + }, |
| 55 | + |
| 56 | + css: ['~/assets/css/main.css'], |
| 57 | + |
| 58 | + vite: { |
| 59 | + plugins: [ |
| 60 | + tailwindcss(), |
| 61 | + ], |
| 62 | + optimizeDeps: { |
| 63 | + include: ['reka-ui', 'tailwind-variants', '@vueuse/shared', '@vueuse/core'] |
| 64 | + }, |
| 65 | + resolve: { |
| 66 | + alias: { |
| 67 | + '@/ui': fileURLToPath(new URL('./app/ui', import.meta.url)), |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | +}) |
| 72 | +``` |
| 73 | + |
| 74 | +Here are some important aspects of the configuration to help you understand its purpose and functionality: |
| 75 | + |
| 76 | +1. **Components Directory**: |
| 77 | + - The `components` option specifies `~/ui/components` as a directory for auto-registering components. This eliminates the need for manual imports, making component usage more straightforward. |
| 78 | + |
| 79 | +2. **Imports Directory**: |
| 80 | + - The `imports` option includes `ui/composables`, enabling automatic imports of composables from this directory. This simplifies the process of using composables throughout your project. |
| 81 | + |
| 82 | +3. **TypeScript Path Mapping**: |
| 83 | + - The `typescript.tsConfig.compilerOptions.paths` setting maps the alias `@/ui` to `../app/ui`. This ensures that TypeScript correctly resolves the alias, providing accurate type checking and auto-completion in your development environment. |
| 84 | + |
| 85 | +4. **CSS Integration**: |
| 86 | + - The `css` array includes `~/assets/css/main.css`, which serves as the main stylesheet for the project. This file integrates Tailwind CSS and custom styles, ensuring a consistent design system. |
| 87 | + |
| 88 | +5. **Vite Configuration**: |
| 89 | + - **Plugins**: The `vite.plugins` array includes the Tailwind CSS plugin, ensuring seamless integration with the Vite build tool. |
| 90 | + - **Dependency Optimization**: The `optimizeDeps.include` array pre-optimizes key dependencies (`reka-ui`, `tailwind-variants`, `@vueuse/shared`, `@vueuse/core`) to enhance the performance of the development server. |
| 91 | + - **Alias Resolution**: The `resolve.alias` block maps `@/ui` to the `./app/ui` directory. This simplifies import paths and ensures consistency across the project. |
| 92 | + |
| 93 | +These configurations are designed to streamline development and improve the overall developer experience when working with Vue UI in a Nuxt application. |
| 94 | + |
| 95 | +## Setting Up Tailwind CSS |
| 96 | + |
| 97 | +For this section, you need to install Tailwind CSS and its dependencies: |
| 98 | + |
| 99 | +```bash [pnpm] |
| 100 | +pnpm add -D tailwindcss @tailwindcss/vite @egoist/tailwindcss-icons |
| 101 | +``` |
| 102 | + |
| 103 | +To style your project, integrate Tailwind CSS using its Vite plugin: |
| 104 | + |
| 105 | +1. Add the plugin to your `nuxt.config.ts` file (already included in the configuration above). |
| 106 | + |
| 107 | +2. Import Tailwind CSS in your main style file, typically `app/assets/css/main.css`: |
| 108 | + |
| 109 | +```css [app/assets/css/main.css] |
| 110 | +@import "tailwindcss"; |
| 111 | +@plugin '@egoist/tailwindcss-icons'; |
| 112 | + |
| 113 | +@variant light (&:where(.light, .light *)); |
| 114 | +@variant dark (&:where(.dark, .dark *)); |
| 115 | + |
| 116 | +@theme default { |
| 117 | + --color-primary: var(--ui-primary); |
| 118 | + --color-secondary: var(--ui-secondary); |
| 119 | + // Add other custom variables here... |
| 120 | +} |
| 121 | + |
| 122 | +@layer base { |
| 123 | + body { |
| 124 | + @apply antialiased text-default bg-default scheme-light dark:scheme-dark; |
| 125 | + } |
| 126 | + |
| 127 | + :root { |
| 128 | + --ui-color-primary-500: var(--color-violet-500); |
| 129 | + --ui-color-secondary-500: var(--color-blue-500); |
| 130 | + // Define other colors and variables... |
| 131 | + } |
| 132 | + |
| 133 | + .dark { |
| 134 | + --ui-primary: var(--ui-color-primary-400); |
| 135 | + --ui-secondary: var(--ui-color-secondary-400); |
| 136 | + // Define dark theme variables... |
| 137 | + } |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | +This setup allows you to define light and dark themes, customize colors, and use Tailwind's built-in CSS variables. |
| 142 | + |
| 143 | +## Example Usage |
| 144 | + |
| 145 | +With everything set up, you can start using Vue UI components and composables. |
| 146 | + |
| 147 | +::: code-group |
| 148 | + |
| 149 | +```vue [app/app.vue] |
| 150 | +<script lang="ts" setup> |
| 151 | +import App from '@/ui/components/App.vue' |
| 152 | +</script> |
| 153 | + |
| 154 | +<template> |
| 155 | + <App> |
| 156 | + <NuxtPage /> |
| 157 | + </App> |
| 158 | +</template> |
| 159 | +``` |
| 160 | + |
| 161 | +```vue [app/pages/index.vue] |
| 162 | +<script setup lang="ts"> |
| 163 | +const toast = useToast() |
| 164 | +function onClick() { |
| 165 | + toast.add({ |
| 166 | + title: 'Hey there!', |
| 167 | + description: 'This is a toast notification.', |
| 168 | + }) |
| 169 | +} |
| 170 | +</script> |
| 171 | + |
| 172 | +<template> |
| 173 | + <div class="p-4 flex flex-col items-start gap-2"> |
| 174 | + <h1 class="text-2xl font-bold"> |
| 175 | + Welcome to Vue UI |
| 176 | + </h1> |
| 177 | + |
| 178 | + <Button label="Click Me" @click="onClick" /> |
| 179 | + </div> |
| 180 | +</template> |
| 181 | +``` |
| 182 | + |
| 183 | +::: |
| 184 | + |
| 185 | +> [!NOTE] |
| 186 | +> To prevent circular dependencies, the `App.vue` component must be explicitly imported in the `app.vue` file. This avoids issues where `app/app.vue` might attempt to auto-import itself. |
| 187 | + |
| 188 | +You're now ready to build your project with Vue UI! |
0 commit comments