/
combinators.ts
32 lines (30 loc) · 1.04 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
import type { Variant, VariantObject } from '@unocss/core'
const scopeMatcher = (strict: boolean, name: string, template: string): VariantObject => {
const re = strict
? new RegExp(`^${name}(?:-\\[(.+?)\\])[:-]`)
: new RegExp(`^${name}(?:-\\[(.+?)\\])?[:-]`)
return {
name: `combinator:${name}`,
match: (matcher: string) => {
const match = matcher.match(re)
if (match) {
return {
matcher: matcher.slice(match[0].length),
selector: s => template.replace('&&-s', s).replace('&&-c', match[1] ?? '*'),
}
}
},
multiPass: true,
}
}
export const variantCombinators: Variant[] = [
scopeMatcher(false, 'all', '&&-s &&-c'),
scopeMatcher(false, 'children', '&&-s>&&-c'),
scopeMatcher(false, 'next', '&&-s+&&-c'),
scopeMatcher(false, 'sibling', '&&-s+&&-c'),
scopeMatcher(false, 'siblings', '&&-s~&&-c'),
scopeMatcher(true, 'group', '&&-c &&-s'),
scopeMatcher(true, 'parent', '&&-c>&&-s'),
scopeMatcher(true, 'previous', '&&-c+&&-s'),
scopeMatcher(true, 'peer', '&&-c~&&-s'),
]