Skip to content

Commit cc968d1

Browse files
committed
Fix conflics, refactor variant generator
2 parents 79b09dc + b7cb213 commit cc968d1

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
lines changed

__tests__/variantsAtRule.test.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ test('it can generate hover variants', () => {
2727
})
2828
})
2929

30+
test('it can generate active variants', () => {
31+
const input = `
32+
@variants active {
33+
.banana { color: yellow; }
34+
.chocolate { color: brown; }
35+
}
36+
`
37+
38+
const output = `
39+
.banana { color: yellow; }
40+
.chocolate { color: brown; }
41+
.active\\:banana:active { color: yellow; }
42+
.active\\:chocolate:active { color: brown; }
43+
`
44+
45+
return run(input).then(result => {
46+
expect(result.css).toMatchCss(output)
47+
expect(result.warnings().length).toBe(0)
48+
})
49+
})
50+
3051
test('it can generate focus variants', () => {
3152
const input = `
3253
@variants focus {
@@ -69,9 +90,9 @@ test('it can generate group-hover variants', () => {
6990
})
7091
})
7192

72-
test('it can generate all variants', () => {
93+
test('it can generate hover, active and focus variants', () => {
7394
const input = `
74-
@variants hover, focus, group-hover {
95+
@variants hover, active, group-hover, focus {
7596
.banana { color: yellow; }
7697
.chocolate { color: brown; }
7798
}
@@ -86,6 +107,8 @@ test('it can generate all variants', () => {
86107
.hover\\:chocolate:hover { color: brown; }
87108
.focus\\:banana:focus { color: yellow; }
88109
.focus\\:chocolate:focus { color: brown; }
110+
.active\\:banana:active { color: yellow; }
111+
.active\\:chocolate:active { color: brown; }
89112
`
90113

91114
return run(input).then(result => {

defaultConfig.stub.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,8 @@ module.exports = {
824824
| Here is where you control which modules are generated and what variants are
825825
| generated for each of those modules.
826826
|
827-
| Currently supported variants: 'responsive', 'hover', 'focus', 'group-hover'
827+
| Currently supported variants: 'responsive', 'hover', 'active', focus',
828+
| 'group-hover'
828829
|
829830
| To disable a module completely, use `false` instead of an array.
830831
|

src/lib/substituteVariantsAtRules.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,35 @@ import _ from 'lodash'
22
import postcss from 'postcss'
33
import buildClassVariant from '../util/buildClassVariant'
44

5-
const variantGenerators = {
6-
hover: (container, config) => {
7-
const cloned = container.clone()
8-
9-
cloned.walkRules(rule => {
10-
rule.selector = `${buildClassVariant(rule.selector, 'hover', config.options.separator)}:hover`
11-
})
5+
function buildPseudoClassVariant(selector, pseudoClass, separator) {
6+
return `${buildClassVariant(selector, pseudoClass, separator)}:${pseudoClass}`
7+
}
128

13-
container.before(cloned.nodes)
14-
},
15-
focus: (container, config) => {
9+
function generatePseudoClassVariant(pseudoClass) {
10+
return (container, config) => {
1611
const cloned = container.clone()
1712

1813
cloned.walkRules(rule => {
19-
rule.selector = `${buildClassVariant(rule.selector, 'focus', config.options.separator)}:focus`
14+
rule.selector = buildPseudoClassVariant(rule.selector, pseudoClass, config.options.separator)
2015
})
2116

2217
container.before(cloned.nodes)
23-
},
24-
'group-hover': (container, config) => {
18+
}
19+
}
20+
21+
const variantGenerators = {
22+
'group-hover': (container, { options: { separator } }) => {
2523
const cloned = container.clone()
2624

2725
cloned.walkRules(rule => {
28-
// prettier-ignore
29-
rule.selector = `.group:hover ${buildClassVariant(rule.selector, 'group-hover', config.options.separator)}`
26+
rule.selector = `.group:hover ${buildClassVariant(rule.selector, 'group-hover', separator)}`
3027
})
3128

3229
container.before(cloned.nodes)
3330
},
31+
hover: generatePseudoClassVariant('hover'),
32+
focus: generatePseudoClassVariant('focus'),
33+
active: generatePseudoClassVariant('active'),
3434
}
3535

3636
export default function(config) {
@@ -48,7 +48,7 @@ export default function(config) {
4848

4949
atRule.before(atRule.clone().nodes)
5050

51-
_.forEach(['group-hover', 'hover', 'focus'], variant => {
51+
_.forEach(['group-hover', 'hover', 'focus', 'active'], variant => {
5252
if (variants.includes(variant)) {
5353
variantGenerators[variant](atRule, unwrappedConfig)
5454
}

0 commit comments

Comments
 (0)