Skip to content

Commit

Permalink
feat: allow for deeply nested color objects (#2148)
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi authored Oct 25, 2020
1 parent 15d6fc4 commit 58a6008
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions __tests__/flattenColorPalette.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
})
})
2 changes: 1 addition & 1 deletion src/util/flattenColorPalette.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
})
Expand Down

0 comments on commit 58a6008

Please sign in to comment.