Skip to content

Commit 8df030a

Browse files
committed
Add fallback values to theme variables
This lets users see the current theme value _and_ pixel/color equivalents
1 parent cf5c707 commit 8df030a

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

packages/tailwindcss-language-server/tests/completions/completions.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,11 +587,11 @@ withFixture('v4/basic', (c) => {
587587
expect(resolved).toEqual({
588588
...item,
589589
detail:
590-
'font-size: var(--font-size-sm, 0.875rem /* 8.75px */); line-height: var(--tw-leading, var(--font-size-sm--line-height, 1.25rem /* 12.5px */));',
590+
'font-size: var(--text-sm); line-height: var(--tw-leading, var(--text-sm--line-height));',
591591
documentation: {
592592
kind: 'markdown',
593593
value:
594-
'```css\n.text-sm {\n font-size: var(--font-size-sm, 0.875rem /* 8.75px */);\n line-height: var(--tw-leading, var(--font-size-sm--line-height, 1.25rem /* 12.5px */));\n}\n```',
594+
'```css\n.text-sm {\n font-size: var(--text-sm, 0.875rem /* 8.75px */);\n line-height: var(--tw-leading, var(--text-sm--line-height, 1.25rem /* 12.5px */));\n}\n```',
595595
},
596596
})
597597
})
@@ -613,7 +613,7 @@ withFixture('v4/basic', (c) => {
613613

614614
expect(resolved).toEqual({
615615
...item,
616-
detail: 'background-color: var(--color-red-500, oklch(0.637 0.237 25.331));',
616+
detail: 'background-color: var(--color-red-500);',
617617
documentation: '#fb2c36',
618618
})
619619
})
@@ -642,19 +642,19 @@ withFixture('v4/workspaces', (c) => {
642642

643643
expect(resolved[0]).toEqual({
644644
...items[0],
645-
detail: 'background-color: var(--color-beet, #8e3b46);',
645+
detail: 'background-color: var(--color-beet);',
646646
documentation: '#8e3b46',
647647
})
648648

649649
expect(resolved[1]).toEqual({
650650
...items[1],
651-
detail: 'background-color: var(--color-orangepeel, #ff9f00);',
651+
detail: 'background-color: var(--color-orangepeel);',
652652
documentation: '#ff9f00',
653653
})
654654

655655
expect(resolved[2]).toEqual({
656656
...items[2],
657-
detail: 'background-color: var(--color-style-main, #8e3b46);',
657+
detail: 'background-color: var(--color-style-main);',
658658
documentation: '#8e3b46',
659659
})
660660
})

packages/tailwindcss-language-server/tests/hover/hover.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ withFixture('v4/basic', (c) => {
194194
text: '<div class="bg-red-500">',
195195
position: { line: 0, character: 13 },
196196
expected:
197-
'.bg-red-500 {\n background-color: var(--color-red-500, oklch(0.637 0.237 25.331));\n}',
197+
'.bg-red-500 {\n background-color: var(--color-red-500, oklch(0.637 0.237 25.331) /* #fb2c36 */);\n}',
198198
expectedRange: {
199199
start: { line: 0, character: 12 },
200200
end: { line: 0, character: 22 },

packages/tailwindcss-language-service/src/util/colorEquivalents.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export function equivalentColorValues({ comments }: { comments: Comment[] }): Pl
1919
return true
2020
}
2121

22+
if (node.value === 'var') {
23+
return true
24+
}
25+
2226
if (!allowedFunctions.includes(node.value)) {
2327
return false
2428
}

packages/tailwindcss-language-service/src/util/css-vars.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import { State } from './state'
44
import { DesignSystem } from './v4'
55

66
test('replacing CSS variables with their fallbacks (when they have them)', () => {
7-
let map = new Map<string, string>([
8-
['--known', 'blue'],
9-
])
7+
let map = new Map<string, string>([['--known', 'blue']])
108

119
let state: State = {
1210
enabled: true,
@@ -26,7 +24,7 @@ test('replacing CSS variables with their fallbacks (when they have them)', () =>
2624
state,
2725
'rgb(var(var(--bar, var(--baz), var(--qux), var(--thing))))',
2826
),
29-
).toBe('rgb(var(var(--bar, var(--baz), var(--qux), var(--thing))))')
27+
).toBe('rgb(var( var(--baz), var(--qux), var(--thing)))')
3028

3129
expect(
3230
replaceCssVarsWithFallbacks(

packages/tailwindcss-language-service/src/util/css-vars.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function replaceCssVarsWithFallbacks(state: State, str: string): string {
2121

2222
type CssVarReplacer = (name: string, fallback: string | null) => string | null
2323

24-
function replaceCssVars(str: string, replace: CssVarReplacer): string {
24+
export function replaceCssVars(str: string, replace: CssVarReplacer): string {
2525
for (let i = 0; i < str.length; ++i) {
2626
if (!str.startsWith('var(', i)) continue
2727

@@ -51,16 +51,11 @@ function replaceCssVars(str: string, replace: CssVarReplacer): string {
5151

5252
if (replacement !== null) {
5353
str = str.slice(0, i) + replacement + str.slice(j + 1)
54-
55-
// We don't want to skip past anything here because `replacement`
56-
// might contain more var(…) calls in which case `i` will already
57-
// be pointing at the right spot to start looking for them
58-
break
5954
}
6055

61-
// It can't be replaced so we can avoid unncessary work by skipping over
62-
// the entire var(…) call.
63-
i = j + 1
56+
// We don't want to skip past anything here because `replacement`
57+
// might contain more var(…) calls in which case `i` will already
58+
// be pointing at the right spot to start looking for them
6459
break
6560
}
6661
}

packages/tailwindcss-language-service/src/util/jit.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { State } from './state'
22
import type { Container, Document, Root, Rule, Node, AtRule } from 'postcss'
33
import { addPixelEquivalentsToValue } from './pixelEquivalents'
44
import { addEquivalents } from './equivalents'
5+
import { replaceCssVars } from './css-vars'
56

67
export function bigSign(bigIntValue) {
78
// @ts-ignore
@@ -44,6 +45,21 @@ export async function stringifyRoot(state: State, root: Root, uri?: string): Pro
4445

4546
let css = clone.toString()
4647

48+
// Add fallbacks to variables with their theme values
49+
// Ideally these would just be commentss like
50+
// `var(--foo) /* 3rem = 48px */` or
51+
// `calc(var(--spacing) * 5) /* 1.25rem = 20px */`
52+
if (state.designSystem) {
53+
css = replaceCssVars(css, (name) => {
54+
if (!name.startsWith('--')) return null
55+
56+
let value = state.designSystem.resolveThemeValue?.(name) ?? null
57+
if (value === null) return null
58+
59+
return `var(${name}, ${value})`
60+
})
61+
}
62+
4763
css = addEquivalents(css, settings.tailwindCSS)
4864

4965
let identSize = state.v4 ? 2 : 4

0 commit comments

Comments
 (0)