Skip to content

Commit

Permalink
Support using variables as arbitrary values without var() (#9880)
Browse files Browse the repository at this point in the history
* Support using variables as arbitrary values without var()

* Update changelog

* Add tests for variable fallback values

Co-authored-by: Adam Wathan <4323180+adamwathan@users.noreply.github.com>
  • Loading branch information
adamwathan and adamwathan authored Nov 22, 2022
1 parent ea10bb9 commit b5f5adf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

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

### Fixed

Expand Down
4 changes: 4 additions & 0 deletions src/util/dataTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function isCSSFunction(value) {
// 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('--')) {
return `var(${value})`
}

// Keep raw strings if it starts with `url(`
if (value.includes('url(')) {
return value
Expand Down
38 changes: 38 additions & 0 deletions tests/arbitrary-values.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,41 @@ it('can explicitly specify type for percentage and length', () => {
`)
})
})

it('can use CSS variables as arbitrary values without `var()`', () => {
let config = {
content: [
{
raw: html`<div
class="w-[--width-var] bg-[--color-var] bg-[--color-var,#000] bg-[length:--size-var] text-[length:--size-var,12px]"
></div>`,
},
],
corePlugins: { preflight: false },
plugins: [],
}

let input = css`
@tailwind utilities;
`

return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
.w-\[--width-var\] {
width: var(--width-var);
}
.bg-\[--color-var\] {
background-color: var(--color-var);
}
.bg-\[--color-var\2c \#000\] {
background-color: var(--color-var, #000);
}
.bg-\[length\:--size-var\] {
background-size: var(--size-var);
}
.text-\[length\:--size-var\2c 12px\] {
font-size: var(--size-var, 12px);
}
`)
})
})

0 comments on commit b5f5adf

Please sign in to comment.