Skip to content

Commit

Permalink
feat: negate math function values (unocss/unocss#1688)
Browse files Browse the repository at this point in the history
  • Loading branch information
MellowCo committed Oct 28, 2022
1 parent 2d12989 commit 2f80e00
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/variants/negative.ts
@@ -1,12 +1,21 @@
import type { Variant } from '@unocss/core'
import { CONTROL_MINI_NO_NEGATIVE } from '../utils'
import { CONTROL_MINI_NO_NEGATIVE, getComponent } from '../utils'

const numberRE = /[0-9.]+(?:[a-z]+|%)?/

const ignoreProps = [
/opacity|color|flex/,
]

const negateFunctions = (value: string) => {
const match = value.match(/^(calc|clamp|max|min)\s*(\(.*)/)
if (match) {
const [fnBody, rest] = getComponent(match[2], '(', ')', ' ') ?? []
if (fnBody)
return `calc(${match[1]}${fnBody} * -1)${rest ? ` ${rest}` : ''}`
}
}

export const variantNegative: Variant = {
name: 'negative',
match(matcher) {
Expand All @@ -25,7 +34,12 @@ export const variantNegative: Variant = {
return
if (ignoreProps.some(i => v[0].match(i)))
return
if (numberRE.test(value)) {
const negated = negateFunctions(value)
if (negated) {
v[1] = negated
changed = true
}
else if (numberRE.test(value)) {
v[1] = value.replace(numberRE, i => `-${i}`)
changed = true
}
Expand Down
3 changes: 3 additions & 0 deletions test/__snapshots__/preset-mini.test.ts.snap
Expand Up @@ -223,7 +223,10 @@ div:hover .group-\\\\[div\\\\:hover\\\\]-\\\\[combinator\\\\:test-4\\\\]{combina
.mxy{margin:32rpx;}
.my-auto{margin-top:auto;margin-bottom:auto;}
.my-revert-layer{margin-top:revert-layer;margin-bottom:revert-layer;}
.-\\\\!mb-safe{margin-bottom:calc(max(env(safe-area-inset-left), env(safe-area-inset-right)) * -1) !important;}
.-mb-px{margin-bottom:-1px;}
.-mt-safe{margin-top:calc(max(env(safe-area-inset-left), env(safe-area-inset-right)) * -1);}
.\\\\!-ms-safe{margin-inline-start:calc(max(env(safe-area-inset-left), env(safe-area-inset-right)) * -1) !important;}
.mt-\\\\[-10\\\\.2\\\\%\\\\]{margin-top:-10.2%;}
.mt-\\\\$height{margin-top:var(--height);}
.next\\\\:mt-0+*{margin-top:0rpx;}
Expand Down
3 changes: 3 additions & 0 deletions test/assets/preset-mini-targets.ts
Expand Up @@ -1000,6 +1000,9 @@ export const presetMiniTargets: string[] = [
'important:p-3',
'sm:important:p-3',
'p3!',
'-mt-safe',
'-!mb-safe',
'!-ms-safe',

// variants class
'all-[.target]-[combinator:test-2]',
Expand Down
3 changes: 3 additions & 0 deletions test/preset-mini.test.ts
Expand Up @@ -24,6 +24,9 @@ const uno = createGenerator({
camelCase: '#234',
},
},
spacing: {
safe: 'max(env(safe-area-inset-left), env(safe-area-inset-right))',
},
},
})

Expand Down

0 comments on commit 2f80e00

Please sign in to comment.