Skip to content

Commit c5c02d0

Browse files
authored
Merge pull request styled-components#78 from kristerkari/support-auto-for-margin-shorthand
Support using "auto" when using margin shorthand
2 parents b41f4f4 + c1e8966 commit c5c02d0

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/__tests__/boxModel.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,39 @@ it('transforms shorthand and overrides previous values', () => {
8484
})
8585
})
8686

87+
it('transforms margin shorthand with auto', () => {
88+
expect(transformCss([['margin', 'auto']])).toEqual({
89+
marginTop: 'auto',
90+
marginRight: 'auto',
91+
marginBottom: 'auto',
92+
marginLeft: 'auto',
93+
})
94+
expect(transformCss([['margin', '0 auto']])).toEqual({
95+
marginTop: 0,
96+
marginRight: 'auto',
97+
marginBottom: 0,
98+
marginLeft: 'auto',
99+
})
100+
expect(transformCss([['margin', 'auto 0']])).toEqual({
101+
marginTop: 'auto',
102+
marginRight: 0,
103+
marginBottom: 'auto',
104+
marginLeft: 0,
105+
})
106+
expect(transformCss([['margin', '2px 3px auto']])).toEqual({
107+
marginTop: 2,
108+
marginRight: 3,
109+
marginBottom: 'auto',
110+
marginLeft: 3,
111+
})
112+
expect(transformCss([['margin', '10px auto 4px']])).toEqual({
113+
marginTop: 10,
114+
marginRight: 'auto',
115+
marginBottom: 4,
116+
marginLeft: 'auto',
117+
})
118+
})
119+
87120
it('transforms border width', () => {
88121
expect(transformCss([['border-width', '1px 2px 3px 4px']])).toEqual({
89122
borderTopWidth: 1,

src/transforms/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import textDecorationLine from './textDecorationLine'
99
import transform from './transform'
1010
import { directionFactory, anyOrderFactory, shadowOffsetFactory } from './util'
1111

12-
const { IDENT, WORD, COLOR } = tokens
12+
const { IDENT, WORD, COLOR, LENGTH, PERCENT, AUTO } = tokens
1313

1414
const background = tokenStream => ({
1515
$merge: { backgroundColor: tokenStream.expect(COLOR) },
@@ -39,7 +39,10 @@ const borderRadius = directionFactory({
3939
suffix: 'Radius',
4040
})
4141
const borderWidth = directionFactory({ prefix: 'border', suffix: 'Width' })
42-
const margin = directionFactory({ prefix: 'margin' })
42+
const margin = directionFactory({
43+
types: [LENGTH, PERCENT, AUTO],
44+
prefix: 'margin',
45+
})
4346
const padding = directionFactory({ prefix: 'padding' })
4447
const flexFlow = anyOrderFactory({
4548
flexWrap: {

0 commit comments

Comments
 (0)