Fix formatting

This commit is contained in:
Hunter Tunnicliff 2021-08-04 20:56:04 -07:00 committed by Robin Malfait
parent 70787fc088
commit 721e57312b
No known key found for this signature in database
GPG Key ID: 92F53D68B9041AFE
2 changed files with 17 additions and 10 deletions

View File

@ -37,23 +37,20 @@ export default function withAlphaVariable({ color, property, variable }) {
}
if (isValidColor(color)) {
const { alpha = 1, mode } = culori.parse(color)
const parsed = culori.parse(color)
if (alpha !== 1) {
if ('alpha' in parsed) {
// Has an alpha value, return color as-is
return {
[property]: color,
}
}
let value
if (mode === 'hsl') {
const { h, s, l } = culori.hsl(color)
value = `hsla(${h}, ${s}, ${l}, var(${variable}))`
} else {
const { r, g, b } = culori.rgb(color)
value = `rgba(${r}, ${g}, ${b}, var(${variable}))`
}
const formatFn = parsed.mode === 'hsl' ? 'formatHsl' : 'formatRgb'
const value = culori[formatFn]({
...parsed,
alpha: 0.3,
}).replace('0.3)', `var(${variable}))`)
return {
[variable]: '1',

View File

@ -7,6 +7,16 @@ test('it adds the right custom property', () => {
'--tw-text-opacity': '1',
color: 'rgba(255, 0, 0, var(--tw-text-opacity))',
})
expect(
withAlphaVariable({
color: 'hsl(240 100% 50%)',
property: 'color',
variable: '--tw-text-opacity',
})
).toEqual({
'--tw-text-opacity': '1',
color: 'hsla(240, 100%, 50%, var(--tw-text-opacity))',
})
})
test('it ignores colors that cannot be parsed', () => {