Skip to content

Commit

Permalink
Support dynamic negation of DEFAULT values (#5718)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradlc committed Oct 6, 2021
1 parent 8ec9497 commit 27db166
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/lib/generateRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ function* resolveMatchedPlugins(classCandidate, context) {
candidatePrefix = twConfigPrefix + candidatePrefix.slice(twConfigPrefixLen + 1)
}

if (negative && context.candidateRuleMap.has(candidatePrefix)) {
yield [context.candidateRuleMap.get(candidatePrefix), '-DEFAULT']
}

for (let [prefix, modifier] of candidatePermutations(candidatePrefix)) {
if (context.candidateRuleMap.has(prefix)) {
yield [context.candidateRuleMap.get(prefix), negative ? `-${modifier}` : modifier]
Expand Down Expand Up @@ -238,7 +242,7 @@ function* resolveMatches(candidate, context) {
}
}
// Only process static plugins on exact matches
else if (modifier === 'DEFAULT') {
else if (modifier === 'DEFAULT' || modifier === '-DEFAULT') {
let ruleSet = plugin
let [rules, options] = parseRules(ruleSet, context.postCssNodeCache)
for (let rule of rules) {
Expand Down
2 changes: 1 addition & 1 deletion src/util/nameClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function formatClass(classPrefix, key) {
return classPrefix
}

if (key === '-') {
if (key === '-' || key === '-DEFAULT') {
return `-${classPrefix}`
}

Expand Down
43 changes: 43 additions & 0 deletions tests/negative-prefix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,49 @@ test('negating a default value', () => {
})
})

test('using a negative prefix with a negative default scale value', () => {
let config = {
content: [{ raw: html`<div class="mt -mt"></div>` }],
theme: {
margin: {
DEFAULT: '8px',
'-DEFAULT': '-4px',
},
},
}

return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchCss(css`
.mt {
margin-top: 8px;
}
.-mt {
margin-top: -4px;
}
`)
})
})

test('negating a default value with a configured prefix', () => {
let config = {
prefix: 'tw-',
content: [{ raw: html`<div class="tw--mt"></div>` }],
theme: {
margin: {
DEFAULT: '15px',
},
},
}

return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchCss(css`
.tw--mt {
margin-top: -15px;
}
`)
})
})

test('arbitrary value keywords should be ignored', () => {
let config = {
content: [{ raw: html`<div class="-mt-[auto]"></div>` }],
Expand Down

0 comments on commit 27db166

Please sign in to comment.