Skip to content

Commit

Permalink
feat: automatic integration with @nuxtjs/tailwindcss (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
ineshbose committed Apr 11, 2024
1 parent 9c80869 commit 7e4db61
Show file tree
Hide file tree
Showing 10 changed files with 764 additions and 578 deletions.
27 changes: 5 additions & 22 deletions docs/content/1.index.md
Expand Up @@ -15,6 +15,7 @@ Dark and Light mode with auto detection made easy with Nuxt 馃寳
- Add `.${color}-mode` class to `<html>` for easy CSS theming
- Force a page to a specific color mode (perfect for incremental development)
- Works with client-side and universal rendering
- Works out of the box with [@nuxtjs/tailwindcss](https://github.com/nuxt-modules/tailwindcss)
- Auto detect system [color-mode](https://drafts.csswg.org/mediaqueries-5/#descdef-media-prefers-color-mode)
- Supports IE9+ 馃懘

Expand All @@ -39,7 +40,10 @@ Then, add `@nuxtjs/color-mode` to the `modules` section of your `nuxt.config.ts`

```ts [nuxt.config.ts]
export default defineNuxtConfig({
modules: ['@nuxtjs/color-mode']
modules: [
'@nuxtjs/color-mode'
// '@nuxtjs/tailwindcss'
]
})
```

Expand Down Expand Up @@ -163,27 +167,6 @@ Props:
- `placeholder`: `String`
- `tag`: `String`, default: `'span'`

## TailwindCSS

Tailwind v2 introduced [dark mode](https://tailwindcss.com/docs/dark-mode), in order to work with `@nuxtjs/color-mode`, you need to set `darkMode: 'class'` in your `tailwind.config.js`:

```js [tailwind.config.js]
module.exports = {
darkMode: 'class'
}
```

Then in your `nuxt.config.ts`, set the `classSuffix` option to an empty string:

```ts [nuxt.config.ts]
export default defineNuxtConfig({
modules: ['@nuxtjs/color-mode'],
colorMode: {
classSuffix: ''
}
})
```

## Migrating to v3

v3 of `@nuxtjs/color-mode` requires either Nuxt Bridge or Nuxt 3. (If you are using Nuxt 2 without Bridge, you should continue to use v2.)
Expand Down
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -39,7 +39,9 @@
},
"dependencies": {
"@nuxt/kit": "^3.11.2",
"pathe": "^1.1.2"
"pathe": "^1.1.2",
"pkg-types": "^1.0.3",
"semver": "^7.6.0"
},
"devDependencies": {
"@commitlint/cli": "^19.2.1",
Expand All @@ -58,4 +60,4 @@
"vitest": "^1.4.0",
"@nuxt/eslint-config": "^0.3.5"
}
}
}
33 changes: 2 additions & 31 deletions playground/app.vue
@@ -1,34 +1,5 @@
<template>
<div class="container">
<Meta
name="description"
content="Demo of using @nuxtjs/color-mode for Nuxt, supporting dark mode and custom colors"
/>
<p>
<NuxtLogo />
</p>
<NuxtLayout>
<NuxtPage />
<p class="note">
Demo based on <a href="https://color-mode.nuxtjs.org">@nuxtjs/color-mode</a> module.
</p>
</div>
</NuxtLayout>
</template>

<style>
p {
padding: 20px 0;
margin: 0;
}
.container {
text-align: center;
padding: 50px;
}
.note {
transition: all .3s;
display: inline-block;
padding: 15px 15px;
border-radius: 5px;
background-color: var(--bg-secondary);
color: var(--color-secondary);
}
</style>
34 changes: 34 additions & 0 deletions playground/layouts/default.vue
@@ -0,0 +1,34 @@
<template>
<div class="container">
<Meta
name="description"
content="Demo of using @nuxtjs/color-mode for Nuxt, supporting dark mode and custom colors"
/>
<p>
<NuxtLogo />
</p>
<slot />
<p class="note">
Demo based on <a href="https://color-mode.nuxtjs.org">@nuxtjs/color-mode</a> module.
</p>
</div>
</template>

<style>
p {
padding: 20px 0;
margin: 0;
}
.container {
text-align: center;
padding: 50px;
}
.note {
transition: all .3s;
display: inline-block;
padding: 15px 15px;
border-radius: 5px;
background-color: var(--bg-secondary);
color: var(--color-secondary);
}
</style>
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
@@ -1,5 +1,5 @@
export default defineNuxtConfig({
components: { global: true, dirs: ['~/components'] },
css: ['~/assets/main.css'],
modules: ['../src/module'],
modules: ['../src/module', '@nuxtjs/tailwindcss'],
})
1 change: 1 addition & 0 deletions playground/package.json
Expand Up @@ -3,6 +3,7 @@
"private": true,
"dependencies": {
"@nuxtjs/color-mode": "latest",
"@nuxtjs/tailwindcss": "^6.11.4",
"nuxt": "^3.11.2"
}
}
6 changes: 6 additions & 0 deletions playground/pages/index.vue
Expand Up @@ -8,5 +8,11 @@
light page
</NuxtLink>
</p>
<p>
Go to
<NuxtLink to="/tailwind">
tailwind integration
</NuxtLink>
</p>
</div>
</template>
21 changes: 21 additions & 0 deletions playground/pages/tailwind.vue
@@ -0,0 +1,21 @@
<script setup>
definePageMeta({
layout: false,
})
</script>

<template>
<div class="bg-white py-4">
<div class="dark:bg-black">
<p class="dark:text-white">
hello tailwind!
</p>
<p>
<NuxtLink to="/">
Home
</NuxtLink>
</p>
<ColorModePicker />
</div>
</div>
</template>

0 comments on commit 7e4db61

Please sign in to comment.