Skip to content

Commit 7371ea9

Browse files
authored
Merge pull request #1676 from innocenzi/color-closure
Allow colors to be defined as closures
2 parents 1791768 + dbcac50 commit 7371ea9

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

__tests__/withAlphaVariable.test.js

+12
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,15 @@ test('it ignores colors that already have an alpha channel', () => {
9595
'background-color': 'hsla(240, 100%, 50%, 0.5)',
9696
})
9797
})
98+
99+
test('it allows a closure to be passed', () => {
100+
expect(
101+
withAlphaVariable({
102+
color: ({ opacityVariable }) => `rgba(0, 0, 0, var(${opacityVariable}))`,
103+
property: 'background-color',
104+
variable: '--bg-opacity',
105+
})
106+
).toEqual({
107+
'background-color': 'rgba(0, 0, 0, var(--bg-opacity))',
108+
})
109+
})

src/util/withAlphaVariable.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import createColor from 'color'
2+
import _ from 'lodash'
23

34
function hasAlpha(color) {
45
return (
@@ -18,6 +19,12 @@ function toRgba(color) {
1819
}
1920

2021
export default function withAlphaVariable({ color, property, variable }) {
22+
if (_.isFunction(color)) {
23+
return {
24+
[property]: color({ opacityVariable: variable }),
25+
}
26+
}
27+
2128
try {
2229
const [r, g, b, a] = toRgba(color)
2330

0 commit comments

Comments
 (0)