Skip to content

Commit ea8915b

Browse files
committed
Generate responsive/hover variants based on config instead of hard-coded
1 parent 98d5a55 commit ea8915b

File tree

3 files changed

+95
-42
lines changed

3 files changed

+95
-42
lines changed

defaultConfig.stub.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,49 @@ module.exports = {
770770
options: {
771771
prefix: '',
772772
important: false,
773+
modules: {
774+
appearance: ['responsive'],
775+
backgroundColors: ['responsive', 'hover'],
776+
backgroundPosition: ['responsive'],
777+
backgroundSize: ['responsive'],
778+
borderColors: ['responsive', 'hover'],
779+
borderRadius: ['responsive'],
780+
borderStyle: ['responsive'],
781+
borderWidths: ['responsive'],
782+
cursor: ['responsive'],
783+
display: ['responsive'],
784+
flexbox: ['responsive'],
785+
float: ['responsive'],
786+
fonts: ['responsive'],
787+
fontWeights: ['responsive', 'hover'],
788+
height: ['responsive'],
789+
leading: ['responsive'],
790+
lists: ['responsive'],
791+
margin: ['responsive'],
792+
maxHeight: ['responsive'],
793+
maxWidth: ['responsive'],
794+
minHeight: ['responsive'],
795+
minWidth: ['responsive'],
796+
negativeMargin: ['responsive'],
797+
opacity: ['responsive'],
798+
overflow: ['responsive'],
799+
padding: ['responsive'],
800+
pointerEvents: ['responsive'],
801+
position: ['responsive'],
802+
resize: ['responsive'],
803+
shadows: ['responsive'],
804+
textAlign: ['responsive'],
805+
textColors: ['responsive', 'hover'],
806+
textSizes: ['responsive'],
807+
textStyle: ['responsive', 'hover'],
808+
tracking: ['responsive'],
809+
userSelect: ['responsive'],
810+
verticalAlign: ['responsive'],
811+
visibility: ['responsive'],
812+
whitespace: ['responsive'],
813+
width: ['responsive'],
814+
zIndex: ['responsive'],
815+
}
773816
},
774817

775818
}

src/lib/generateUtilities.js

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ import whitespace from '../generators/whitespace'
4747
import width from '../generators/width'
4848
import zIndex from '../generators/zIndex'
4949

