-
-
Notifications
You must be signed in to change notification settings - Fork 778
/
negative.ts
54 lines (49 loc) · 1.37 KB
/
negative.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { Variant } from '@unocss/core'
import { getComponent } from '@unocss/rule-utils'
import { CONTROL_MINI_NO_NEGATIVE } from '../utils'
const numberRE = /[0-9.]+(?:[a-z]+|%)?/
const ignoreProps = [
/opacity|color|flex/,
]
function 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) {
if (!matcher.startsWith('-'))
return
return {
matcher: matcher.slice(1),
body: (body) => {
if (body.find(v => v[0] === CONTROL_MINI_NO_NEGATIVE))
return
let changed = false
body.forEach((v) => {
const value = v[1]?.toString()
if (!value || value === '0')
return
if (ignoreProps.some(i => v[0].match(i)))
return
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
}
})
if (changed)
return body
return []
},
}
},
}