Skip to content

Commit

Permalink
Allow theme.space to be array *or* object
Browse files Browse the repository at this point in the history
  • Loading branch information
jaridmargolin committed Mar 7, 2019
1 parent a1bf639 commit 0046815
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
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ export const style = ({
} }
func.propTypes[prop].meta = { func.propTypes[prop].meta = {
prop, prop,
themeKey: key themeKey: key,
} }


if (alias) { if (alias) {
func.propTypes[alias] = cloneFunction(propType) func.propTypes[alias] = cloneFunction(propType)
func.propTypes[alias].meta = { func.propTypes[alias].meta = {
prop: alias, 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 spaceScale = [0, 4, 8, 16, 32, 64, 128, 256, 512]


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

const isNegative = n < 0 const isNegative = n < 0
const absolute = Math.abs(n) const absolute = Math.abs(n)
const value = get(scale, absolute) const value = get(scale, absolute)
Expand Down
47 changes: 35 additions & 12 deletions test/styles.js
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,17 @@
import test from 'ava' 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 = { const theme = {
colors: { colors: {
Expand Down Expand Up @@ -142,6 +154,17 @@ test('returns negative string values from theme', t => {
t.deepEqual(styles, [{ margin: '-1em' }]) 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 => { test('pl prop sets paddingLeft', t => {
const styles = space({ pl: 2 }) const styles = space({ pl: 2 })
t.deepEqual(styles, [{ paddingLeft: '8px' }]) 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 => { test('gridGap uses the default scale', t => {
const a = gridGap({ const a = gridGap({
theme: {}, theme: {},
gridGap: 2 gridGap: 2,
}) })


t.deepEqual(a, { gridGap: '8px' }) 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 => { test('gridRowGap uses the default scale', t => {
const a = gridRowGap({ const a = gridRowGap({
theme: {}, theme: {},
gridRowGap: 2 gridRowGap: 2,
}) })


t.deepEqual(a, { gridRowGap: '8px' }) 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 => { test('gridColumnGap uses the default scale', t => {
const a = gridColumnGap({ const a = gridColumnGap({
theme: {}, theme: {},
gridColumnGap: 2 gridColumnGap: 2,
}) })


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

0 comments on commit 0046815

Please sign in to comment.