Support forcing coercion type with arbitrary value syntax (#4263)

* Support forcing coercion type with arbitrary value syntax

* Refactor + more tests
This commit is contained in:
Adam Wathan 2021-05-07 08:59:24 -04:00 committed by GitHub
parent 4a374ebe5e
commit e764df5055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 30 additions and 5 deletions

View File

@ -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('!')) {

View File

@ -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 []
}

View File

@ -204,6 +204,18 @@ let typeMap = {
lookup: asLookupValue,
}
export function coerceValue(type, modifier, values) {
return typeMap[type](modifier, values)
function splitAtFirst(input, delim) {
return (([first, ...rest]) => [first, rest.join(delim)])(input.split(delim))
}
export function coerceValue(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]
}

View File

@ -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);
}

View File

@ -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>