Skip to content

Commit fe8a144

Browse files
author
Sam Goody
committed
Added psuedo-class support (eg. child-hover)
1 parent d662474 commit fe8a144

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

src/index.js

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,59 @@
11
import { default as plugin } from 'tailwindcss/plugin'
22

3-
const aliases = {
4-
"& > .": ['children', 'child'],
5-
"& ." : ['heir', 'descendant'],
6-
"& ~ .": ['sibling'],
7-
}
3+
const aliases = Object.entries({
4+
"& >": ['children', 'child'],
5+
"&" : ['heir', 'descendant'],
6+
"& ~": ['sibling', 'twin'],
7+
})
88

9-
const variants = Object.entries(aliases).
10-
flatMap(([k, v]) => v.map(i => [i, k + i]));
9+
// Handle all tailwind psuedoclasses, see:
10+
// https://tailwindcss.com/docs/hover-focus-and-other-states#quick-reference
11+
const psuedo = [
12+
... [
13+
'active', 'focus', 'focus-within', 'focus-visible', 'hover', 'disabled', // Interactive
14+
'first-of-type', 'last-of-type', 'only-of-type', // Positional
15+
'autofill', 'checked', 'default', 'indeterminate', 'placeholder-shown', // Forms
16+
'required', 'valid', 'invalid', 'in-range', 'out-of-range', 'read-only', // Forms
17+
'empty', 'visited', 'target', // State
18+
].map(v => [v, v]),
19+
... [ // Pseudo-elements
20+
'before', 'after', 'first-letter', 'first-line', 'marker', 'selection', 'placeholder'
21+
].map(v => [v, `:${v}`]),
22+
... [ // Attributes
23+
['open', 'open'],
24+
['rtl', 'dir="rtl"'],
25+
['ltr', 'dir="ltr"'],
26+
].map(v => [v[0], '', `[${v[1]}]`]),
27+
... [ // Positional
28+
['first', 'first-child'],
29+
['last', 'last-child'],
30+
['only', 'only-child'],
31+
['odd', 'nth-child(odd)'],
32+
['even', 'nth-child(even)'],
33+
['not-first', 'not(:first-child)'],
34+
['not-last', 'not(:last-child)'],
35+
['file', 'file-selector-button'],
36+
],
37+
]
38+
39+
// convert ['hover', 'hover'] -> ['children-hover', '& > .children:hover']
40+
let variants = psuedo.flatMap(([pstate, pclass, pselector]) =>
41+
aliases.flatMap(([prefix, newvariants]) =>
42+
newvariants.map(newvariant => [
43+
`${newvariant}-${pstate}`,
44+
`${prefix} .${newvariant}` + (pselector || `:${pclass}`)
45+
])))
46+
47+
let basic = aliases
48+
.flatMap(([k, v]) => v.map(i => [i, `${k} .${i}`]));
49+
50+
variants.unshift(...basic);
51+
52+
// console.log(variants)
1153

1254
export default plugin(({ addVariant }) =>
13-
variants.forEach(v => addVariant.apply(null, v)));
55+
variants.forEach(v => addVariant(...v)));
56+
57+
// More complete list of Psuedo
58+
// https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes
59+
// https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements

0 commit comments

Comments
 (0)