feat: allow for deeply nested color objects (#2148)

This commit is contained in:
Enzo Innocenzi 2020-10-25 19:27:57 +01:00 committed by GitHub
parent 15d6fc4715
commit 58a600816c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -43,3 +43,35 @@ test('it flattens nested color objects', () => {
'blue-3': 'rgb(0,0,100)',
})
})
test('it flattens deeply nested color objects', () => {
expect(
flattenColorPalette({
primary: 'purple',
secondary: {
DEFAULT: 'blue',
hover: 'cyan',
focus: 'red',
},
button: {
primary: {
DEFAULT: 'magenta',
hover: 'green',
focus: {
DEFAULT: 'yellow',
variant: 'orange',
},
},
},
})
).toEqual({
primary: 'purple',
secondary: 'blue',
'secondary-hover': 'cyan',
'secondary-focus': 'red',
'button-primary': 'magenta',
'button-primary-hover': 'green',
'button-primary-focus': 'yellow',
'button-primary-focus-variant': 'orange',
})
})

View File

@ -7,7 +7,7 @@ export default function flattenColorPalette(colors) {
return [[name, color]]
}
return _.map(color, (value, key) => {
return _.map(flattenColorPalette(color), (value, key) => {
const suffix = key === 'DEFAULT' ? '' : `-${key}`
return [`${name}${suffix}`, value]
})