/
core.ts
130 lines (117 loc) · 3.78 KB
/
core.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import type { Preset } from '@unocss/core'
import { warnOnce } from '@unocss/core'
import type {
IconifyLoaderOptions,
UniversalIconLoader,
} from '@iconify/utils/lib/loader/types'
import { encodeSvgForCss } from '@iconify/utils/lib/svg/encode-svg-for-css'
import type { IconsOptions } from './types'
const COLLECTION_NAME_PARTS_MAX = 3
export { IconsOptions }
export function createPresetIcons(lookupIconLoader: (options: IconsOptions) => Promise<UniversalIconLoader>) {
return function presetIcons(options: IconsOptions = {}): Preset {
const {
scale = 1,
mode = 'auto',
prefix = 'i-',
warn = false,
collections: customCollections,
extraProperties = {},
customizations = {},
autoInstall = false,
layer = 'icons',
unit,
} = options
const loaderOptions: IconifyLoaderOptions = {
addXmlNs: true,
scale,
customCollections,
autoInstall,
// avoid warn from @iconify/loader: we'll warn below if not found
warn: undefined,
customizations: {
...customizations,
additionalProps: { ...extraProperties },
trimCustomSvg: true,
async iconCustomizer(collection, icon, props) {
await customizations.iconCustomizer?.(collection, icon, props)
if (unit) {
if (!props.width)
props.width = `${scale}${unit}`
if (!props.height)
props.height = `${scale}${unit}`
}
},
},
}
let iconLoader: UniversalIconLoader
return {
name: '@unocss/preset-icons',
enforce: 'pre',
options,
layers: { icons: -30 },
rules: [[
/^([a-z0-9:-]+)(?:\?(mask|bg|auto))?$/,
async ([full, body, _mode = mode]) => {
let collection = ''
let name = ''
let svg: string | undefined
iconLoader = iconLoader || await lookupIconLoader(options)
const usedProps = {}
if (body.includes(':')) {
[collection, name] = body.split(':')
svg = await iconLoader(collection, name, { ...loaderOptions, usedProps })
}
else {
const parts = body.split(/-/g)
for (let i = COLLECTION_NAME_PARTS_MAX; i >= 1; i--) {
collection = parts.slice(0, i).join('-')
name = parts.slice(i).join('-')
svg = await iconLoader(collection, name, { ...loaderOptions, usedProps })
if (svg)
break
}
}
if (!svg) {
if (warn)
warnOnce(`failed to load icon "${full}"`)
return
}
const url = `url("data:image/svg+xml;utf8,${encodeSvgForCss(svg)}")`
if (_mode === 'auto')
_mode = svg.includes('currentColor') ? 'mask' : 'bg'
if (_mode === 'mask') {
// Thanks to https://codepen.io/noahblon/post/coloring-svgs-in-css-background-images
return {
'--un-icon': url,
'mask': 'var(--un-icon) no-repeat',
'mask-size': '100% 100%',
'-webkit-mask': 'var(--un-icon) no-repeat',
'-webkit-mask-size': '100% 100%',
'background-color': 'currentColor',
...usedProps,
}
}
else {
return {
'background': `${url} no-repeat`,
'background-size': '100% 100%',
'background-color': 'transparent',
...usedProps,
}
}
},
{ layer, prefix },
]],
}
}
}
export function combineLoaders(loaders: UniversalIconLoader[]) {
return <UniversalIconLoader>((...args) => {
for (const loader of loaders) {
const result = loader(...args)
if (result)
return result
}
})
}