Skip to content
This repository was archived by the owner on May 27, 2021. It is now read-only.

Commit 8e8ee5e

Browse files
tbetousChanceArthur
authored andcommitted
Add ability to customize selector (#12)
1 parent d48e920 commit 8e8ee5e

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

README.md

+18-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plugins: [
1616

1717
## Usage
1818

19-
Styles generated by this plugin are only used if the `mode-dark` class is applied to the `<html>` element. How you do that is up to you. `prefers-dark.js` is provided as an option, which is a simple script that enables dark mode based on the user's system theme.
19+
Styles generated by this plugin are only used if the selector is applied to the `<html>` element. How you do that is up to you. `prefers-dark.js` is provided as an option, which is a simple script that enables dark mode based on the user's system theme.
2020

2121
### Available Variants
2222

@@ -55,21 +55,34 @@ variants: {
5555
}
5656
```
5757

58+
### Changing the Selector
59+
60+
By default, `.mode-dark` is used as the selector for dark mode. You can change this by adding the `darkSelector` key to the `theme` section in your Tailwind configuration.
61+
62+
```javascript
63+
theme: {
64+
darkSelector: '.mode-dark'
65+
}
66+
```
67+
68+
Don't forget to also change the selector in `prefers-dark.js` if you are using it.
69+
5870
## PurgeCSS
5971

60-
If you are using PurgeCSS, you should either add `mode-dark` to the `whitelist` array...
72+
If you are using PurgeCSS, you should either add the selector to the `whitelist` array...
6173

6274
```javascript
6375
whitelist: ['mode-dark']
6476
```
6577

66-
... or add `dark-mode.js` to the `content` array.
78+
... or add `prefers-dark.js` to the `content` array.
6779

6880
```javascript
6981
content: [
7082
'**/*.js',
71-
'./node_modules/tailwindcss-dark-mode/dark-mode.js'
83+
'./node_modules/tailwindcss-dark-mode/prefers-dark.js',
84+
'./or/your/own/prefers-dark.js'
7285
]
7386
```
7487

75-
Otherwise, PurgeCSS will assume that any classes related to dark mode should be removed, as the `mode-dark` class is only applied when the page is loaded.
88+
Otherwise, PurgeCSS will assume that any classes related to dark mode should be removed, as the selector is only applied when the page is loaded.

index.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
11
module.exports = function() {
2-
return function({addVariant, e}) {
2+
return function({addVariant, theme, e}) {
3+
const darkSelector = theme('darkSelector', '.mode-dark');
34
addVariant('dark', ({modifySelectors, separator}) => {
45
modifySelectors(({className}) => {
5-
return `.mode-dark .${e(`dark${separator}${className}`)}`;
6+
return `${darkSelector} .${e(`dark${separator}${className}`)}`;
67
});
78
});
89

910
addVariant('dark-hover', ({modifySelectors, separator}) => {
1011
modifySelectors(({className}) => {
11-
return `.mode-dark .${e(`dark-hover${separator}${className}`)}:hover`;
12+
return `${darkSelector} .${e(`dark-hover${separator}${className}`)}:hover`;
1213
});
1314
});
1415

1516
addVariant('dark-focus', ({modifySelectors, separator}) => {
1617
modifySelectors(({className}) => {
17-
return `.mode-dark .${e(`dark-focus${separator}${className}`)}:focus`;
18+
return `${darkSelector} .${e(`dark-focus${separator}${className}`)}:focus`;
1819
});
1920
});
2021

2122
addVariant('dark-active', ({modifySelectors, separator}) => {
2223
modifySelectors(({className}) => {
23-
return `.mode-dark .${e(`dark-active${separator}${className}`)}:active`;
24+
return `${darkSelector} .${e(`dark-active${separator}${className}`)}:active`;
2425
});
2526
});
2627

2728
addVariant('dark-group-hover', ({modifySelectors, separator}) => {
2829
modifySelectors(({className}) => {
29-
return `.mode-dark .group:hover .${e(`dark-group-hover${separator}${className}`)}`;
30+
return `${darkSelector} .group:hover .${e(`dark-group-hover${separator}${className}`)}`;
3031
});
3132
});
3233

3334
addVariant('dark-focus-within', ({modifySelectors, separator}) => {
3435
modifySelectors(({className}) => {
35-
return `.mode-dark .${e(`dark-focus-within${separator}${className}`)}:focus-within`;
36+
return `${darkSelector} .${e(`dark-focus-within${separator}${className}`)}:focus-within`;
3637
});
3738
});
3839
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "tailwindcss-dark-mode",
33
"description": "A Tailwind CSS plugin that adds variants for dark mode",
4-
"version": "1.1.1",
4+
"version": "1.1.2",
55
"author": "Chance Arthur",
66
"license": "MIT",
77
"copyright": "Chance Arthur",

0 commit comments

Comments
 (0)