Skip to content

Fix equivalent calculation when using prefixes in v4 #1166

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

Merged
merged 4 commits into from
Feb 5, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { replaceCssVars, replaceCssCalc, Range } from './replacements'
import { addPixelEquivalentsToValue } from '../pixelEquivalents'
import { applyComments, Comment } from '../comments'
import { getEquivalentColor } from '../colorEquivalents'
import { resolveVariableValue } from './lookup'

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

let value = state.designSystem.resolveThemeValue?.(name) ?? null
let value = resolveVariableValue(state.designSystem, name)
if (value === null) return null

// Inline CSS calc expressions in theme values
Expand Down Expand Up @@ -70,7 +71,7 @@ export function addThemeValues(css: string, state: State, settings: TailwindCssS
}
}

let value = state.designSystem.resolveThemeValue?.(name) ?? null
let value = resolveVariableValue(state.designSystem, name)
if (value === null) return null

let px = addPixelEquivalentsToValue(value, settings.rootFontSize, false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { State, TailwindCssSettings } from '../state'

import { evaluateExpression } from './calc'
import { resolveVariableValue } from './lookup'
import { replaceCssVars, replaceCssCalc } from './replacements'

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

let value = state.designSystem.resolveThemeValue?.(name) ?? null
let value = resolveVariableValue(state.designSystem, name)
if (value === null) return fallback

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

let value = state.designSystem.resolveThemeValue?.(name) ?? null
let value = resolveVariableValue(state.designSystem, name)
if (value === null) return fallback

return value
Expand Down
12 changes: 12 additions & 0 deletions packages/tailwindcss-language-service/src/util/rewriting/lookup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { DesignSystem } from '../v4'

// Resolve a variable value from the design system
export function resolveVariableValue(design: DesignSystem, name: string) {
let prefix = design.theme.prefix ?? null

if (prefix && name.startsWith(`--${prefix}`)) {
name = `--${name.slice(prefix.length + 3)}`
}

return design.resolveThemeValue?.(name) ?? null
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { State } from '../state'
import { resolveVariableValue } from './lookup'
import { replaceCssVars } from './replacements'

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

Expand Down
1 change: 1 addition & 0 deletions packages/vscode-tailwindcss/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Support style-rule like completions inside `@custom-variant` ([#1165](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1165))
- Support style-rule like completions inside `@variant` ([#1165](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1165))
- Make sure `@slot` isn't considered an unknown at-rule ([#1165](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1165))
- Fix equivalent calculation when using prefixes in v4 ([#1166](https://github.com/tailwindlabs/tailwindcss-intellisense/pull/1166))

## 0.14.2

Expand Down