Skip to content

Commit b2ebb1b

Browse files
committed
Fix handling of CSS variable declarations
1 parent 6df465a commit b2ebb1b

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/util/withAlphaVariable.js

+16-5
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,22 @@ export function withAlphaValue(color, alphaValue, defaultValue) {
1717
// Apply alpha value
1818
parsed.alpha = alphaValue
1919

20-
// Return formatted string
20+
// Format string
21+
let value
2122
if (parsed.mode === 'hsl') {
22-
return culori.formatHsl(parsed)
23+
value = culori.formatHsl(parsed)
2324
} else {
24-
return culori.formatRgb(parsed)
25+
value = culori.formatRgb(parsed)
26+
}
27+
28+
// Correctly apply CSS variable alpha value
29+
if (typeof alphaValue === 'string' && alphaValue.startsWith('var(') && value.endsWith('NaN)')) {
30+
value = value.replace('NaN)', `${alphaValue})`)
31+
}
32+
33+
// Color could not be formatted correctly
34+
if (!value.includes('NaN')) {
35+
return value
2536
}
2637
}
2738

@@ -49,8 +60,8 @@ export default function withAlphaVariable({ color, property, variable }) {
4960
const formatFn = parsed.mode === 'hsl' ? 'formatHsl' : 'formatRgb'
5061
const value = culori[formatFn]({
5162
...parsed,
52-
alpha: 0.3,
53-
}).replace('0.3)', `var(${variable}))`)
63+
alpha: NaN, // intentionally set to `NaN` for replacing
64+
}).replace('NaN)', `var(${variable}))`)
5465

5566
return {
5667
[variable]: '1',

0 commit comments

Comments
 (0)