Skip to content

Commit 27db166

Browse files
authored
Support dynamic negation of DEFAULT values (#5718)
1 parent 8ec9497 commit 27db166

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/lib/generateRules.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ function* resolveMatchedPlugins(classCandidate, context) {
185185
candidatePrefix = twConfigPrefix + candidatePrefix.slice(twConfigPrefixLen + 1)
186186
}
187187

188+
if (negative && context.candidateRuleMap.has(candidatePrefix)) {
189+
yield [context.candidateRuleMap.get(candidatePrefix), '-DEFAULT']
190+
}
191+
188192
for (let [prefix, modifier] of candidatePermutations(candidatePrefix)) {
189193
if (context.candidateRuleMap.has(prefix)) {
190194
yield [context.candidateRuleMap.get(prefix), negative ? `-${modifier}` : modifier]
@@ -238,7 +242,7 @@ function* resolveMatches(candidate, context) {
238242
}
239243
}
240244
// Only process static plugins on exact matches
241-
else if (modifier === 'DEFAULT') {
245+
else if (modifier === 'DEFAULT' || modifier === '-DEFAULT') {
242246
let ruleSet = plugin
243247
let [rules, options] = parseRules(ruleSet, context.postCssNodeCache)
244248
for (let rule of rules) {

src/util/nameClass.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function formatClass(classPrefix, key) {
1414
return classPrefix
1515
}
1616

17-
if (key === '-') {
17+
if (key === '-' || key === '-DEFAULT') {
1818
return `-${classPrefix}`
1919
}
2020

tests/negative-prefix.test.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,49 @@ test('negating a default value', () => {
254254
})
255255
})
256256

257+
test('using a negative prefix with a negative default scale value', () => {
258+
let config = {
259+
content: [{ raw: html`<div class="mt -mt"></div>` }],
260+
theme: {
261+
margin: {
262+
DEFAULT: '8px',
263+
'-DEFAULT': '-4px',
264+
},
265+
},
266+
}
267+
268+
return run('@tailwind utilities', config).then((result) => {
269+
return expect(result.css).toMatchCss(css`
270+
.mt {
271+
margin-top: 8px;
272+
}
273+
.-mt {
274+
margin-top: -4px;
275+
}
276+
`)
277+
})
278+
})
279+
280+
test('negating a default value with a configured prefix', () => {
281+
let config = {
282+
prefix: 'tw-',
283+
content: [{ raw: html`<div class="tw--mt"></div>` }],
284+
theme: {
285+
margin: {
286+
DEFAULT: '15px',
287+
},
288+
},
289+
}
290+
291+
return run('@tailwind utilities', config).then((result) => {
292+
return expect(result.css).toMatchCss(css`
293+
.tw--mt {
294+
margin-top: -15px;
295+
}
296+
`)
297+
})
298+
})
299+
257300
test('arbitrary value keywords should be ignored', () => {
258301
let config = {
259302
content: [{ raw: html`<div class="-mt-[auto]"></div>` }],

0 commit comments

Comments
 (0)