Skip to content

Commit 4b03dfc

Browse files
committed
fix: support negative values from theme objects
Fixes #27
1 parent 23a3aa0 commit 4b03dfc

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

packages/emotion/src/styled.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ describe('#styled', () => {
5252
expect(container.firstChild).toHaveStyle('color: pink;')
5353
})
5454

55+
it('handles negative values', () => {
56+
const theme = {
57+
space: {
58+
md: 10,
59+
},
60+
}
61+
const Dummy = styled.div`
62+
margin: -md;
63+
`
64+
const { container } = render(
65+
<ThemeProvider theme={theme}>
66+
<Dummy />
67+
</ThemeProvider>,
68+
)
69+
expect(container.firstChild).toHaveStyle('margin: -10px;')
70+
})
71+
5572
it('works with css as object', () => {
5673
const Dummy = styled.div({
5774
margin: '2',

packages/styled-components/src/styled.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ describe('#styled', () => {
5252
expect(container.firstChild).toHaveStyle('color: pink;')
5353
})
5454

55+
it('handles negative values', () => {
56+
const theme = {
57+
space: {
58+
md: 10,
59+
},
60+
}
61+
const Dummy = styled.div`
62+
margin: -md;
63+
`
64+
const { container } = render(
65+
<ThemeProvider theme={theme}>
66+
<Dummy />
67+
</ThemeProvider>,
68+
)
69+
expect(container.firstChild).toHaveStyle('margin: -10px;')
70+
})
71+
5572
it('works with css as object', () => {
5673
const Dummy = styled.div({
5774
margin: '2',

packages/system/src/styles/space.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import { style, themeGetter, compose } from '../style'
2-
import { num, negative } from '../util'
2+
import { is, string, negative } from '../util'
33
import { px as pxUnit } from '../unit'
44

5+
function toNegative(value) {
6+
if (string(value)) return `-${value}`
7+
return value * -1
8+
}
9+
510
export const getSpace = themeGetter({
611
key: 'space',
712
defaultVariants: [0, 4, 8, 16, 24, 48, 96, 144, 192, 240],
813
transform: (_, { rawValue, variants }) => {
9-
if (!num(rawValue)) {
10-
return variants[rawValue] || rawValue
14+
if (string(rawValue)) {
15+
const neg = rawValue.startsWith('-')
16+
const variantKey = neg ? rawValue.substr(1) : rawValue
17+
const variantValue = variants[variantKey]
18+
const value = is(variantValue) ? variantValue : rawValue
19+
return pxUnit(neg ? toNegative(value) : value)
1120
}
1221
const abs = Math.abs(rawValue)
1322
const neg = negative(rawValue)
14-
const value = variants[abs] || abs
15-
if (!num(value)) {
16-
return pxUnit(neg ? `-${value}` : value)
17-
}
18-
return pxUnit(value * (neg ? -1 : 1))
23+
const value = is(variants[abs]) ? variants[abs] : abs
24+
return pxUnit(neg ? toNegative(value) : value)
1925
},
2026
})
2127

packages/system/src/styles/space.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ describe('#getSpace', () => {
2020
expect(getSpace(-10)(props)).toEqual('-10px')
2121
expect(getSpace('50%')(props)).toEqual('50%')
2222
})
23+
24+
it('gets objects value from the theme', () => {
25+
const props = { theme: { space: { md: 10 } } }
26+
expect(getSpace('md')(props)).toEqual('10px')
27+
expect(getSpace('-md')(props)).toEqual('-10px')
28+
})
2329
})
2430

2531
describe('#space', () => {

0 commit comments

Comments
 (0)