Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow negating utilities using min/max/clamp #9237

Merged
merged 2 commits into from Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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