/
combinators.ts
43 lines (36 loc) · 1.07 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
41
42
43
import type { Variant, VariantHandler, VariantObject } from '@unocss/core'
import { getComponent, handler as h } from '../utils'
const scopeMatcher = (name: string, combinator: string): VariantObject => ({
name: `combinator:${name}`,
match: (matcher: string): VariantHandler | undefined => {
if (!matcher.startsWith(name))
return
let newMatcher = matcher.substring(name.length + 1)
const body = getComponent(newMatcher, '[', ']', [':', '-'])
if (!body)
return
const [match, rest] = body
let bracketValue = h.bracket(match) ?? ''
if (bracketValue === '') {
bracketValue = '*'
}
else {
if (matcher[name.length] !== '-')
return
if (rest !== '')
newMatcher = rest
}
return {
matcher: newMatcher,
selector: s => `${s}${combinator}${bracketValue}`,
}
},
multiPass: true,
})
export const variantCombinators: Variant[] = [
scopeMatcher('all', ' '),
scopeMatcher('children', '>'),
scopeMatcher('next', '+'),
scopeMatcher('sibling', '+'),
scopeMatcher('siblings', '~'),
]