Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support using variables as arbitrary values without var() #9880

Merged
merged 4 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
`)
})
})