Skip to content

Commit

Permalink
Support forcing coercion type with arbitrary value syntax (#4263)
Browse files Browse the repository at this point in the history
* Support forcing coercion type with arbitrary value syntax

* Refactor + more tests
  • Loading branch information
adamwathan committed May 7, 2021
1 parent 4a374eb commit e764df5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/jit/lib/generateRules.js
Expand Up @@ -192,9 +192,13 @@ function* resolveMatchedPlugins(classCandidate, context) {
}
}

function splitWithSeparator(input, separator) {
return input.split(new RegExp(`\\${separator}(?![^[]*\\])`, 'g'))
}

function* resolveMatches(candidate, context) {
let separator = context.tailwindConfig.separator
let [classCandidate, ...variants] = candidate.split(separator).reverse()
let [classCandidate, ...variants] = splitWithSeparator(candidate, separator).reverse()
let important = false

if (classCandidate.startsWith('!')) {
Expand Down
4 changes: 2 additions & 2 deletions src/jit/lib/setupContext.js
Expand Up @@ -534,9 +534,9 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs

function wrapped(modifier) {
let { type = 'any' } = options
let value = coerceValue(type, modifier, options.values)
let [value, coercedType] = coerceValue(type, modifier, options.values)

if (value === undefined) {
if (type !== coercedType || value === undefined) {
return []
}

Expand Down
14 changes: 13 additions & 1 deletion src/util/pluginUtils.js
Expand Up @@ -204,6 +204,18 @@ let typeMap = {
lookup: asLookupValue,
}

function splitAtFirst(input, delim) {
return (([first, ...rest]) => [first, rest.join(delim)])(input.split(delim))
}

export function coerceValue(type, modifier, values) {
return typeMap[type](modifier, values)
if (modifier.startsWith('[') && modifier.endsWith(']')) {
let [explicitType, value] = splitAtFirst(modifier.slice(1, -1), ':')

if (value.length > 0 && Object.keys(typeMap).includes(explicitType)) {
return [asValue(`[${value}]`, values), explicitType]
}
}

return [typeMap[type](modifier, values), type]
}
6 changes: 6 additions & 0 deletions tests/jit/arbitrary-values.test.css
Expand Up @@ -269,12 +269,18 @@
.text-\[2\.23rem\] {
font-size: 2.23rem;
}
.text-\[length\:var\(--font-size\)\] {
font-size: var(--font-size);
}
.leading-\[var\(--leading\)\] {
line-height: var(--leading);
}
.tracking-\[var\(--tracking\)\] {
letter-spacing: var(--tracking);
}
.text-\[color\:var\(--color\)\] {
color: var(--color);
}
.placeholder-\[var\(--placeholder\)\]::placeholder {
color: var(--placeholder);
}
Expand Down
3 changes: 3 additions & 0 deletions tests/jit/arbitrary-values.test.html
Expand Up @@ -54,6 +54,9 @@
<div class="skew-x-[3px]"></div>
<div class="skew-y-[3px]"></div>
<div class="text-[2.23rem]"></div>
<div class="text-[length:var(--font-size)]"></div>
<div class="text-[color:var(--color)]"></div>
<div class="text-[angle:var(--angle)]"></div>
<div class="duration-[2s]"></div>
<div class="m-[7px]"></div>
<div class="mx-[7px]"></div>
Expand Down

0 comments on commit e764df5

Please sign in to comment.