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

Added nested colors in theme. #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions src/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,51 @@ describe('makeTheme', () => {
new Color(colors.red).darken(0.1).toString()
)
})

it('extracts theme structure', () => {
const exampleTheme = {
background: 'white',
color: 'red',
ui: {
background: 'gray',
color: 'red'
}
}

const theme = makeTheme(exampleTheme)

const colorSelector = theme.color
expect(colorSelector).toBeA('function')
expect(colorSelector.length).toBe(1)

const uiColorSelector = theme.ui.color
expect(uiColorSelector).toBeA('function')
expect(uiColorSelector.length).toBe(1)

const themeFromProvider = {
theme: {
background: '#FFFFFF',
color: '#2A2A2A',
ui: {
background: '#000000',
color: '#9B1919'
}
}
}

expect(colorSelector(themeFromProvider))
.toBe(themeFromProvider.theme.color)
expect(uiColorSelector(themeFromProvider))
.toBe(themeFromProvider.theme.ui.color)

const darkerColor = colorSelector.darken(0.1)
const lighterUIColor = uiColorSelector.lighten(0.4)

expect(darkerColor(themeFromProvider)).toBe(
new Color(themeFromProvider.theme.color).darken(0.1).toString()
)
expect(lighterUIColor(themeFromProvider)).toBe(
new Color(themeFromProvider.theme.ui.color).lighten(0.4).toString()
)
})
})
25 changes: 21 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import makeThemeColor from './makeThemeColor'

const makeTheme = (...colors) =>
colors.reduce((result, color) => {
result[color] = makeThemeColor(color)
const makeTheme = (theme, ...themePath) => {
if (typeof(theme) !== 'object') {
// compat mode
// make theme object with keys of all arguments
theme = [theme, ...themePath].reduce(
(result, color) => {
result[color] = ''
return result
}, {})
return makeTheme(theme)
}

themePath = themePath.length ? themePath : ['theme']

return Object.entries(theme).reduce((result, [key, item]) => {
if (typeof(item) === 'object') {
result[key] = makeTheme(item, ...themePath, key)
} else {
result[key] = makeThemeColor(...themePath, key)
}
return result
}, {})
}

export { makeThemeColor }

export default makeTheme
7 changes: 6 additions & 1 deletion src/makeThemeColor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import decorateSelector from './decorateSelector'

const makeThemeColor = key => decorateSelector(props => props.theme[key])
const makeThemeColor = (...propsPath) =>
decorateSelector(props => {
propsPath.forEach(key => props = props[key])
return props
})


export default makeThemeColor