Skip to content

Commit b5f5adf

Browse files
authored
Support using variables as arbitrary values without var() (#9880)
* Support using variables as arbitrary values without var() * Update changelog * Add tests for variable fallback values Co-authored-by: Adam Wathan <[email protected]>
1 parent ea10bb9 commit b5f5adf

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- Add `line-height` modifier support to `font-size` utilities ([#9875](https://github.com/tailwindlabs/tailwindcss/pull/9875))
13+
- Support using variables as arbitrary values without `var(...)` ([#9880](https://github.com/tailwindlabs/tailwindcss/pull/9880))
1314

1415
### Fixed
1516

src/util/dataTypes.js

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function isCSSFunction(value) {
1313
// This is not a data type, but rather a function that can normalize the
1414
// correct values.
1515
export function normalize(value, isRoot = true) {
16+
if (value.startsWith('--')) {
17+
return `var(${value})`
18+
}
19+
1620
// Keep raw strings if it starts with `url(`
1721
if (value.includes('url(')) {
1822
return value

tests/arbitrary-values.test.js

+38
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,41 @@ it('can explicitly specify type for percentage and length', () => {
537537
`)
538538
})
539539
})
540+
541+
it('can use CSS variables as arbitrary values without `var()`', () => {
542+
let config = {
543+
content: [
544+
{
545+
raw: html`<div
546+
class="w-[--width-var] bg-[--color-var] bg-[--color-var,#000] bg-[length:--size-var] text-[length:--size-var,12px]"
547+
></div>`,
548+
},
549+
],
550+
corePlugins: { preflight: false },
551+
plugins: [],
552+
}
553+
554+
let input = css`
555+
@tailwind utilities;
556+
`
557+
558+
return run(input, config).then((result) => {
559+
expect(result.css).toMatchFormattedCss(css`
560+
.w-\[--width-var\] {
561+
width: var(--width-var);
562+
}
563+
.bg-\[--color-var\] {
564+
background-color: var(--color-var);
565+
}
566+
.bg-\[--color-var\2c \#000\] {
567+
background-color: var(--color-var, #000);
568+
}
569+
.bg-\[length\:--size-var\] {
570+
background-size: var(--size-var);
571+
}
572+
.text-\[length\:--size-var\2c 12px\] {
573+
font-size: var(--size-var, 12px);
574+
}
575+
`)
576+
})
577+
})

0 commit comments

Comments
 (0)