Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto install @nuxtjs/color-mode module #737

Merged
merged 2 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions apps/www/src/content/docs/dark-mode/nuxt.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ description: Adding dark mode to your nuxt app.

<Steps>

### Install Dependencies
<!-- ### Install Dependencies

```bash
npm install -D @nuxtjs/color-mode
Expand All @@ -25,19 +25,21 @@ export default defineNuxtConfig({
classSuffix: ''
}
})
```

Optional, to include icons for theme button.
```bash
npm install -D @iconify/vue @iconify-json/radix-icons
```
``` -->

### Add a mode toggle

Place a mode toggle on your site to toggle between light and dark mode.

The `@nuxtjs/color-mode` module is automatically installed and configured during the installation of the `shadcn-nuxt` module, so you literally have nothing to do.

We're using [`useColorMode`](https://color-mode.nuxtjs.org/#usage) from [`Nuxt Color Mode`](https://color-mode.nuxtjs.org/).

Optional, to include icons for theme button.
```bash
npm install -D @iconify/vue @iconify-json/radix-icons
```

```vue
<script setup lang="ts">
import { Icon } from '@iconify/vue'
Expand Down
12 changes: 10 additions & 2 deletions packages/module/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readFileSync, readdirSync } from 'node:fs'
import { join } from 'node:path'
import { parseSync } from '@oxc-parser/wasm'
import { addComponent, addTemplate, createResolver, defineNuxtModule, findPath, useLogger } from '@nuxt/kit'
import { addComponent, addTemplate, createResolver, defineNuxtModule, findPath, installModule, useLogger } from '@nuxt/kit'
import { UTILS } from '../../cli/src/utils/templates'

// TODO: add test to make sure all registry is being parse correctly
Expand Down Expand Up @@ -59,6 +59,14 @@ export default defineNuxtModule<ModuleOptions>({
})
})

// Installs the `@nuxtjs/color-mode` module.

await installModule('@nuxtjs/color-mode', {
colorMode: {
classSuffix: '',
},
})

// Manually scan `componentsDir` for components and register them for auto imports
try {
readdirSync(resolve(COMPONENT_DIR_PATH))
Expand All @@ -73,7 +81,7 @@ export default defineNuxtModule<ModuleOptions>({

const exportedKeys: string[] = ast.program.body
.filter(node => node.type === 'ExportNamedDeclaration')
// @ts-expect-error parse return any
// @ts-expect-error parse return any
.flatMap(node => node.specifiers.map(specifier => specifier.exported.name))
.filter((key: string) => /^[A-Z]/.test(key))

Expand Down