Skip to content

Commit e764df5

Browse files
authored
Support forcing coercion type with arbitrary value syntax (#4263)
* Support forcing coercion type with arbitrary value syntax * Refactor + more tests
1 parent 4a374eb commit e764df5

File tree

5 files changed

+29
-4
lines changed

5 files changed

+29
-4
lines changed

src/jit/lib/generateRules.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,13 @@ function* resolveMatchedPlugins(classCandidate, context) {
192192
}
193193
}
194194

195+
function splitWithSeparator(input, separator) {
196+
return input.split(new RegExp(`\\${separator}(?![^[]*\\])`, 'g'))
197+
}
198+
195199
function* resolveMatches(candidate, context) {
196200
let separator = context.tailwindConfig.separator
197-
let [classCandidate, ...variants] = candidate.split(separator).reverse()
201+
let [classCandidate, ...variants] = splitWithSeparator(candidate, separator).reverse()
198202
let important = false
199203

200204
if (classCandidate.startsWith('!')) {

src/jit/lib/setupContext.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,9 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
534534

535535
function wrapped(modifier) {
536536
let { type = 'any' } = options
537-
let value = coerceValue(type, modifier, options.values)
537+
let [value, coercedType] = coerceValue(type, modifier, options.values)
538538

539-
if (value === undefined) {
539+
if (type !== coercedType || value === undefined) {
540540
return []
541541
}
542542

src/util/pluginUtils.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,18 @@ let typeMap = {
204204
lookup: asLookupValue,
205205
}
206206

207+
function splitAtFirst(input, delim) {
208+
return (([first, ...rest]) => [first, rest.join(delim)])(input.split(delim))
209+
}
210+
207211
export function coerceValue(type, modifier, values) {
208-
return typeMap[type](modifier, values)
212+
if (modifier.startsWith('[') && modifier.endsWith(']')) {
213+
let [explicitType, value] = splitAtFirst(modifier.slice(1, -1), ':')
214+
215+
if (value.length > 0 && Object.keys(typeMap).includes(explicitType)) {
216+
return [asValue(`[${value}]`, values), explicitType]
217+
}
218+
}
219+
220+
return [typeMap[type](modifier, values), type]
209221
}

tests/jit/arbitrary-values.test.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,18 @@
269269
.text-\[2\.23rem\] {
270270
font-size: 2.23rem;
271271
}
272+
.text-\[length\:var\(--font-size\)\] {
273+
font-size: var(--font-size);
274+
}
272275
.leading-\[var\(--leading\)\] {
273276
line-height: var(--leading);
274277
}
275278
.tracking-\[var\(--tracking\)\] {
276279
letter-spacing: var(--tracking);
277280
}
281+
.text-\[color\:var\(--color\)\] {
282+
color: var(--color);
283+
}
278284
.placeholder-\[var\(--placeholder\)\]::placeholder {
279285
color: var(--placeholder);
280286
}

tests/jit/arbitrary-values.test.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
<div class="skew-x-[3px]"></div>
5555
<div class="skew-y-[3px]"></div>
5656
<div class="text-[2.23rem]"></div>
57+
<div class="text-[length:var(--font-size)]"></div>
58+
<div class="text-[color:var(--color)]"></div>
59+
<div class="text-[angle:var(--angle)]"></div>
5760
<div class="duration-[2s]"></div>
5861
<div class="m-[7px]"></div>
5962
<div class="mx-[7px]"></div>

0 commit comments

Comments
 (0)