This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
33 lines (31 loc) · 1.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const plugin = require('tailwindcss/plugin');
const _ = require('lodash');
const selectorParser = require('postcss-selector-parser');
const childrenVariant = function(pseudoClass = null, childrenSelector = null) {
childrenSelector = childrenSelector ? childrenSelector : (pseudoClass ? `:${pseudoClass}` : '*');
return ({ modifySelectors, separator }) => {
return modifySelectors(({ selector }) => {
return selectorParser(selectors => {
selectors.walkClasses(classNode => {
classNode.value = `children${pseudoClass ? (separator + pseudoClass) : ''}${separator}${classNode.value}`;
classNode.parent.insertAfter(classNode, selectorParser().astSync(` > ${childrenSelector}`));
});
}).processSync(selector);
});
};
};
module.exports = plugin(function({ addVariant }) {
addVariant('children', childrenVariant());
addVariant('children-first', childrenVariant('first', ':first-child'));
addVariant('children-last', childrenVariant('last', ':last-child'));
addVariant('children-odd', childrenVariant('odd', ':nth-child(odd)'));
addVariant('children-even', childrenVariant('even', ':nth-child(even)'));
addVariant('children-not-first', childrenVariant('not-first', ':not(:first-child)'));
addVariant('children-not-last', childrenVariant('not-last', ':not(:last-child)'));
addVariant('children-hover', childrenVariant('hover'));
addVariant('children-focus', childrenVariant('focus'));
addVariant('children-focus-within', childrenVariant('focus-within'));
addVariant('children-active', childrenVariant('active'));
addVariant('children-visited', childrenVariant('visited'));
addVariant('children-disabled', childrenVariant('disabled'));
});