/
combinators.ts
40 lines (35 loc) · 1.02 KB
/
combinators.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
import type { Variant, VariantObject } from '@unocss/core'
import { handler as h, variantGetBracket } from '../utils'
const scopeMatcher = (name: string, combinator: string): VariantObject => ({
name: `combinator:${name}`,
match(matcher) {
if (!matcher.startsWith(name))
return
let body = variantGetBracket(name, matcher, [':', '-'])
if (!body) {
for (const separator of [':', '-']) {
if (matcher.startsWith(`${name}${separator}`)) {
body = ['', matcher.slice(name.length + separator.length)]
break
}
}
if (!body)
return
}
let bracketValue = h.bracket(body[0]) ?? ''
if (bracketValue === '')
bracketValue = '*'
return {
matcher: body[1],
selector: s => `${s}${combinator}${bracketValue}`,
}
},
multiPass: true,
})
export const variantCombinators: Variant[] = [
scopeMatcher('all', ' '),
scopeMatcher('children', '>'),
scopeMatcher('next', '+'),
scopeMatcher('sibling', '+'),
scopeMatcher('siblings', '~'),
]