50+
function withVariants(module, variants) {
51+
return _.reduce(variants, (module, variant) => {
52+
return {
53+
responsive: responsive,
54+
hover: hoverable,
55+
}[variant](module)
56+
}, module)
57+
}
58+
5059
export default function(config) {
5160
return function(css) {
5261
const unwrappedConfig = config()
@@ -57,47 +66,47 @@ export default function(config) {
5766
nodes: _.flatten([
5867
// The `lists` module needs to be first to allow overriding the margin and
5968
// padding values that it sets with other utilities.
60-
responsive(lists(unwrappedConfig)),
61-
responsive(appearance(unwrappedConfig)),
62-
responsive(hoverable(backgroundColors(unwrappedConfig))),
63-
responsive(backgroundPosition(unwrappedConfig)),
64-
responsive(backgroundSize(unwrappedConfig)),
65-
responsive(hoverable(borderColors(unwrappedConfig))),
66-
responsive(borderRadius(unwrappedConfig)),
67-
responsive(borderStyle(unwrappedConfig)),
68-
responsive(borderWidths(unwrappedConfig)),
69-
responsive(cursor(unwrappedConfig)),
70-
responsive(display(unwrappedConfig)),
71-
responsive(flexbox(unwrappedConfig)),
72-
responsive(float(unwrappedConfig)),
73-
responsive(fonts(unwrappedConfig)),
74-
responsive(hoverable(fontWeights(unwrappedConfig))),
75-
responsive(height(unwrappedConfig)),
76-
responsive(leading(unwrappedConfig)),
77-
responsive(margin(unwrappedConfig)),
78-
responsive(maxHeight(unwrappedConfig)),
79-
responsive(maxWidth(unwrappedConfig)),
80-
responsive(minHeight(unwrappedConfig)),
81-
responsive(minWidth(unwrappedConfig)),
82-
responsive(negativeMargin(unwrappedConfig)),
83-
responsive(opacity(unwrappedConfig)),
84-
responsive(overflow(unwrappedConfig)),
85-
responsive(padding(unwrappedConfig)),
86-
responsive(pointerEvents(unwrappedConfig)),
87-
responsive(position(unwrappedConfig)),
88-
responsive(resize(unwrappedConfig)),
89-
responsive(shadows(unwrappedConfig)),
90-
responsive(textAlign(unwrappedConfig)),
91-
responsive(hoverable(textColors(unwrappedConfig))),
92-
responsive(textSizes(unwrappedConfig)),
93-
responsive(hoverable(textStyle(unwrappedConfig))),
94-
responsive(tracking(unwrappedConfig)),
95-
responsive(userSelect(unwrappedConfig)),
96-
responsive(verticalAlign(unwrappedConfig)),
97-
responsive(visibility(unwrappedConfig)),
98-
responsive(whitespace(unwrappedConfig)),
99-
responsive(width(unwrappedConfig)),
100-
responsive(zIndex(unwrappedConfig)),
69+
withVariants(lists(unwrappedConfig), unwrappedConfig.options.modules.lists),
70+
withVariants(appearance(unwrappedConfig), unwrappedConfig.options.modules.appearance),
71+
withVariants(backgroundColors(unwrappedConfig), unwrappedConfig.options.modules.backgroundColors),
72+
withVariants(backgroundPosition(unwrappedConfig), unwrappedConfig.options.modules.backgroundPosition),
73+
withVariants(backgroundSize(unwrappedConfig), unwrappedConfig.options.modules.backgroundSize),
74+
withVariants(borderColors(unwrappedConfig), unwrappedConfig.options.modules.borderColors),
75+
withVariants(borderRadius(unwrappedConfig), unwrappedConfig.options.modules.borderRadius),
76+
withVariants(borderStyle(unwrappedConfig), unwrappedConfig.options.modules.borderStyle),
77+
withVariants(borderWidths(unwrappedConfig), unwrappedConfig.options.modules.borderWidths),
78+
withVariants(cursor(unwrappedConfig), unwrappedConfig.options.modules.cursor),
79+
withVariants(display(unwrappedConfig), unwrappedConfig.options.modules.display),
80+
withVariants(flexbox(unwrappedConfig), unwrappedConfig.options.modules.flexbox),
81+
withVariants(float(unwrappedConfig), unwrappedConfig.options.modules.float),
82+
withVariants(fonts(unwrappedConfig), unwrappedConfig.options.modules.fonts),
83+
withVariants(fontWeights(unwrappedConfig), unwrappedConfig.options.modules.fontWeights),
84+
withVariants(height(unwrappedConfig), unwrappedConfig.options.modules.height),
85+
withVariants(leading(unwrappedConfig), unwrappedConfig.options.modules.leading),
86+
withVariants(margin(unwrappedConfig), unwrappedConfig.options.modules.margin),
87+
withVariants(maxHeight(unwrappedConfig), unwrappedConfig.options.modules.maxHeight),
88+
withVariants(maxWidth(unwrappedConfig), unwrappedConfig.options.modules.maxWidth),
89+
withVariants(minHeight(unwrappedConfig), unwrappedConfig.options.modules.minHeight),
90+
withVariants(minWidth(unwrappedConfig), unwrappedConfig.options.modules.minWidth),
91+
withVariants(negativeMargin(unwrappedConfig), unwrappedConfig.options.modules.negativeMargin),
92+
withVariants(opacity(unwrappedConfig), unwrappedConfig.options.modules.opacity),
93+
withVariants(overflow(unwrappedConfig), unwrappedConfig.options.modules.overflow),
94+
withVariants(padding(unwrappedConfig), unwrappedConfig.options.modules.padding),
95+
withVariants(pointerEvents(unwrappedConfig), unwrappedConfig.options.modules.pointerEvents),
96+
withVariants(position(unwrappedConfig), unwrappedConfig.options.modules.position),
97+
withVariants(resize(unwrappedConfig), unwrappedConfig.options.modules.resize),
98+
withVariants(shadows(unwrappedConfig), unwrappedConfig.options.modules.shadows),
99+
withVariants(textAlign(unwrappedConfig), unwrappedConfig.options.modules.textAlign),
100+
withVariants(textColors(unwrappedConfig), unwrappedConfig.options.modules.textColors),
101+
withVariants(textSizes(unwrappedConfig), unwrappedConfig.options.modules.textSizes),
102+
withVariants(textStyle(unwrappedConfig), unwrappedConfig.options.modules.textStyle),
103+
withVariants(tracking(unwrappedConfig), unwrappedConfig.options.modules.tracking),
104+
withVariants(userSelect(unwrappedConfig), unwrappedConfig.options.modules.userSelect),
105+
withVariants(verticalAlign(unwrappedConfig), unwrappedConfig.options.modules.verticalAlign),
106+
withVariants(visibility(unwrappedConfig), unwrappedConfig.options.modules.visibility),
107+
withVariants(whitespace(unwrappedConfig), unwrappedConfig.options.modules.whitespace),
108+
withVariants(width(unwrappedConfig), unwrappedConfig.options.modules.width),
109+
withVariants(zIndex(unwrappedConfig), unwrappedConfig.options.modules.zIndex),
101110
]),
102111
})
103112

src/util/hoverable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import _ from 'lodash'
12
import postcss from 'postcss'
23
import cloneNodes from './cloneNodes'
34

@@ -6,5 +7,5 @@ export default function hoverable(rules) {
67
.atRule({
78
name: 'hoverable',
89
})
9-
.append(cloneNodes(rules))
10+
.append(cloneNodes(_.isArray(rules) ? rules : [rules]))
1011
}

0 commit comments

Comments
 (0)