Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Support dynamic negation of DEFAULT values (#5718)
  • Loading branch information
bradlc authored Oct 6, 2021
commit 27db166531249b2075e6d0042b08326e269f4c6f
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