Skip to content
Merged
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
12 changes: 12 additions & 0 deletions __tests__/withAlphaVariable.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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