Skip to content

Color opacity breaks when using functions to define colors #2512

@JeffSchofield

Description

@JeffSchofield

Describe the problem:

If I define a color using a function like this:

{
	...
	colors: {
		'primary': ({ opacityVariable }) => `rgba(255,0,0,var(${ opacityVariable }))`
	}
	...
}

The CSS variable passed in by opacityVariable is not set by default, breaking the color.

Looking at

export default function withAlphaVariable({ color, property, variable }) {
, it seems like this might be a bug. If I edit this file to include [variable]: '1' in the returned object, the default variable is set and everything works as anticipated.

After some discussion on discord, Robin suggested adding a default to the css variable to resolve this such as: var(${opacitiyVariable}, 1). However crswll pointed out this results in specificity issues (see repro below).

My use case is that I need to pass in my own CSS variable as the color so I cannot use the toRgba function. This is for a vite plugin so I would like to retain identical behavior to default.

Link to a minimal reproduction:

I expect the text to be red (#FF0000) but it is black: https://play.tailwindcss.com/8uIqAsBJHR?layout=vertical&file=html
Inspecting the CSS shows the --text-opacity variable is not defined.

Specificity problem from @crswll on Discord: https://play.tailwindcss.com/b2b9JVJeNf?layout=vertical&file=html

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions