Skip to content

Commit

Permalink
Merge pull request #417 from jaridmargolin/space-object
Browse files Browse the repository at this point in the history
Allow theme.space to be array *or* object
  • Loading branch information
jxnblk committed Mar 11, 2019
2 parents a1bf639 + 0046815 commit 573956d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/index.js
Expand Up @@ -91,14 +91,14 @@ export const style = ({
}
func.propTypes[prop].meta = {
prop,
themeKey: key
themeKey: key,
}

if (alias) {
func.propTypes[alias] = cloneFunction(propType)
func.propTypes[alias].meta = {
prop: alias,
themeKey: key
themeKey: key,
}
}

Expand Down Expand Up @@ -136,7 +136,10 @@ export const variant = ({ key, prop = 'variant' }) => {
const spaceScale = [0, 4, 8, 16, 32, 64, 128, 256, 512]

const getSpace = (n, scale) => {
if (!num(n)) return n
if (!num(n)) {
return px(get(scale, n, n))
}

const isNegative = n < 0
const absolute = Math.abs(n)
const value = get(scale, absolute)
Expand Down
47 changes: 35 additions & 12 deletions test/styles.js
@@ -1,5 +1,17 @@
import test from 'ava'
import { space, color, width, fontSize, size, gridGap, gridRowGap, gridColumnGap, buttonStyle, textStyle, colorStyle } from '../src'
import {
space,
color,
width,
fontSize,
size,
gridGap,
gridRowGap,
gridColumnGap,
buttonStyle,
textStyle,
colorStyle,
} from '../src'

const theme = {
colors: {
Expand Down Expand Up @@ -142,6 +154,17 @@ test('returns negative string values from theme', t => {
t.deepEqual(styles, [{ margin: '-1em' }])
})

test('returns values from theme object', t => {
const styles = space({
theme: {
space: { sm: 1 },
},
margin: 'sm',
})

t.deepEqual(styles, [{ margin: '1px' }])
})

test('pl prop sets paddingLeft', t => {
const styles = space({ pl: 2 })
t.deepEqual(styles, [{ paddingLeft: '8px' }])
Expand Down Expand Up @@ -214,7 +237,7 @@ test('gridGap returns a scalar style', t => {
test('gridGap uses the default scale', t => {
const a = gridGap({
theme: {},
gridGap: 2
gridGap: 2,
})

t.deepEqual(a, { gridGap: '8px' })
Expand All @@ -234,7 +257,7 @@ test('gridRowGap returns a scalar style', t => {
test('gridRowGap uses the default scale', t => {
const a = gridRowGap({
theme: {},
gridRowGap: 2
gridRowGap: 2,
})

t.deepEqual(a, { gridRowGap: '8px' })
Expand All @@ -254,7 +277,7 @@ test('gridColumnGap returns a scalar style', t => {
test('gridColumnGap uses the default scale', t => {
const a = gridColumnGap({
theme: {},
gridColumnGap: 2
gridColumnGap: 2,
})

t.deepEqual(a, { gridColumnGap: '8px' })
Expand All @@ -267,10 +290,10 @@ test('textStyle prop returns theme.textStyles object', t => {
heading: {
fontWeight: 'bold',
lineHeight: 1.25,
}
}
},
},
},
textStyle: 'heading'
textStyle: 'heading',
})
t.deepEqual(a, {
fontWeight: 'bold',
Expand All @@ -284,14 +307,14 @@ test('colors prop returns theme.colorStyles object', t => {
colorStyles: {
dark: {
color: '#fff',
backgroundColor: '#000'
}
}
backgroundColor: '#000',
},
},
},
colors: 'dark'
colors: 'dark',
})
t.deepEqual(a, {
color: '#fff',
backgroundColor: '#000'
backgroundColor: '#000',
})
})

0 comments on commit 573956d

Please sign in to comment.