Skip to content

Commit c6e4dab

Browse files
Remove named opacity support for color opacity modifiers (#14278)
Right now if you have a custom opacity theme value configured like this… ```css @theme { --opacity-disabled: 50%; } ``` …then you can use that named opacity value as a color opacity modifier like this: ```html <div class="bg-red-500/disabled"> ``` We think this behavior is confusing. The color opacity modifier is not setting opacity but the alpha value of a color. Opacity is for setting the opacity of an element, so the custom names you'd come up with are named after those situations, which makes them not seem appropriate for color modifiers.
1 parent d9e3fd6 commit c6e4dab

File tree

4 files changed

+24
-44
lines changed

4 files changed

+24
-44
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Fix support for declaration fallbacks in plugins ([#14265](https://github.com/tailwindlabs/tailwindcss/pull/14265))
2121
- Support bare values when using `tailwindcss/defaultTheme` ([#14257](https://github.com/tailwindlabs/tailwindcss/pull/14257))
2222

23+
## Changed
24+
25+
- Remove named opacity support for color opacity modifiers ([#14278](https://github.com/tailwindlabs/tailwindcss/pull/14278))
26+
2327
## [4.0.0-alpha.20] - 2024-08-23
2428

2529
### Added

packages/tailwindcss/src/compile.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ function compileBaseUtility(candidate: Candidate, designSystem: DesignSystem) {
235235
// Assumption: If an arbitrary property has a modifier, then we assume it
236236
// is an opacity modifier.
237237
if (candidate.modifier) {
238-
value = asColor(value, candidate.modifier, designSystem.theme)
238+
value = asColor(value, candidate.modifier)
239239
}
240240

241241
if (value === null) return []

packages/tailwindcss/src/utilities.test.ts

+3-16
Original file line numberDiff line numberDiff line change
@@ -9610,28 +9610,15 @@ test('bg', async () => {
96109610
expect(
96119611
await compileCss(
96129612
css`
9613-
@theme {
9613+
@theme reference {
96149614
--opacity-half: 0.5;
96159615
--opacity-custom: var(--custom-opacity);
96169616
}
96179617
@tailwind utilities;
96189618
`,
9619-
['bg-current/half', 'bg-current/custom'],
9619+
['bg-current/half', 'bg-current/custom', '[color:red]/half'],
96209620
),
9621-
).toMatchInlineSnapshot(`
9622-
":root {
9623-
--opacity-half: .5;
9624-
--opacity-custom: var(--custom-opacity);
9625-
}
9626-
9627-
.bg-current\\/custom {
9628-
background-color: color-mix(in srgb, currentColor calc(var(--opacity-custom, var(--custom-opacity)) * 100%), transparent);
9629-
}
9630-
9631-
.bg-current\\/half {
9632-
background-color: color-mix(in srgb, currentColor calc(var(--opacity-half, .5) * 100%), transparent);
9633-
}"
9634-
`)
9621+
).toEqual('')
96359622
})
96369623

96379624
test('from', async () => {

packages/tailwindcss/src/utilities.ts

+16-27
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,13 @@ export function withAlpha(value: string, alpha: string): string {
129129
/**
130130
* Resolve a color value + optional opacity modifier to a final color.
131131
*/
132-
export function asColor(
133-
value: string,
134-
modifier: CandidateModifier | null,
135-
theme: Theme,
136-
): string | null {
132+
export function asColor(value: string, modifier: CandidateModifier | null): string | null {
137133
if (!modifier) return value
138134

139135
if (modifier.kind === 'arbitrary') {
140136
return withAlpha(value, modifier.value)
141137
}
142138

143-
// Check if the modifier exists in the `opacity` theme configuration and use
144-
// that value if so.
145-
let alpha = theme.resolve(modifier.value, ['--opacity'])
146-
if (alpha) {
147-
return withAlpha(value, alpha)
148-
}
149-
150139
if (Number.isNaN(Number(modifier.value))) {
151140
return null
152141
}
@@ -208,7 +197,7 @@ function resolveThemeColor<T extends ColorThemeKey>(
208197
}
209198
}
210199

211-
return value ? asColor(value, candidate.modifier, theme) : null
200+
return value ? asColor(value, candidate.modifier) : null
212201
}
213202

214203
export function createUtilities(theme: Theme) {
@@ -360,7 +349,7 @@ export function createUtilities(theme: Theme) {
360349
value = candidate.value.value
361350

362351
// Apply an opacity modifier to the value if appropriate.
363-
value = asColor(value, candidate.modifier, theme)
352+
value = asColor(value, candidate.modifier)
364353
} else {
365354
value = resolveThemeColor(candidate, theme, desc.themeKeys)
366355
}
@@ -2241,7 +2230,7 @@ export function createUtilities(theme: Theme) {
22412230
return [borderProperties(), ...decls]
22422231
}
22432232
default: {
2244-
value = asColor(value, candidate.modifier, theme)
2233+
value = asColor(value, candidate.modifier)
22452234
if (value === null) return
22462235

22472236
return desc.color(value)
@@ -2549,7 +2538,7 @@ export function createUtilities(theme: Theme) {
25492538
return [decl('background-image', value)]
25502539
}
25512540
default: {
2552-
value = asColor(value, candidate.modifier, theme)
2541+
value = asColor(value, candidate.modifier)
25532542
if (value === null) return
25542543

25552544
return [decl('background-color', value)]
@@ -2625,7 +2614,7 @@ export function createUtilities(theme: Theme) {
26252614
return desc.position(value)
26262615
}
26272616
default: {
2628-
value = asColor(value, candidate.modifier, theme)
2617+
value = asColor(value, candidate.modifier)
26292618
if (value === null) return
26302619

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

27652754
if (candidate.value.kind === 'arbitrary') {
2766-
let value = asColor(candidate.value.value, candidate.modifier, theme)
2755+
let value = asColor(candidate.value.value, candidate.modifier)
27672756
if (value === null) return
27682757
return [decl('fill', value)]
27692758
}
@@ -2801,7 +2790,7 @@ export function createUtilities(theme: Theme) {
28012790
return [decl('stroke-width', value)]
28022791
}
28032792
default: {
2804-
value = asColor(candidate.value.value, candidate.modifier, theme)
2793+
value = asColor(candidate.value.value, candidate.modifier)
28052794
if (value === null) return
28062795

28072796
return [decl('stroke', value)]
@@ -3099,7 +3088,7 @@ export function createUtilities(theme: Theme) {
30993088
return [decl('text-decoration-thickness', value)]
31003089
}
31013090
default: {
3102-
value = asColor(value, candidate.modifier, theme)
3091+
value = asColor(value, candidate.modifier)
31033092
if (value === null) return
31043093

31053094
return [decl('text-decoration-color', value)]
@@ -3936,7 +3925,7 @@ export function createUtilities(theme: Theme) {
39363925
]
39373926
}
39383927
default: {
3939-
value = asColor(value, candidate.modifier, theme)
3928+
value = asColor(value, candidate.modifier)
39403929
if (value === null) return
39413930

39423931
return [decl('outline-color', value)]
@@ -4068,7 +4057,7 @@ export function createUtilities(theme: Theme) {
40684057
return [decl('font-size', value)]
40694058
}
40704059
default: {
4071-
value = asColor(value, candidate.modifier, theme)
4060+
value = asColor(value, candidate.modifier)
40724061
if (value === null) return
40734062

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

41854174
switch (type) {
41864175
case 'color': {
4187-
value = asColor(value, candidate.modifier, theme)
4176+
value = asColor(value, candidate.modifier)
41884177
if (value === null) return
41894178

41904179
return [
@@ -4280,7 +4269,7 @@ export function createUtilities(theme: Theme) {
42804269

42814270
switch (type) {
42824271
case 'color': {
4283-
value = asColor(value, candidate.modifier, theme)
4272+
value = asColor(value, candidate.modifier)
42844273
if (value === null) return
42854274

42864275
return [
@@ -4392,7 +4381,7 @@ export function createUtilities(theme: Theme) {
43924381
]
43934382
}
43944383
default: {
4395-
value = asColor(value, candidate.modifier, theme)
4384+
value = asColor(value, candidate.modifier)
43964385
if (value === null) return
43974386

43984387
return [decl('--tw-ring-color', value)]
@@ -4468,7 +4457,7 @@ export function createUtilities(theme: Theme) {
44684457
]
44694458
}
44704459
default: {
4471-
value = asColor(value, candidate.modifier, theme)
4460+
value = asColor(value, candidate.modifier)
44724461
if (value === null) return
44734462

44744463
return [decl('--tw-inset-ring-color', value)]
@@ -4533,7 +4522,7 @@ export function createUtilities(theme: Theme) {
45334522
]
45344523
}
45354524
default: {
4536-
value = asColor(value, candidate.modifier, theme)
4525+
value = asColor(value, candidate.modifier)
45374526
if (value === null) return
45384527

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

0 commit comments

Comments
 (0)