/
variant.ts
31 lines (26 loc) · 904 Bytes
/
variant.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
import type { VariantHandler, VariantObject } from '@unocss/core'
import type { TagifyOptions } from './types'
import { MARKER } from './extractor'
export const variantTagify = (options: TagifyOptions): VariantObject => {
const { extraProperties } = options
const prefix = `${MARKER}${options.prefix ?? ''}`
return {
name: 'tagify',
match(input) {
if (!input.startsWith(prefix))
return
const matcher = input.slice(prefix.length)
const handler: VariantHandler = {
matcher,
selector: i => i.slice(MARKER.length + 1),
}
if (extraProperties) {
if (typeof extraProperties === 'function')
handler.body = entries => [...entries, ...Object.entries(extraProperties(matcher) ?? {})]
else
handler.body = entries => [...entries, ...Object.entries(extraProperties)]
}
return handler
},
}
}