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

feat(*) Allow using class name via config #9

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ plugins: [

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.

To modify the default class name, pass a configuration object to the plugin:

```javascript
plugins: [
require('tailwindcss-dark-mode')({
darkModeClassName: 'dark-mode-enabled',
})
]
```

### Available Variants

Variants for dark mode are based on [Tailwind's own variants](https://tailwindcss.com/docs/state-variants/)...
Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const selectorParser = require('postcss-selector-parser');

module.exports = function() {
module.exports = function({ darkModeClassName = 'mode-dark' }) {
return function({addVariant, e}) {
addVariant('dark', ({modifySelectors, separator}) => {
modifySelectors(({selector}) => {
return selectorParser((selectors) => {
selectors.walkClasses((sel) => {
sel.value = `${e(`dark${separator}${sel.value}`)}`;
sel.parent.insertBefore(sel, selectorParser().astSync('.mode-dark '));
sel.parent.insertBefore(sel, selectorParser().astSync(`.${darkModeClassName} `));
});
}).processSync(selector);
});
Expand All @@ -23,7 +23,7 @@ module.exports = function() {
return selectorParser((selectors) => {
selectors.walkClasses((sel) => {
sel.value = `${e(`dark-group-hover${separator}${sel.value}`)}`;
sel.parent.insertBefore(sel, selectorParser().astSync('.mode-dark .group:hover '));
sel.parent.insertBefore(sel, selectorParser().astSync(`.${darkModeClassName} .group:hover `));
});
}).processSync(selector);
});
Expand All @@ -35,7 +35,7 @@ module.exports = function() {
return selectorParser((selectors) => {
selectors.walkClasses((sel) => {
sel.value = `${e(`dark-${pseudoClass}${separator}${sel.value}`)}`;
sel.parent.insertBefore(sel, selectorParser().astSync('.mode-dark '));
sel.parent.insertBefore(sel, selectorParser().astSync(`.${darkModeClassName} `));
sel.parent.insertAfter(sel, selectorParser.pseudo({value: `:${pseudoClass}`}));
});
}).processSync(selector);
Expand Down