Skip to content

Commit 6df465a

Browse files
committed
Fix formatting
1 parent a582beb commit 6df465a

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/util/withAlphaVariable.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,20 @@ export default function withAlphaVariable({ color, property, variable }) {
3737
}
3838

3939
if (isValidColor(color)) {
40-
const { alpha = 1, mode } = culori.parse(color)
40+
const parsed = culori.parse(color)
4141

42-
if (alpha !== 1) {
42+
if ('alpha' in parsed) {
4343
// Has an alpha value, return color as-is
4444
return {
4545
[property]: color,
4646
}
4747
}
4848

49-
let value
50-
if (mode === 'hsl') {
51-
const { h, s, l } = culori.hsl(color)
52-
value = `hsla(${h}, ${s}, ${l}, var(${variable}))`
53-
} else {
54-
const { r, g, b } = culori.rgb(color)
55-
value = `rgba(${r}, ${g}, ${b}, var(${variable}))`
56-
}
49+
const formatFn = parsed.mode === 'hsl' ? 'formatHsl' : 'formatRgb'
50+
const value = culori[formatFn]({
51+
...parsed,
52+
alpha: 0.3,
53+
}).replace('0.3)', `var(${variable}))`)
5754

5855
return {
5956
[variable]: '1',

tests/withAlphaVariable.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ test('it adds the right custom property', () => {
77
'--tw-text-opacity': '1',
88
color: 'rgba(255, 0, 0, var(--tw-text-opacity))',
99
})
10+
expect(
11+
withAlphaVariable({
12+
color: 'hsl(240 100% 50%)',
13+
property: 'color',
14+
variable: '--tw-text-opacity',
15+
})
16+
).toEqual({
17+
'--tw-text-opacity': '1',
18+
color: 'hsla(240, 100%, 50%, var(--tw-text-opacity))',
19+
})
1020
})
1121

1222
test('it ignores colors that cannot be parsed', () => {

0 commit comments

Comments
 (0)