Skip to content

Lazily evaluate values in the extend section #775

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 1 commit into from
Mar 18, 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
61 changes: 61 additions & 0 deletions __tests__/resolveConfig.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,64 @@ test('the theme function can use a default value if the key is missing', () => {
},
})
})

test('theme values in the extend section are lazily evaluated', () => {
const userConfig = {
theme: {
colors: {
red: 'red',
green: 'green',
blue: 'blue',
},
extend: {
borderColor: theme => ({
default: theme('colors.red'),
}),
},
},
}

const defaultConfig = {
prefix: '-',
important: false,
separator: ':',
theme: {
colors: {
cyan: 'cyan',
magenta: 'magenta',
yellow: 'yellow',
},
borderColor: theme => ({
default: theme('colors.yellow', '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: 'red',
red: 'red',
green: 'green',
blue: 'blue',
},
},
variants: {
borderColor: ['responsive', 'hover', 'focus'],
},
})
})
20 changes: 11 additions & 9 deletions src/util/resolveConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ function resolveFunctionKeys(object) {

function mergeExtensions({ extend, ...theme }) {
return mergeWith({}, theme, extend, (_, extensions, key) => {
return isFunction(theme[key])
? mergedTheme => ({
...theme[key](mergedTheme),
...extensions,
})
: {
...theme[key],
...extensions,
}
if (isFunction(theme[key]) || (extend && isFunction(extend[key]))) {
return mergedTheme => ({
...(isFunction(theme[key]) ? theme[key](mergedTheme) : theme[key]),
...(extend && isFunction(extend[key]) ? extend[key](mergedTheme) : extensions),
})
} else {
return {
...theme[key],
...extensions,
}
}
})
}

Expand Down