Skip to content

Commit e2becb8

Browse files
committed
Add fallback values to theme variables
This lets users see the current theme value _and_ pixel/color equivalents.
1 parent 45421d8 commit e2becb8

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

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

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

32+
if (node.value === 'var') {
33+
return true
34+
}
35+
3236
if (!allowedFunctions.includes(node.value)) {
3337
return false
3438
}

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

Lines changed: 2 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 { addThemeValues } from './rewriting'
56

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

4546
let css = clone.toString()
4647

48+
css = addThemeValues(css, state, settings.tailwindCSS)
4749
css = addEquivalents(css, settings.tailwindCSS)
4850

4951
let identSize = state.v4 ? 2 : 4
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import type { State } from '../state'
2+
3+
import { replaceCssVars } from './replacements'
4+
5+
export function addThemeValues(css: string, state: State) {
6+
if (!state.designSystem) return css
7+
8+
// Add fallbacks to variables with their theme values
9+
// Ideally these would just be commentss like
10+
// `var(--foo) /* 3rem = 48px */` or
11+
// `calc(var(--spacing) * 5) /* 1.25rem = 20px */`
12+
css = replaceCssVars(css, ({ name }) => {
13+
if (!name.startsWith('--')) return null
14+
15+
let value = state.designSystem.resolveThemeValue?.(name) ?? null
16+
if (value === null) return null
17+
18+
return `var(${name}, ${value})`
19+
})
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './replacements'
22
export * from './var-fallbacks'
3+
export * from './add-theme-values'

0 commit comments

Comments
 (0)