File size: 1,673 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import { isSSR } from './helpers'
import { cssVariableRegex } from './regexs'
/**
* takes a CSS variable and attempts
* to turn it into a RGBA value
*
* ```
* 'var(--white)' => 'rgba(255,255,255,1)'
* ```
*
* @param input string
* @returns string
*/
export const variableToRgba = (input: string): string => {
const [token, fallback] = parseCSSVariable(input)
if (!token || isSSR()) {
return input
}
const value = window
.getComputedStyle(document.documentElement)
.getPropertyValue(token)
if (value) {
/**
* We have a valid variable returned
* trim and return
*/
return value.trim()
} else if (fallback && fallback.startsWith('--')) {
/**
* fallback is something like --my-variable
* so we try get property value
*/
const value = window
.getComputedStyle(document.documentElement)
.getPropertyValue(fallback)
/**
* if it exists, return else nothing was found so just return the input
*/
if (value) {
return value
} else {
return input
}
} else if (fallback && cssVariableRegex.test(fallback)) {
/**
* We have a fallback and it's another CSS variable
*/
return variableToRgba(fallback)
} else if (fallback) {
/**
* We have a fallback and it's not a CSS variable
*/
return fallback
}
/**
* Nothing worked so just return the input
* like our other FluidValue replace functions do
*/
return input
}
const parseCSSVariable = (current: string) => {
const match = cssVariableRegex.exec(current)
if (!match) return [,]
const [, token, fallback] = match
return [token, fallback]
}
|