-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lookup variables in the CSS theme (#1082)
- Loading branch information
1 parent
712b5ff
commit 3014df5
Showing
5 changed files
with
94 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 35 additions & 5 deletions
40
packages/tailwindcss-language-service/src/util/css-vars.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,56 @@ | ||
import { expect, test } from 'vitest' | ||
import { replaceCssVarsWithFallbacks } from './css-vars' | ||
import { State } from './state' | ||
import { DesignSystem } from './v4' | ||
|
||
test('replacing CSS variables with their fallbacks (when they have them)', () => { | ||
expect(replaceCssVarsWithFallbacks('var(--foo, red)')).toBe(' red') | ||
expect(replaceCssVarsWithFallbacks('var(--foo, )')).toBe(' ') | ||
let map = new Map<string, string>([ | ||
['--known', 'blue'], | ||
]) | ||
|
||
expect(replaceCssVarsWithFallbacks('rgb(var(--foo, 255 0 0))')).toBe('rgb( 255 0 0)') | ||
expect(replaceCssVarsWithFallbacks('rgb(var(--foo, var(--bar)))')).toBe('rgb( var(--bar))') | ||
let state: State = { | ||
enabled: true, | ||
designSystem: { | ||
resolveThemeValue: (name) => map.get(name) ?? null, | ||
} as DesignSystem, | ||
} | ||
|
||
expect(replaceCssVarsWithFallbacks(state, 'var(--foo, red)')).toBe(' red') | ||
expect(replaceCssVarsWithFallbacks(state, 'var(--foo, )')).toBe(' ') | ||
|
||
expect(replaceCssVarsWithFallbacks(state, 'rgb(var(--foo, 255 0 0))')).toBe('rgb( 255 0 0)') | ||
expect(replaceCssVarsWithFallbacks(state, 'rgb(var(--foo, var(--bar)))')).toBe('rgb( var(--bar))') | ||
|
||
expect( | ||
replaceCssVarsWithFallbacks('rgb(var(var(--bar, var(--baz), var(--qux), var(--thing))))'), | ||
replaceCssVarsWithFallbacks( | ||
state, | ||
'rgb(var(var(--bar, var(--baz), var(--qux), var(--thing))))', | ||
), | ||
).toBe('rgb(var(var(--bar, var(--baz), var(--qux), var(--thing))))') | ||
|
||
expect( | ||
replaceCssVarsWithFallbacks( | ||
state, | ||
'rgb(var(--one, var(--bar, var(--baz), var(--qux), var(--thing))))', | ||
), | ||
).toBe('rgb( var(--baz), var(--qux), var(--thing))') | ||
|
||
expect( | ||
replaceCssVarsWithFallbacks( | ||
state, | ||
'color-mix(in srgb, var(--color-primary, oklch(0 0 0 / 2.5)), var(--color-secondary, oklch(0 0 0 / 2.5)), 50%)', | ||
), | ||
).toBe('color-mix(in srgb, oklch(0 0 0 / 2.5), oklch(0 0 0 / 2.5), 50%)') | ||
|
||
// Known theme keys are replaced with their values | ||
expect(replaceCssVarsWithFallbacks(state, 'var(--known)')).toBe('blue') | ||
|
||
// Values from the theme take precedence over fallbacks | ||
expect(replaceCssVarsWithFallbacks(state, 'var(--known, red)')).toBe('blue') | ||
|
||
// Unknown theme keys use a fallback if provided | ||
expect(replaceCssVarsWithFallbacks(state, 'var(--unknown, red)')).toBe(' red') | ||
|
||
// Unknown theme keys without fallbacks are not replaced | ||
expect(replaceCssVarsWithFallbacks(state, 'var(--unknown)')).toBe('var(--unknown)') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters