Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cssvar colors to withAlphaVariable and withAlphaValue #4659

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/util/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ let ALPHA_SEP = `\\s*[,/]\\s*`
let RGB_HSL = new RegExp(
`^(rgb|hsl)a?\\(\\s*(${VALUE})${SEP}(${VALUE})${SEP}(${VALUE})(?:${ALPHA_SEP}(${VALUE}))?\\s*\\)$`
)
let COLOR_VAR = /(rgb|hsl)\(\s*(var\(.*?\))\s*\)/

export function parseColor(value) {
if (typeof value !== 'string') {
Expand Down Expand Up @@ -37,6 +38,15 @@ export function parseColor(value) {
}
}

let colorVar = value.match(COLOR_VAR)

if (colorVar !== null) {
return {
mode: colorVar[1],
color: colorVar[2],
}
}

let match = value.match(RGB_HSL)

if (match !== null) {
Expand All @@ -52,5 +62,10 @@ export function parseColor(value) {

export function formatColor({ mode, color, alpha }) {
let hasAlpha = alpha !== undefined

if (typeof color === 'string') {
return `${mode}(${color}${hasAlpha ? ` / ${alpha}` : ''})`
}

return `${mode}(${color.join(' ')}${hasAlpha ? ` / ${alpha}` : ''})`
}
36 changes: 34 additions & 2 deletions tests/withAlphaVariable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,21 @@ test('it ignores colors that cannot be parsed', () => {
})
expect(
withAlphaVariable({
color: 'rgb(var(--color))',
color: 'rgba(var(--color))',
property: 'background-color',
variable: '--tw-bg-opacity',
})
).toEqual({
'background-color': 'rgb(var(--color))',
'background-color': 'rgba(var(--color))',
})
expect(
withAlphaVariable({
color: 'hsla(var(--color))',
property: 'background-color',
variable: '--tw-bg-opacity',
})
).toEqual({
'background-color': 'hsla(var(--color))',
})
})

Expand Down Expand Up @@ -183,6 +192,29 @@ test('it allows a closure to be passed', () => {
})
})

test('it allows css variables to be passed', () => {
expect(
withAlphaVariable({
color: 'rgb(var(--test-var))',
property: 'background-color',
variable: '--tw-bg-opacity',
})
).toEqual({
'--tw-bg-opacity': '1',
'background-color': 'rgb(var(--test-var) / var(--tw-bg-opacity))',
})
expect(
withAlphaVariable({
color: 'hsl(var(--test-var))',
property: 'background-color',
variable: '--tw-bg-opacity',
})
).toEqual({
'--tw-bg-opacity': '1',
'background-color': 'hsl(var(--test-var) / var(--tw-bg-opacity))',
})
})

test('it transforms rgb and hsl to space-separated rgb and hsl', () => {
expect(
withAlphaVariable({
Expand Down