Skip to content

Support using "auto" when using margin shorthand #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/__tests__/boxModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,39 @@ it('transforms shorthand and overrides previous values', () => {
})
})

it('transforms margin shorthand with auto', () => {
expect(transformCss([['margin', 'auto']])).toEqual({
marginTop: 'auto',
marginRight: 'auto',
marginBottom: 'auto',
marginLeft: 'auto',
})
expect(transformCss([['margin', '0 auto']])).toEqual({
marginTop: 0,
marginRight: 'auto',
marginBottom: 0,
marginLeft: 'auto',
})
expect(transformCss([['margin', 'auto 0']])).toEqual({
marginTop: 'auto',
marginRight: 0,
marginBottom: 'auto',
marginLeft: 0,
})
expect(transformCss([['margin', '2px 3px auto']])).toEqual({
marginTop: 2,
marginRight: 3,
marginBottom: 'auto',
marginLeft: 3,
})
expect(transformCss([['margin', '10px auto 4px']])).toEqual({
marginTop: 10,
marginRight: 'auto',
marginBottom: 4,
marginLeft: 'auto',
})
})

it('transforms border width', () => {
expect(transformCss([['border-width', '1px 2px 3px 4px']])).toEqual({
borderTopWidth: 1,
Expand Down
7 changes: 5 additions & 2 deletions src/transforms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import textDecorationLine from './textDecorationLine'
import transform from './transform'
import { directionFactory, anyOrderFactory, shadowOffsetFactory } from './util'

const { IDENT, WORD, COLOR } = tokens
const { IDENT, WORD, COLOR, LENGTH, PERCENT, AUTO } = tokens

const background = tokenStream => ({
$merge: { backgroundColor: tokenStream.expect(COLOR) },
Expand Down Expand Up @@ -39,7 +39,10 @@ const borderRadius = directionFactory({
suffix: 'Radius',
})
const borderWidth = directionFactory({ prefix: 'border', suffix: 'Width' })
const margin = directionFactory({ prefix: 'margin' })
const margin = directionFactory({
types: [LENGTH, PERCENT, AUTO],
prefix: 'margin',
})
const padding = directionFactory({ prefix: 'padding' })
const flexFlow = anyOrderFactory({
flexWrap: {
Expand Down