Skip to content

Commit

Permalink
Allow negating utilities using min/max/clamp (#9237)
Browse files Browse the repository at this point in the history
* Allow negating utilities using min/max/clamp

* Update changelog
  • Loading branch information
thecrypticace committed Sep 1, 2022
1 parent 8b1bf80 commit 17b81b4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add a standalone CLI build for 32-bit Linux on ARM (`node16-linux-armv7`) ([#9084](https://github.com/tailwindlabs/tailwindcss/pull/9084))
- Add future flag to disable color opacity utility plugins ([#9088](https://github.com/tailwindlabs/tailwindcss/pull/9088))
- Add negative value support for `outline-offset` ([#9136](https://github.com/tailwindlabs/tailwindcss/pull/9136))
- Allow negating utilities using min/max/clamp ([#9237](https://github.com/tailwindlabs/tailwindcss/pull/9237))

### Fixed

Expand Down
12 changes: 10 additions & 2 deletions src/util/negateValue.js
Expand Up @@ -10,7 +10,15 @@ export default function (value) {
return value.replace(/^[+-]?/, (sign) => (sign === '-' ? '' : '-'))
}

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

for (const fn of numericFunctions) {
if (value.includes(`${fn}(`)) {
return `calc(${value} * -1)`
}
}
}
36 changes: 36 additions & 0 deletions tests/negative-prefix.test.js
Expand Up @@ -146,6 +146,42 @@ test('a value that includes a calc', () => {
})
})

test('a value that includes min/max/clamp functions', () => {
let config = {
content: [{ raw: html`<div class="mt-min -mt-min mt-max -mt-max mt-clamp -mt-clamp"></div>` }],
theme: {
margin: {
min: 'min(100vmin, 3rem)',
max: 'max(100vmax, 3rem)',
clamp: 'clamp(1rem, 100vh, 3rem)',
},
},
}

return run('@tailwind utilities', config).then((result) => {
return expect(result.css).toMatchCss(css`
.mt-min {
margin-top: min(100vmin, 3rem);
}
.-mt-min {
margin-top: calc(min(100vmin, 3rem) * -1);
}
.mt-max {
margin-top: max(100vmax, 3rem);
}
.-mt-max {
margin-top: calc(max(100vmax, 3rem) * -1);
}
.mt-clamp {
margin-top: clamp(1rem, 100vh, 3rem);
}
.-mt-clamp {
margin-top: calc(clamp(1rem, 100vh, 3rem) * -1);
}
`)
})
})

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

0 comments on commit 17b81b4

Please sign in to comment.