Skip to content

Commit cbbfa82

Browse files
authored
Support variable shorthand for arbitrary modifiers (#9962)
* Support variable shorthand for arbitrary modifiers * Update changelog Co-authored-by: Adam Wathan <[email protected]>
1 parent 1d23dcb commit cbbfa82

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +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))
13+
- Support using variables as arbitrary values without `var(...)` ([#9880](https://github.com/tailwindlabs/tailwindcss/pull/9880), [#9962](https://github.com/tailwindlabs/tailwindcss/pull/9962))
1414
- Add `--watch=always` option to prevent exit when stdin closes ([#9966](https://github.com/tailwindlabs/tailwindcss/pull/9966))
1515

1616
### Fixed

src/util/pluginUtils.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ export function parseColorFormat(value) {
133133
return value
134134
}
135135

136+
function unwrapArbitraryModifier(modifier) {
137+
modifier = modifier.slice(1, -1)
138+
if (modifier.startsWith('--')) {
139+
modifier = `var(${modifier})`
140+
}
141+
return modifier
142+
}
143+
136144
export function asColor(modifier, options = {}, { tailwindConfig = {} } = {}) {
137145
if (options.values?.[modifier] !== undefined) {
138146
return parseColorFormat(options.values?.[modifier])
@@ -153,7 +161,7 @@ export function asColor(modifier, options = {}, { tailwindConfig = {} } = {}) {
153161
normalizedColor = parseColorFormat(normalizedColor)
154162

155163
if (isArbitraryValue(alpha)) {
156-
return withAlphaValue(normalizedColor, alpha.slice(1, -1))
164+
return withAlphaValue(normalizedColor, unwrapArbitraryModifier(alpha))
157165
}
158166

159167
if (tailwindConfig.theme?.opacity?.[alpha] === undefined) {
@@ -287,7 +295,7 @@ export function* getMatchingTypes(types, rawModifier, options, tailwindConfig) {
287295
if (configValue !== null) {
288296
utilityModifier = configValue
289297
} else if (isArbitraryValue(utilityModifier)) {
290-
utilityModifier = utilityModifier.slice(1, -1)
298+
utilityModifier = unwrapArbitraryModifier(utilityModifier)
291299
}
292300
}
293301
}

tests/arbitrary-values.test.js

+28
Original file line numberDiff line numberDiff line change
@@ -575,3 +575,31 @@ it('can use CSS variables as arbitrary values without `var()`', () => {
575575
`)
576576
})
577577
})
578+
579+
it('can use CSS variables as arbitrary modifiers without `var()`', () => {
580+
let config = {
581+
content: [
582+
{
583+
raw: html`<div class="text-sm/[--line-height] bg-red-500/[--opacity]"></div>`,
584+
},
585+
],
586+
corePlugins: { preflight: false },
587+
plugins: [],
588+
}
589+
590+
let input = css`
591+
@tailwind utilities;
592+
`
593+
594+
return run(input, config).then((result) => {
595+
expect(result.css).toMatchFormattedCss(css`
596+
.bg-red-500\/\[--opacity\] {
597+
background-color: rgb(239 68 68 / var(--opacity));
598+
}
599+
.text-sm\/\[--line-height\] {
600+
font-size: 0.875rem;
601+
line-height: var(--line-height);
602+
}
603+
`)
604+
})
605+
})

0 commit comments

Comments
 (0)