Skip to content

Commit 6a59f51

Browse files
committed
Improve automatic var injection (#12236)
* prevent automatic var injection for properties that accept `<dashed-ident>` for the value * add test * add `font-palette` * improve readability
1 parent 549d50d commit 6a59f51

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3030
- Allow plugins from a parent document to be used in an iframe ([#12208](https://github.com/tailwindlabs/tailwindcss/pull/12208))
3131
- Add types for `tailwindcss/nesting` ([#12269](https://github.com/tailwindlabs/tailwindcss/pull/12269))
3232
- Bump `jiti`, `fast-glob`, and `browserlist` dependencies ([#11550](https://github.com/tailwindlabs/tailwindcss/pull/11550))
33+
- Improve automatic `var` injection for properties that accept a `<dashed-ident>` ([#12236](https://github.com/tailwindlabs/tailwindcss/pull/12236))
3334

3435
## [3.3.3] - 2023-07-13
3536

src/lib/generateRules.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ function extractArbitraryProperty(classCandidate, context) {
496496
return null
497497
}
498498

499-
let normalized = normalize(value)
499+
let normalized = normalize(value, { property })
500500

501501
if (!isParsableCssValue(property, normalized)) {
502502
return null

src/util/dataTypes.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,34 @@ function isCSSFunction(value) {
1010
return cssFunctions.some((fn) => new RegExp(`^${fn}\\(.*\\)`).test(value))
1111
}
1212

13+
// These properties accept a `<dashed-ident>` as one of the values. This means that you can use them
14+
// as: `timeline-scope: --tl;`
15+
//
16+
// Without the `var(--tl)`, in these cases we don't want to normalize the value, and you should add
17+
// the `var()` yourself.
18+
//
19+
// More info:
20+
// - https://drafts.csswg.org/scroll-animations/#propdef-timeline-scope
21+
// - https://developer.mozilla.org/en-US/docs/Web/CSS/timeline-scope#dashed-ident
22+
//
23+
const AUTO_VAR_INJECTION_EXCEPTIONS = new Set([
24+
// Concrete properties
25+
'scroll-timeline-name',
26+
'timeline-scope',
27+
'view-timeline-name',
28+
'font-palette',
29+
30+
// Shorthand properties
31+
'scroll-timeline',
32+
'animation-timeline',
33+
'view-timeline',
34+
])
35+
1336
// This is not a data type, but rather a function that can normalize the
1437
// correct values.
15-
export function normalize(value, isRoot = true) {
16-
if (value.startsWith('--')) {
38+
export function normalize(value, context = null, isRoot = true) {
39+
let isVarException = context && AUTO_VAR_INJECTION_EXCEPTIONS.has(context.property)
40+
if (value.startsWith('--') && !isVarException) {
1741
return `var(${value})`
1842
}
1943

@@ -27,7 +51,7 @@ export function normalize(value, isRoot = true) {
2751
return part
2852
}
2953

30-
return normalize(part, false)
54+
return normalize(part, context, false)
3155
})
3256
.join('')
3357
}

tests/normalize-data-types.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { css, run } from './util/run'
12
import { normalize } from '../src/util/dataTypes'
23
import { crosscheck } from './util/run'
34

@@ -78,3 +79,31 @@ crosscheck(() => {
7879
expect(normalize(input)).toBe(output)
7980
})
8081
})
82+
83+
it('should not automatically inject the `var()` for properties that accept `<dashed-ident>` as the value', () => {
84+
let config = {
85+
content: [
86+
// Automatic var injection
87+
{ raw: '[color:--foo]' },
88+
89+
// Automatic var injection is skipped
90+
{ raw: '[timeline-scope:--foo]' },
91+
],
92+
}
93+
94+
let input = css`
95+
@tailwind utilities;
96+
`
97+
98+
return run(input, config).then((result) => {
99+
expect(result.css).toMatchFormattedCss(css`
100+
.\[color\:--foo\] {
101+
color: var(--foo);
102+
}
103+
104+
.\[timeline-scope\:--foo\] {
105+
timeline-scope: --foo;
106+
}
107+
`)
108+
})
109+
})

0 commit comments

Comments
 (0)