Skip to content

Commit

Permalink
feat(preset-mini): negate math function values
Browse files Browse the repository at this point in the history
  • Loading branch information
chu121su12 committed Oct 6, 2022
1 parent 76b883f commit 92c0448
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
18 changes: 16 additions & 2 deletions packages/preset-mini/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 @@ -116,7 +116,10 @@ div:hover .group-\\\\[div\\\\:hover\\\\]-\\\\[combinator\\\\:test-4\\\\]{combina
.mxy{margin:1rem;}
.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:0rem;}
Expand Down
3 changes: 3 additions & 0 deletions test/assets/preset-mini-targets.ts
Expand Up @@ -869,6 +869,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 @@ -23,6 +23,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 92c0448

Please sign in to comment.