Skip to content
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

Allow theme.space to be array *or* object #417

Merged
merged 1 commit into from Mar 11, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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',
})
})