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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove named opacity support for color opacity modifiers #14278

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix fallback values when using the CSS `theme()` function ([#14262](https://github.com/tailwindlabs/tailwindcss/pull/14262))
- Fix support for declaration fallbacks in plugins ([#14265](https://github.com/tailwindlabs/tailwindcss/pull/14265))

## Changed

- Remove named opacity support for color opacity modifiers ([#14278](https://github.com/tailwindlabs/tailwindcss/pull/14278))

## [4.0.0-alpha.20] - 2024-08-23

### Added
Expand Down
2 changes: 1 addition & 1 deletion packages/tailwindcss/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function compileBaseUtility(candidate: Candidate, designSystem: DesignSystem) {
// Assumption: If an arbitrary property has a modifier, then we assume it
// is an opacity modifier.
if (candidate.modifier) {
value = asColor(value, candidate.modifier, designSystem.theme)
value = asColor(value, candidate.modifier)
}

if (value === null) return []
Expand Down
19 changes: 3 additions & 16 deletions packages/tailwindcss/src/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9610,28 +9610,15 @@ test('bg', async () => {
expect(
await compileCss(
css`
@theme {
@theme reference {
--opacity-half: 0.5;
--opacity-custom: var(--custom-opacity);
}
@tailwind utilities;
`,
['bg-current/half', 'bg-current/custom'],
['bg-current/half', 'bg-current/custom', '[color:red]/half'],
),
).toMatchInlineSnapshot(`
":root {
--opacity-half: .5;
--opacity-custom: var(--custom-opacity);
}

.bg-current\\/custom {
background-color: color-mix(in srgb, currentColor calc(var(--opacity-custom, var(--custom-opacity)) * 100%), transparent);
}

.bg-current\\/half {
background-color: color-mix(in srgb, currentColor calc(var(--opacity-half, .5) * 100%), transparent);
}"
`)
).toEqual('')
})

test('from', async () => {
Expand Down
43 changes: 16 additions & 27 deletions packages/tailwindcss/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,24 +129,13 @@ export function withAlpha(value: string, alpha: string): string {
/**
* Resolve a color value + optional opacity modifier to a final color.
*/
export function asColor(
value: string,
modifier: CandidateModifier | null,
theme: Theme,
): string | null {
export function asColor(value: string, modifier: CandidateModifier | null): string | null {
if (!modifier) return value

if (modifier.kind === 'arbitrary') {
return withAlpha(value, modifier.value)
}

// Check if the modifier exists in the `opacity` theme configuration and use
// that value if so.
let alpha = theme.resolve(modifier.value, ['--opacity'])
if (alpha) {
return withAlpha(value, alpha)
}

if (Number.isNaN(Number(modifier.value))) {
return null
}
Expand Down Expand Up @@ -208,7 +197,7 @@ function resolveThemeColor<T extends ColorThemeKey>(
}
}

return value ? asColor(value, candidate.modifier, theme) : null
return value ? asColor(value, candidate.modifier) : null
}

export function createUtilities(theme: Theme) {
Expand Down Expand Up @@ -360,7 +349,7 @@ export function createUtilities(theme: Theme) {
value = candidate.value.value

// Apply an opacity modifier to the value if appropriate.
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
} else {
value = resolveThemeColor(candidate, theme, desc.themeKeys)
}
Expand Down Expand Up @@ -2241,7 +2230,7 @@ export function createUtilities(theme: Theme) {
return [borderProperties(), ...decls]
}
default: {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return desc.color(value)
Expand Down Expand Up @@ -2549,7 +2538,7 @@ export function createUtilities(theme: Theme) {
return [decl('background-image', value)]
}
default: {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return [decl('background-color', value)]
Expand Down Expand Up @@ -2625,7 +2614,7 @@ export function createUtilities(theme: Theme) {
return desc.position(value)
}
default: {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return desc.color(value)
Expand Down Expand Up @@ -2763,7 +2752,7 @@ export function createUtilities(theme: Theme) {
if (candidate.negative || !candidate.value) return

if (candidate.value.kind === 'arbitrary') {
let value = asColor(candidate.value.value, candidate.modifier, theme)
let value = asColor(candidate.value.value, candidate.modifier)
if (value === null) return
return [decl('fill', value)]
}
Expand Down Expand Up @@ -2801,7 +2790,7 @@ export function createUtilities(theme: Theme) {
return [decl('stroke-width', value)]
}
default: {
value = asColor(candidate.value.value, candidate.modifier, theme)
value = asColor(candidate.value.value, candidate.modifier)
if (value === null) return

return [decl('stroke', value)]
Expand Down Expand Up @@ -3099,7 +3088,7 @@ export function createUtilities(theme: Theme) {
return [decl('text-decoration-thickness', value)]
}
default: {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return [decl('text-decoration-color', value)]
Expand Down Expand Up @@ -3936,7 +3925,7 @@ export function createUtilities(theme: Theme) {
]
}
default: {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return [decl('outline-color', value)]
Expand Down Expand Up @@ -4068,7 +4057,7 @@ export function createUtilities(theme: Theme) {
return [decl('font-size', value)]
}
default: {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return [decl('color', value)]
Expand Down Expand Up @@ -4184,7 +4173,7 @@ export function createUtilities(theme: Theme) {

switch (type) {
case 'color': {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return [
Expand Down Expand Up @@ -4280,7 +4269,7 @@ export function createUtilities(theme: Theme) {

switch (type) {
case 'color': {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return [
Expand Down Expand Up @@ -4392,7 +4381,7 @@ export function createUtilities(theme: Theme) {
]
}
default: {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return [decl('--tw-ring-color', value)]
Expand Down Expand Up @@ -4468,7 +4457,7 @@ export function createUtilities(theme: Theme) {
]
}
default: {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return [decl('--tw-inset-ring-color', value)]
Expand Down Expand Up @@ -4533,7 +4522,7 @@ export function createUtilities(theme: Theme) {
]
}
default: {
value = asColor(value, candidate.modifier, theme)
value = asColor(value, candidate.modifier)
if (value === null) return

return [decl('--tw-ring-offset-color', value)]
Expand Down