Skip to content

Make theme a function instead of an object when passed to config closures #774

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
merged 2 commits into from
Mar 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 65 additions & 9 deletions __tests__/resolveConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ test('functions in the default theme section are lazily evaluated', () => {
magenta: 'magenta',
yellow: 'yellow',
},
backgroundColors: ({ colors }) => colors,
textColors: ({ colors }) => colors,
backgroundColors: theme => theme('colors'),
textColors: theme => theme('colors'),
},
variants: {
backgroundColors: ['responsive', 'hover', 'focus'],
Expand Down Expand Up @@ -369,12 +369,12 @@ test('functions in the user theme section are lazily evaluated', () => {
green: 'green',
blue: 'blue',
},
backgroundColors: ({ colors }) => ({
...colors,
backgroundColors: theme => ({
...theme('colors'),
customBackground: '#bada55',
}),
textColors: ({ colors }) => ({
...colors,
textColors: theme => ({
...theme('colors'),
customText: '#facade',
}),
},
Expand Down Expand Up @@ -461,7 +461,7 @@ test('theme values in the extend section extend the existing theme', () => {
'50': '.5',
'100': '1',
},
backgroundColors: ({ colors }) => colors,
backgroundColors: theme => theme('colors'),
},
variants: {
backgroundColors: ['responsive', 'hover', 'focus'],
Expand Down Expand Up @@ -510,7 +510,7 @@ test('theme values in the extend section extend the user theme', () => {
'20': '.2',
'40': '.4',
},
height: theme => theme.width,
height: theme => theme('width'),
extend: {
opacity: {
'60': '.6',
Expand Down Expand Up @@ -618,7 +618,7 @@ test('theme values in the extend section can extend values that are depended on
magenta: 'magenta',
yellow: 'yellow',
},
backgroundColors: ({ colors }) => colors,
backgroundColors: theme => theme('colors'),
},
variants: {
backgroundColors: ['responsive', 'hover', 'focus'],
Expand Down Expand Up @@ -701,3 +701,59 @@ test('theme values in the extend section are not deeply merged', () => {
},
})
})

test('the theme function can use a default value if the key is missing', () => {
const userConfig = {
theme: {
colors: {
red: 'red',
green: 'green',
blue: 'blue',
},
},
}

const defaultConfig = {
prefix: '-',
important: false,
separator: ':',
theme: {
colors: {
cyan: 'cyan',
magenta: 'magenta',
yellow: 'yellow',
},
borderColor: theme => ({
default: theme('colors.gray', 'currentColor'),
...theme('colors'),
}),
},
variants: {
borderColor: ['responsive', 'hover', 'focus'],
},
}

const result = resolveConfig([userConfig, defaultConfig])

expect(result).toEqual({
prefix: '-',
important: false,
separator: ':',
theme: {
colors: {
red: 'red',
green: 'green',
blue: 'blue',
},
borderColor: {
default: 'currentColor',
red: 'red',
green: 'green',
blue: 'blue',
},
},
variants: {
borderColor: ['responsive', 'hover', 'focus'],
},
})
})
5 changes: 4 additions & 1 deletion src/util/resolveConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import mergeWith from 'lodash/mergeWith'
import isFunction from 'lodash/isFunction'
import defaults from 'lodash/defaults'
import map from 'lodash/map'
import get from 'lodash/get'

function resolveFunctionKeys(object) {
const getKey = (key, defaultValue) => get(object, key, defaultValue)

return Object.keys(object).reduce((resolved, key) => {
return {
...resolved,
[key]: isFunction(object[key]) ? object[key](object) : object[key],
[key]: isFunction(object[key]) ? object[key](getKey) : object[key],
}
}, {})
}
Expand Down
16 changes: 8 additions & 8 deletions stubs/defaultConfig.stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ module.exports = {
wider: '.05em',
widest: '.1em',
},
textColor: theme => theme.colors,
backgroundColor: theme => theme.colors,
textColor: theme => theme('colors'),
backgroundColor: theme => theme('colors'),
backgroundPosition: {
bottom: 'bottom',
center: 'center',
Expand All @@ -238,7 +238,7 @@ module.exports = {
'8': '8px',
},
borderColor: theme => {
return global.Object.assign({ default: theme.colors.gray[700] }, theme.colors)
return global.Object.assign({ default: theme('colors.gray.700', 'currentColor') }, theme('colors'))
},
borderRadius: {
none: '0',
Expand All @@ -257,7 +257,7 @@ module.exports = {
},
width: theme => ({
auto: 'auto',
...theme.spacing,
...theme('spacing'),
'1/2': '50%',
'1/3': '33.33333%',
'2/3': '66.66667%',
Expand All @@ -274,7 +274,7 @@ module.exports = {
}),
height: theme => ({
auto: 'auto',
...theme.spacing,
...theme('spacing'),
full: '100%',
screen: '100vh',
}),
Expand Down Expand Up @@ -304,9 +304,9 @@ module.exports = {
full: '100%',
screen: '100vh',
},
padding: theme => theme.spacing,
margin: theme => ({ auto: 'auto', ...theme.spacing }),
negativeMargin: theme => theme.spacing,
padding: theme => theme('spacing'),
margin: theme => ({ auto: 'auto', ...theme('spacing') }),
negativeMargin: theme => theme('spacing'),
objectPosition: {
bottom: 'bottom',
center: 'center',
Expand Down