Fix handling of CSS variable declarations

This commit is contained in:
Hunter Tunnicliff 2021-08-04 21:25:57 -07:00 • committed by Robin Malfait
parent 721e57312b
commit be4aa931b6
No known key found for this signature in database
GPG key ID: 92F53D68B9041AFE

View file

@ -17,11 +17,22 @@ export function withAlphaValue(color, alphaValue, defaultValue) {
// Apply alpha value
parsed.alpha = alphaValue
// Return formatted string
// Format string
let value
if (parsed.mode === 'hsl') {
return culori.formatHsl(parsed)
value = culori.formatHsl(parsed)
} else {
return culori.formatRgb(parsed)
value = culori.formatRgb(parsed)
}
// Correctly apply CSS variable alpha value
if (typeof alphaValue === 'string' && alphaValue.startsWith('var(') && value.endsWith('NaN)')) {
value = value.replace('NaN)', `${alphaValue})`)
}
// Color could not be formatted correctly
if (!value.includes('NaN')) {
return value
}
}
@ -49,8 +60,8 @@ export default function withAlphaVariable({ color, property, variable }) {
const formatFn = parsed.mode === 'hsl' ? 'formatHsl' : 'formatRgb'
const value = culori[formatFn]({
...parsed,
alpha: 0.3,
}).replace('0.3)', `var(${variable}))`)
alpha: NaN, // intentionally set to `NaN` for replacing
}).replace('NaN)', `var(${variable}))`)
return {
[variable]: '1',