Skip to content

Commit

Permalink
feat: add classPrefix and classSuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Sep 15, 2020
1 parent 9a729c8 commit 0f2141c
Show file tree
Hide file tree
Showing 5 changed files with 359 additions and 295 deletions.
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
- Sync between tabs 🔄
- Supports IE9+ 👴

ℹ️ This module is using a cookie to support server-side rendering, if your visitors are located in Europe, make sure to follow the [EU cookie directive](https://en.wikipedia.org/wiki/HTTP_cookie#EU_cookie_directive)
ℹ️  This module is using a cookie to support server-side rendering, if your visitors are located in Europe, make sure to follow the [EU cookie directive](https://en.wikipedia.org/wiki/HTTP_cookie#EU_cookie_directive)

## Setup

Expand Down Expand Up @@ -100,6 +100,8 @@ colorMode: {
hid: 'nuxt-color-mode-script',
globalName: '__NUXT_COLOR_MODE__',
componentName: 'ColorScheme',
classPrefix: '',
classSufix: '-mode',
cookie: {
key: 'nuxt-color-mode',
options: {
Expand Down Expand Up @@ -137,6 +139,34 @@ Props:

## TailwindCSS Dark Mode

### Tailwind v1.8+

Tailwind v1.8 introduced [dark mode support](https://github.com/tailwindlabs/tailwindcss/pull/2279), in order to work with @nuxtjs/color-mode, you need to set `dark: 'class'` in your `tailwind.config.js`:

```js
// tailwind.config.js
module.exports = {
experimental: {
darkModeVariant: true
},
dark: 'class'
}
```

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

```js
// nuxt.config.js
export default {
// ...
colorMode: {
classSuffix: ''
}
}
```

### Tailwindcss Dark Mode Plugin

You can easily integrate this module with [tailwindcss-dark-mode](https://github.com/ChanceArthur/tailwindcss-dark-mode) by just setting `darkSelector: '.dark-mode'`, see [changing the selector documentation](https://github.com/ChanceArthur/tailwindcss-dark-mode#changing-the-selector).

```js
Expand Down
2 changes: 2 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export default async function (moduleOptions) {
hid: 'nuxt-color-mode-script',
globalName: '__NUXT_COLOR_MODE__',
componentName: 'ColorScheme',
classPrefix: '',
classSuffix: '-mode',
cookie: {
key: 'nuxt-color-mode',
options: {
Expand Down
4 changes: 2 additions & 2 deletions lib/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function getCookie (name) {
}

function addClass (value) {
const className = value + '-mode'
const className = '<%= options.classPrefix %>' + value + '<%= options.classSuffix %>'
if (de.classList) {
de.classList.add(className)
} else {
Expand All @@ -46,7 +46,7 @@ function addClass (value) {
}

function removeClass (value) {
const className = value + '-mode'
const className = '<%= options.classPrefix %>' + value + '<%= options.classSuffix %>'
if (de.classList) {
de.classList.remove(className)
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"eslint": "^7.9.0",
"husky": "latest",
"jest": "^26.4.2",
"nuxt-edge": "^2.14.1-26608350.3204f301",
"nuxt": "^2.14.5",
"rollup": "^2.26.11",
"rollup-plugin-babel": "latest",
"rollup-plugin-terser": "^6.1.0",
Expand Down
Loading

0 comments on commit 0f2141c

Please # to comment.