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
This commit is contained in:
parent
4a8f584176
commit
89ec6fbe67
3 changed files with 57 additions and 4 deletions
|
|
@ -491,7 +491,7 @@ function extractArbitraryProperty(classCandidate, context) {
|
|||
return null
|
||||
}
|
||||
|
||||
let normalized = normalize(value)
|
||||
let normalized = normalize(value, { property })
|
||||
|
||||
if (!isParsableCssValue(property, normalized)) {
|
||||
return null
|
||||
|
|
|
|||
|
|
@ -11,10 +11,34 @@ function isCSSFunction(value) {
|
|||
return IS_CSS_FN.test(value)
|
||||
}
|
||||
|
||||
// These properties accept a `<dashed-ident>` as one of the values. This means that you can use them
|
||||
// as: `timeline-scope: --tl;`
|
||||
//
|
||||
// Without the `var(--tl)`, in these cases we don't want to normalize the value, and you should add
|
||||
// the `var()` yourself.
|
||||
//
|
||||
// More info:
|
||||
// - https://drafts.csswg.org/scroll-animations/#propdef-timeline-scope
|
||||
// - https://developer.mozilla.org/en-US/docs/Web/CSS/timeline-scope#dashed-ident
|
||||
//
|
||||
const AUTO_VAR_INJECTION_EXCEPTIONS = new Set([
|
||||
// Concrete properties
|
||||
'scroll-timeline-name',
|
||||
'timeline-scope',
|
||||
'view-timeline-name',
|
||||
'font-palette',
|
||||
|
||||
// Shorthand properties
|
||||
'scroll-timeline',
|
||||
'animation-timeline',
|
||||
'view-timeline',
|
||||
])
|
||||
|
||||
// This is not a data type, but rather a function that can normalize the
|
||||
// correct values.
|
||||
export function normalize(value, isRoot = true) {
|
||||
if (value.startsWith('--')) {
|
||||
export function normalize(value, context = null, isRoot = true) {
|
||||
let isVarException = context && AUTO_VAR_INJECTION_EXCEPTIONS.has(context.property)
|
||||
if (value.startsWith('--') && !isVarException) {
|
||||
return `var(${value})`
|
||||
}
|
||||
|
||||
|
|
@ -28,7 +52,7 @@ export function normalize(value, isRoot = true) {
|
|||
return part
|
||||
}
|
||||
|
||||
return normalize(part, false)
|
||||
return normalize(part, context, false)
|
||||
})
|
||||
.join('')
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { css, run } from './util/run'
|
||||
import { normalize } from '../src/util/dataTypes'
|
||||
|
||||
let table = [
|
||||
|
|
@ -75,3 +76,31 @@ let table = [
|
|||
it.each(table)('normalize data: %s', (input, output) => {
|
||||
expect(normalize(input)).toBe(output)
|
||||
})
|
||||
|
||||
it('should not automatically inject the `var()` for properties that accept `<dashed-ident>` as the value', () => {
|
||||
let config = {
|
||||
content: [
|
||||
// Automatic var injection
|
||||
{ raw: '[color:--foo]' },
|
||||
|
||||
// Automatic var injection is skipped
|
||||
{ raw: '[timeline-scope:--foo]' },
|
||||
],
|
||||
}
|
||||
|
||||
let input = css`
|
||||
@tailwind utilities;
|
||||
`
|
||||
|
||||
return run(input, config).then((result) => {
|
||||
expect(result.css).toMatchFormattedCss(css`
|
||||
.\[color\:--foo\] {
|
||||
color: var(--foo);
|
||||
}
|
||||
|
||||
.\[timeline-scope\:--foo\] {
|
||||
timeline-scope: --foo;
|
||||
}
|
||||
`)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue