Skip to content

Commit e8b64fd

Browse files
committed
Move negative margin logic into a helper
Adds a new `utils` bucket that's passed as a second arg when using a closure for theme values. The idea is you can destructure useful helper functions out of this argument, in this case a `negative` function that converts a positive scale to negative values. That's the only helper function right now, but making it a destructurable arg so we can add more if necessary without adding a bunch of positional arguments.
1 parent 2d91aa8 commit e8b64fd

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/util/resolveConfig.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import defaults from 'lodash/defaults'
44
import map from 'lodash/map'
55
import get from 'lodash/get'
66

7+
const utils = {
8+
negative: function (scale) {
9+
return Object.keys(scale)
10+
.filter(key => scale[key] !== '0')
11+
.reduce((negativeScale, key) => ({
12+
...negativeScale,
13+
[`-${key}`]: `-${scale[key]}`
14+
}), {})
15+
}
16+
}
17+
718
function value(valueToResolve, ...args) {
819
return isFunction(valueToResolve) ? valueToResolve(...args) : valueToResolve
920
}
@@ -33,7 +44,7 @@ function resolveFunctionKeys(object) {
3344
return Object.keys(object).reduce((resolved, key) => {
3445
return {
3546
...resolved,
36-
[key]: isFunction(object[key]) ? object[key](resolveObjectPath) : object[key],
47+
[key]: isFunction(object[key]) ? object[key](resolveObjectPath, utils) : object[key],
3748
}
3849
}, {})
3950
}

stubs/defaultConfig.stub.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -305,20 +305,11 @@ module.exports = {
305305
screen: '100vh',
306306
},
307307
padding: theme => theme('spacing'),
308-
margin: theme => {
309-
const negativeSpacing = Object.keys(theme('spacing'))
310-
.filter(key => theme('spacing')[key] !== '0')
311-
.reduce((negativeSpacing, key) => ({
312-
...negativeSpacing,
313-
[`-${key}`]: `-${theme('spacing')[key]}`
314-
}), {})
315-
316-
return {
317-
auto: 'auto',
318-
...theme('spacing'),
319-
...negativeSpacing,
320-
}
321-
},
308+
margin: (theme, { negative }) => ({
309+
auto: 'auto',
310+
...theme('spacing'),
311+
...negative(theme('spacing')),
312+
}),
322313
objectPosition: {
323314
bottom: 'bottom',
324315
center: 'center',

0 commit comments

Comments
 (0)