Skip to content

add support for directional border shorthands #95

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

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 32 additions & 0 deletions src/__tests__/border.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,35 @@ it('transforms border shorthand missing color & style', () => {
borderStyle: 'solid',
})
})

it('transforms bottom direction border shorthand', () => {
expect(transformCss([['border-bottom', '2px dashed #f00']])).toEqual({
borderBottomWidth: 2,
borderBottomColor: '#f00',
borderStyle: 'dashed',
})
})

it('transforms left direction border shorthand', () => {
expect(transformCss([['border-left', '2px dashed #f00']])).toEqual({
borderLeftWidth: 2,
borderLeftColor: '#f00',
borderStyle: 'dashed',
})
})

it('transforms right direction border shorthand', () => {
expect(transformCss([['border-right', '2px dashed #f00']])).toEqual({
borderRightWidth: 2,
borderRightColor: '#f00',
borderStyle: 'dashed',
})
})

it('transforms top direction border shorthand', () => {
expect(transformCss([['border-top', '2px dashed #f00']])).toEqual({
borderTopWidth: 2,
borderTopColor: '#f00',
borderStyle: 'dashed',
})
})
41 changes: 27 additions & 14 deletions src/transforms/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,29 @@ const {
const background = tokenStream => ({
$merge: { backgroundColor: tokenStream.expect(COLOR) },
})
const border = anyOrderFactory({
borderWidth: {
tokens: [LENGTH, UNSUPPORTED_LENGTH_UNIT],
default: 1,
},
borderColor: {
tokens: [COLOR],
default: 'black',
},
borderStyle: {
tokens: [regExpToken(/^(solid|dashed|dotted)$/)],
default: 'solid',
},
})

const createBorderFactory = (direction = '') =>
anyOrderFactory({
[`border${direction}Width`]: {
tokens: [LENGTH, UNSUPPORTED_LENGTH_UNIT],
default: 1,
},
[`border${direction}Color`]: {
tokens: [COLOR],
default: 'black',
},
borderStyle: {
tokens: [regExpToken(/^(solid|dashed|dotted)$/)],
default: 'solid',
},
})

const border = createBorderFactory()
const borderTop = createBorderFactory('Top')
const borderBottom = createBorderFactory('Bottom')
const borderLeft = createBorderFactory('Left')
const borderRight = createBorderFactory('Right')

const borderColor = directionFactory({
types: [WORD],
prefix: 'border',
Expand Down Expand Up @@ -70,6 +79,10 @@ const textShadowOffset = shadowOffsetFactory()
export default {
background,
border,
borderTop,
borderBottom,
borderLeft,
borderRight,
borderColor,
borderRadius,
borderWidth,
Expand Down