Skip to content

Commit 8a465d1

Browse files
committed
fix(preset-wind4): cancel support for chained variables
close #4994
1 parent 4f023d4 commit 8a465d1

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

packages-presets/preset-wind4/src/utils/utilities.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,13 @@ export function colorCSSGenerator(
252252

253253
if (color) {
254254
const result: [CSSObject, ...CSSValueInput[]] = [css]
255-
255+
const isCSSVar = color.includes('var(')
256256
if (Object.values(SpecialColorKey).includes(color)) {
257257
css[property] = color
258258
}
259259
else {
260260
const alphaKey = `--un-${varName}-opacity`
261-
const value = keys ? generateThemeVariable('colors', keys) : color
261+
const value = (keys && !isCSSVar) ? generateThemeVariable('colors', keys) : color
262262
let method = modifier ?? (keys ? 'in srgb' : 'in oklab')
263263
if (!method.startsWith('in ') && !method.startsWith('var(')) {
264264
method = `in ${method}`
@@ -267,7 +267,7 @@ export function colorCSSGenerator(
267267
css[property] = `color-mix(${method}, ${value} ${alpha ?? `var(${alphaKey})`}, transparent)${rawColorComment}`
268268
result.push(defineProperty(alphaKey, { syntax: '<percentage>', initialValue: '100%' }))
269269

270-
if (keys) {
270+
if (keys && !isCSSVar) {
271271
themeTracking(`colors`, keys)
272272
if (!modifier) {
273273
const colorValue = ['shadow', 'inset-shadow', 'text-shadow', 'drop-shadow'].includes(varName)

test/preset-wind4.test.ts

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -154,33 +154,31 @@ describe('preset-wind4', () => {
154154
],
155155
theme: {
156156
colors: {
157-
foo: 'var(--colors-bar)',
158-
bar: 'var(--colors-baz-bcd, #000)',
159-
baz: {
160-
bcd: 'var(--colors-test, #fff)',
161-
},
162-
test: '#fff',
157+
// foo: 'var(--colors-bar)',
158+
// bar: 'var(--colors-baz-bcd, #000)',
159+
// baz: {
160+
// bcd: 'var(--colors-test, #fff)',
161+
// },
162+
// ^^^ don't do this
163+
164+
// issue #4994: Chaining CSS variables within a theme is strongly discouraged,
165+
// as it can cause browsers to fail to resolve variables to the correct values promptly during evaluation (or recalculation).
166+
// Furthermore, Uno does not perform in-depth analysis of the source of these references.
167+
primary: `var(--custom-css-variable, #123456)`,
168+
secondary: `calc(var(--another-css-variable, 10%) + 5%)`,
163169
},
164170
},
165171
})
166172

167-
const { css } = await uno.generate('c-foo')
173+
const { css } = await uno.generate('c-primary c-primary/50 c-secondary')
168174
expect(css).toMatchInlineSnapshot(`
169175
"/* layer: properties */
170176
@supports ((-webkit-hyphens: none) and (not (margin-trim: inline))) or ((-moz-orient: inline) and (not (color:rgb(from red r g b)))){*, ::before, ::after, ::backdrop{--un-text-opacity:100%;}}
171177
@property --un-text-opacity{syntax:"<percentage>";inherits:false;initial-value:100%;}
172-
/* layer: theme */
173-
:root, :host {
174-
--colors-foo: var(--colors-bar);
175-
--colors-bar: var(--colors-baz-bcd, #000);
176-
--colors-baz-bcd: var(--colors-test, #fff);
177-
--colors-test: #fff;
178-
}
179178
/* layer: default */
180-
.c-foo{color:color-mix(in srgb, var(--colors-foo) var(--un-text-opacity), transparent) /* var(--colors-bar) */;}
181-
@supports (color: color-mix(in lab, red, red)){
182-
.c-foo{color:color-mix(in oklab, var(--colors-foo) var(--un-text-opacity), transparent) /* var(--colors-bar) */;}
183-
}"
179+
.c-primary{color:color-mix(in srgb, var(--custom-css-variable, #123456) var(--un-text-opacity), transparent) /* var(--custom-css-variable, #123456) */;}
180+
.c-primary\\/50{color:color-mix(in srgb, var(--custom-css-variable, #123456) 50%, transparent) /* var(--custom-css-variable, #123456) */;}
181+
.c-secondary{color:color-mix(in srgb, calc(var(--another-css-variable, 10%) + 5%) var(--un-text-opacity), transparent) /* calc(var(--another-css-variable, 10%) + 5%) */;}"
184182
`)
185183
})
186184

0 commit comments

Comments
 (0)