Skip to content

Commit 5844bbd

Browse files
committed
css: add support for negative margins
1 parent 424b8e8 commit 5844bbd

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

packages/css/index.js

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,20 @@ const aliases = {
1818
mr: 'marginRight',
1919
mb: 'marginBottom',
2020
ml: 'marginLeft',
21-
mx: [ 'marginLeft', 'marginRight' ],
22-
my: [ 'marginTop', 'marginBottom' ],
23-
marginX: [ 'marginLeft', 'marginRight' ],
24-
marginY: [ 'marginTop', 'marginBottom' ],
21+
mx: 'marginX',
22+
my: 'marginY',
2523
p: 'padding',
2624
pt: 'paddingTop',
2725
pr: 'paddingRight',
2826
pb: 'paddingBottom',
2927
pl: 'paddingLeft',
30-
px: [ 'paddingLeft', 'paddingRight' ],
31-
py: [ 'paddingTop', 'paddingBottom' ],
28+
px: 'paddingX',
29+
py: 'paddingY',
30+
}
31+
32+
const directions = {
33+
marginX: [ 'marginLeft', 'marginRight' ],
34+
marginY: [ 'marginTop', 'marginBottom' ],
3235
paddingX: [ 'paddingLeft', 'paddingRight' ],
3336
paddingY: [ 'paddingTop', 'paddingBottom' ],
3437
}
@@ -42,11 +45,15 @@ const scales = {
4245
marginRight: 'space',
4346
marginBottom: 'space',
4447
marginLeft: 'space',
48+
marginX: 'space',
49+
marginY: 'space',
4550
padding: 'space',
4651
paddingTop: 'space',
4752
paddingRight: 'space',
4853
paddingBottom: 'space',
4954
paddingLeft: 'space',
55+
paddingX: 'space',
56+
paddingY: 'space',
5057
fontFamily: 'fonts',
5158
fontSize: 'fontSizes',
5259
fontWeight: 'fontWeights',
@@ -74,6 +81,26 @@ const scales = {
7481
maxHeight: 'sizes',
7582
}
7683

84+
const getMargin = (scale, value) => {
85+
if (typeof value !== 'number' || value >= 0) {
86+
return get(scale, value, value)
87+
}
88+
const absolute = Math.abs(value)
89+
const n = get(scale, absolute, absolute)
90+
if (typeof n === 'string') return '-' + n
91+
return n * -1
92+
}
93+
94+
const transforms = {
95+
margin: getMargin,
96+
marginTop: getMargin,
97+
marginRight: getMargin,
98+
marginBottom: getMargin,
99+
marginLeft: getMargin,
100+
marginX: getMargin,
101+
marginY: getMargin,
102+
}
103+
77104
export const responsive = styles => theme => {
78105
const next = {}
79106
const breakpoints = get(theme, 'breakpoints', [ '40em', '52em', '64em' ])
@@ -106,8 +133,8 @@ export const css = args => (props = {}) => {
106133
const styles = responsive(obj)(theme)
107134

108135
for (const key in styles) {
109-
const prop = aliases[key] || key
110-
const scaleName = scales[prop] || scales[prop[0]]
136+
const prop = get(aliases, key, key)
137+
const scaleName = get(scales, prop)
111138
const scale = get(theme, scaleName, get(theme, prop, {}))
112139
const x = styles[key]
113140
const val = typeof x === 'function' ? x(theme) : x
@@ -120,10 +147,13 @@ export const css = args => (props = {}) => {
120147
result[prop] = css(val)(theme)
121148
continue
122149
}
123-
const value = get(scale, val, val)
124-
if (Array.isArray(prop)) {
125-
for (let i = 0; i < prop.length; i++) {
126-
result[prop[i]] = value
150+
const transform = get(transforms, prop, get)
151+
const value = transform(scale, val, val)
152+
if (directions[prop]) {
153+
// if (Array.isArray(prop)) {
154+
const dirs = directions[prop]
155+
for (let i = 0; i < dirs.length; i++) {
156+
result[dirs[i]] = value
127157
}
128158
} else {
129159
result[prop] = value

packages/css/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,3 +217,15 @@ test('handles responsive variants', t => {
217217
}
218218
})
219219
})
220+
221+
test('handles negative margins from scale', t => {
222+
const result = css({
223+
mt: -3,
224+
mx: -4,
225+
})(theme)
226+
t.deepEqual(result, {
227+
marginTop: -16,
228+
marginLeft: -32,
229+
marginRight: -32,
230+
})
231+
})

0 commit comments

Comments
 (0)