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

Commit 2badeaa

Browse files
committed
Add new variants (#1)
The following variants are now available: - dark-hover - dark-focus - dark-active - dark-group-hover - dark-focus-within
1 parent 6f072b4 commit 2badeaa

File tree

3 files changed

+62
-17
lines changed

3 files changed

+62
-17
lines changed

README.md

+30-15
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,46 @@ plugins: [
1414
]
1515
```
1616

17-
If you'd like to switch between light and dark modes depending on the time of day, `dark-mode.js` is provided, which is a simple script that adds the `.mode-dark` class to the `<html>` element from 6 PM to 6 AM in the user's timezone.
18-
1917
## Usage
2018

21-
Dark mode must be enabled in your Tailwind configuration for any utilities that need it.
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. `dark-mode.js` is provided as an option, which is a simple script that enables dark mode from 6 PM to 6 AM in the user's timezone.
2220

23-
```javascript
24-
variants: {
25-
backgroundColor: ['responsive', 'hover', 'focus', 'dark'],
26-
borderColor: ['responsive', 'hover', 'focus', 'dark'],
27-
textColor: ['responsive', 'hover', 'focus', 'dark']
28-
}
29-
```
21+
### Available Variants
22+
23+
Variants for dark mode are based on [Tailwind's own variants](https://tailwindcss.com/docs/state-variants/)...
24+
25+
- `dark`
26+
- `dark-hover`
27+
- `dark-focus`
28+
- `dark-active`
29+
- `dark-group-hover`
30+
- `dark-focus-within`
3031

31-
Styles specific to dark mode are applied as a variant (the same as responsive, hover, focus, etc).
32+
... and are used in the same way.
3233

3334
```html
3435
<div class="bg-white dark:bg-black">
3536
<p class="text-black dark:text-white">
3637
My eyes are grateful.
38+
39+
<a ... class="text-blue-300 hover:text-blue-400 dark:text-blue-700 dark-hover:text-blue-600">
40+
Learn more
41+
</a>
3742
</p>
3843
</div>
3944
```
4045

41-
The markup above would show a white background with black text by default. If `.mode-dark` was applied to the `<html>` element, the background would be black with white text.
46+
### Enabling Variants
47+
48+
As with existing variants such as `hover` and `focus`, variants for dark mode must be enabled in your Tailwind configuration for any utilities that need them.
49+
50+
```javascript
51+
variants: {
52+
backgroundColor: ['dark', 'dark-hover', 'dark-group-hover'],
53+
borderColor: ['dark', 'dark-focus', 'dark-focus-within'],
54+
textColor: ['dark', 'dark-hover', 'dark-active']
55+
}
56+
```
4257

4358
## PurgeCSS
4459

@@ -48,13 +63,13 @@ If you are using PurgeCSS, you should either add `mode-dark` to the `whitelist`
4863
whitelist: ['mode-dark']
4964
```
5065

51-
... Or add `dark-mode.js` to the `content` array.
66+
... or add `dark-mode.js` to the `content` array.
5267

5368
```javascript
5469
content: [
5570
'**/*.js',
56-
'dark-mode.js'
71+
'./node_modules/tailwindcss-dark-mode/dark-mode.js'
5772
]
5873
```
5974

60-
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.
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.

index.js

+30
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,35 @@ module.exports = function() {
55
return `.mode-dark .${e(`dark${separator}${className}`)}`;
66
});
77
});
8+
9+
addVariant('dark-hover', ({modifySelectors, separator}) => {
10+
modifySelectors(({className}) => {
11+
return `.mode-dark .${e(`dark-hover${separator}${className}`)}:hover`;
12+
});
13+
});
14+
15+
addVariant('dark-focus', ({modifySelectors, separator}) => {
16+
modifySelectors(({className}) => {
17+
return `.mode-dark .${e(`dark-focus${separator}${className}`)}:focus`;
18+
});
19+
});
20+
21+
addVariant('dark-active', ({modifySelectors, separator}) => {
22+
modifySelectors(({className}) => {
23+
return `.mode-dark .${e(`dark-active${separator}${className}`)}:active`;
24+
});
25+
});
26+
27+
addVariant('dark-group-hover', ({modifySelectors, separator}) => {
28+
modifySelectors(({className}) => {
29+
return `.mode-dark .group:hover .${e(`dark-group-hover${separator}${className}`)}`;
30+
});
31+
});
32+
33+
addVariant('dark-focus-within', ({modifySelectors, separator}) => {
34+
modifySelectors(({className}) => {
35+
return `.mode-dark .${e(`dark-focus-within${separator}${className}`)}:focus-within`;
36+
});
37+
});
838
};
939
};

package.json

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

0 commit comments

Comments
 (0)