Skip to content

Commit deec3cc

Browse files
Fix equivalent calculation when using prefixes in v4 (#1166)
Fixes #1160 We were passing the CSS variable straight through when resolving the theme values but this isn't accurate when a prefix is used. The theme variables *do not* have prefixes but the emitted CSS variables _do_. We have to strip the prefix before doing the lookup.
1 parent adc3769 commit deec3cc

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

packages/tailwindcss-language-service/src/util/rewriting/add-theme-values.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { replaceCssVars, replaceCssCalc, Range } from './replacements'
55
import { addPixelEquivalentsToValue } from '../pixelEquivalents'
66
import { applyComments, Comment } from '../comments'
77
import { getEquivalentColor } from '../colorEquivalents'
8+
import { resolveVariableValue } from './lookup'
89

910
export function addThemeValues(css: string, state: State, settings: TailwindCssSettings) {
1011
if (!state.designSystem) return css
@@ -16,7 +17,7 @@ export function addThemeValues(css: string, state: State, settings: TailwindCssS
1617
let inlined = replaceCssVars(expr.value, ({ name }) => {
1718
if (!name.startsWith('--')) return null
1819

19-
let value = state.designSystem.resolveThemeValue?.(name) ?? null
20+
let value = resolveVariableValue(state.designSystem, name)
2021
if (value === null) return null
2122

2223
// Inline CSS calc expressions in theme values
@@ -70,7 +71,7 @@ export function addThemeValues(css: string, state: State, settings: TailwindCssS
7071
}
7172
}
7273

73-
let value = state.designSystem.resolveThemeValue?.(name) ?? null
74+
let value = resolveVariableValue(state.designSystem, name)
7475
if (value === null) return null
7576

7677
let px = addPixelEquivalentsToValue(value, settings.rootFontSize, false)

packages/tailwindcss-language-service/src/util/rewriting/inline-theme-values.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { State, TailwindCssSettings } from '../state'
22

33
import { evaluateExpression } from './calc'
4+
import { resolveVariableValue } from './lookup'
45
import { replaceCssVars, replaceCssCalc } from './replacements'
56

67
export function inlineThemeValues(css: string, state: State) {
@@ -10,7 +11,7 @@ export function inlineThemeValues(css: string, state: State) {
1011
let inlined = replaceCssVars(expr.value, ({ name, fallback }) => {
1112
if (!name.startsWith('--')) return null
1213

13-
let value = state.designSystem.resolveThemeValue?.(name) ?? null
14+
let value = resolveVariableValue(state.designSystem, name)
1415
if (value === null) return fallback
1516

1617
return value
@@ -22,7 +23,7 @@ export function inlineThemeValues(css: string, state: State) {
2223
css = replaceCssVars(css, ({ name, fallback }) => {
2324
if (!name.startsWith('--')) return null
2425

25-
let value = state.designSystem.resolveThemeValue?.(name) ?? null
26+
let value = resolveVariableValue(state.designSystem, name)
2627
if (value === null) return fallback
2728

2829
return value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { DesignSystem } from '../v4'
2+
3+
// Resolve a variable value from the design system
4+
export function resolveVariableValue(design: DesignSystem, name: string) {
5+
let prefix = design.theme.prefix ?? null
6+
7+
if (prefix && name.startsWith(`--${prefix}`)) {
8+
name = `--${name.slice(prefix.length + 3)}`
9+
}
10+
11+
return design.resolveThemeValue?.(name) ?? null
12+
}

packages/tailwindcss-language-service/src/util/rewriting/var-fallbacks.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { State } from '../state'
2+
import { resolveVariableValue } from './lookup'
23
import { replaceCssVars } from './replacements'
34

45
export function replaceCssVarsWithFallbacks(state: State, str: string): string {
@@ -7,7 +8,7 @@ export function replaceCssVarsWithFallbacks(state: State, str: string): string {
78
// take precedences over other sources as that emulates the behavior of a
89
// browser where the fallback is only used if the variable is defined.
910
if (state.designSystem && name.startsWith('--')) {
10-
let value = state.designSystem.resolveThemeValue?.(name) ?? null
11+
let value = resolveVariableValue(state.designSystem, name)
1112
if (value !== null) return value
1213
}
1314

packages/vscode-tailwindcss/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Support style-rule like completions inside `@custom-variant` ([#1165](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1165))
1111
- Support style-rule like completions inside `@variant` ([#1165](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1165))
1212
- Make sure `@slot` isn't considered an unknown at-rule ([#1165](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1165))
13+
- Fix equivalent calculation when using prefixes in v4 ([#1166](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1166))
1314

1415
## 0.14.2
1516

0 commit comments

Comments
 (0)