Skip to content

Commit 17b81b4

Browse files
Allow negating utilities using min/max/clamp (#9237)
* Allow negating utilities using min/max/clamp * Update changelog
1 parent 8b1bf80 commit 17b81b4

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Add a standalone CLI build for 32-bit Linux on ARM (`node16-linux-armv7`) ([#9084](https://github.com/tailwindlabs/tailwindcss/pull/9084))
1414
- Add future flag to disable color opacity utility plugins ([#9088](https://github.com/tailwindlabs/tailwindcss/pull/9088))
1515
- Add negative value support for `outline-offset` ([#9136](https://github.com/tailwindlabs/tailwindcss/pull/9136))
16+
- Allow negating utilities using min/max/clamp ([#9237](https://github.com/tailwindlabs/tailwindcss/pull/9237))
1617

1718
### Fixed
1819

src/util/negateValue.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@ export default function (value) {
1010
return value.replace(/^[+-]?/, (sign) => (sign === '-' ? '' : '-'))
1111
}
1212

13-
if (value.includes('var(') || value.includes('calc(')) {
14-
return `calc(${value} * -1)`
13+
// What functions we support negating numeric values for
14+
// var() isn't inherently a numeric function but we support it anyway
15+
// The trigonometric functions are omitted because you'll need to use calc(…) with them _anyway_
16+
// to produce generally useful results and that will be covered already
17+
let numericFunctions = ['var', 'calc', 'min', 'max', 'clamp']
18+
19+
for (const fn of numericFunctions) {
20+
if (value.includes(`${fn}(`)) {
21+
return `calc(${value} * -1)`
22+
}
1523
}
1624
}

tests/negative-prefix.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,42 @@ test('a value that includes a calc', () => {
146146
})
147147
})
148148

149+
test('a value that includes min/max/clamp functions', () => {
150+
let config = {
151+
content: [{ raw: html`<div class="mt-min -mt-min mt-max -mt-max mt-clamp -mt-clamp"></div>` }],
152+
theme: {
153+
margin: {
154+
min: 'min(100vmin, 3rem)',
155+
max: 'max(100vmax, 3rem)',
156+
clamp: 'clamp(1rem, 100vh, 3rem)',
157+
},
158+
},
159+
}
160+
161+
return run('@tailwind utilities', config).then((result) => {
162+
return expect(result.css).toMatchCss(css`
163+
.mt-min {
164+
margin-top: min(100vmin, 3rem);
165+
}
166+
.-mt-min {
167+
margin-top: calc(min(100vmin, 3rem) * -1);
168+
}
169+
.mt-max {
170+
margin-top: max(100vmax, 3rem);
171+
}
172+
.-mt-max {
173+
margin-top: calc(max(100vmax, 3rem) * -1);
174+
}
175+
.mt-clamp {
176+
margin-top: clamp(1rem, 100vh, 3rem);
177+
}
178+
.-mt-clamp {
179+
margin-top: calc(clamp(1rem, 100vh, 3rem) * -1);
180+
}
181+
`)
182+
})
183+
})
184+
149185
test('a keyword value', () => {
150186
let config = {
151187
content: [{ raw: html`<div class="-mt-auto mt-auto"></div>` }],

0 commit comments

Comments
 (0)