Skip to content

Commit

Permalink
Merge pull request #1676 from innocenzi/color-closure
Browse files Browse the repository at this point in the history
Allow colors to be defined as closures
  • Loading branch information
adamwathan committed Aug 15, 2020
2 parents 1791768 + dbcac50 commit 7371ea9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
12 changes: 12 additions & 0 deletions __tests__/withAlphaVariable.test.js
Expand Up @@ -95,3 +95,15 @@ test('it ignores colors that already have an alpha channel', () => {
'background-color': 'hsla(240, 100%, 50%, 0.5)',
})
})

test('it allows a closure to be passed', () => {
expect(
withAlphaVariable({
color: ({ opacityVariable }) => `rgba(0, 0, 0, var(${opacityVariable}))`,
property: 'background-color',
variable: '--bg-opacity',
})
).toEqual({
'background-color': 'rgba(0, 0, 0, var(--bg-opacity))',
})
})
7 changes: 7 additions & 0 deletions src/util/withAlphaVariable.js
@@ -1,4 +1,5 @@
import createColor from 'color'
import _ from 'lodash'

function hasAlpha(color) {
return (
Expand All @@ -18,6 +19,12 @@ function toRgba(color) {
}

export default function withAlphaVariable({ color, property, variable }) {
if (_.isFunction(color)) {
return {
[property]: color({ opacityVariable: variable }),
}
}

try {
const [r, g, b, a] = toRgba(color)

Expand Down

0 comments on commit 7371ea9

Please sign in to comment.