Skip to content

Commit

Permalink
Support forcing coercion type with arbitrary value syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed May 6, 2021
1 parent 76c670f commit e7f3f9a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/jit/lib/generateRules.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
11 changes: 10 additions & 1 deletion src/util/pluginUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,5 +205,14 @@ let typeMap = {
}

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

if (parts.length > 1 && Object.keys(typeMap).includes(parts[0])) {
return [asValue(`[${parts.slice(1).join(':')}]`, values), parts[0]]
}
}

return [typeMap[type](modifier, values), type]
}
3 changes: 3 additions & 0 deletions tests/jit/arbitrary-values.test.css
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@
.text-\[2\.23rem\] {
font-size: 2.23rem;
}
.text-\[length\:var\(--font-size\)\] {
font-size: var(--font-size);
}
.leading-\[var\(--leading\)\] {
line-height: var(--leading);
}
Expand Down
1 change: 1 addition & 0 deletions tests/jit/arbitrary-values.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<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="duration-[2s]"></div>
<div class="m-[7px]"></div>
<div class="mx-[7px]"></div>
Expand Down

0 comments on commit e7f3f9a

Please sign in to comment.