-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
VIcon.ts
239 lines (203 loc) Β· 5.77 KB
/
VIcon.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import './VIcon.sass'
// Mixins
import BindsAttrs from '../../mixins/binds-attrs'
import Colorable from '../../mixins/colorable'
import Sizeable from '../../mixins/sizeable'
import Themeable from '../../mixins/themeable'
// Util
import { convertToUnit, keys, remapInternalIcon } from '../../util/helpers'
// Types
import Vue, { CreateElement, VNode, VNodeChildren, VNodeData } from 'vue'
import mixins from '../../util/mixins'
import { VuetifyIcon, VuetifyIconComponent } from 'vuetify/types/services/icons'
enum SIZE_MAP {
xSmall = '12px',
small = '16px',
default = '24px',
medium = '28px',
large = '36px',
xLarge = '40px'
}
function isFontAwesome5 (iconType: string): boolean {
return ['fas', 'far', 'fal', 'fab', 'fad'].some(val => iconType.includes(val))
}
function isSvgPath (icon: string): boolean {
return (/^[mzlhvcsqta]\s*[-+.0-9][^mlhvzcsqta]+/i.test(icon) && /[\dz]$/i.test(icon) && icon.length > 4)
}
const VIcon = mixins(
BindsAttrs,
Colorable,
Sizeable,
Themeable
/* @vue/component */
).extend({
name: 'v-icon',
props: {
dense: Boolean,
disabled: Boolean,
left: Boolean,
right: Boolean,
size: [Number, String],
tag: {
type: String,
required: false,
default: 'i',
},
},
computed: {
medium () {
return false
},
},
methods: {
getIcon (): VuetifyIcon {
let iconName = ''
if (this.$slots.default) iconName = this.$slots.default[0].text!.trim()
return remapInternalIcon(this, iconName)
},
getSize (): string | undefined {
const sizes = {
xSmall: this.xSmall,
small: this.small,
medium: this.medium,
large: this.large,
xLarge: this.xLarge,
}
const explicitSize = keys(sizes).find(key => sizes[key])
return (
(explicitSize && SIZE_MAP[explicitSize]) || convertToUnit(this.size)
)
},
// Component data for both font and svg icon.
getDefaultData (): VNodeData {
const hasClickListener = Boolean(
this.listeners$.click || this.listeners$['!click']
)
const data: VNodeData = {
staticClass: 'v-icon notranslate',
class: {
'v-icon--disabled': this.disabled,
'v-icon--left': this.left,
'v-icon--link': hasClickListener,
'v-icon--right': this.right,
'v-icon--dense': this.dense,
},
attrs: {
'aria-hidden': !hasClickListener,
role: hasClickListener ? 'button' : null,
tabindex: hasClickListener ? 0 : undefined,
...this.attrs$,
},
on: this.listeners$,
}
return data
},
applyColors (data: VNodeData): void {
data.class = { ...data.class, ...this.themeClasses }
this.setTextColor(this.color, data)
},
renderFontIcon (icon: string, h: CreateElement): VNode {
const newChildren: VNodeChildren = []
const data = this.getDefaultData()
let iconType = 'material-icons'
// Material Icon delimiter is _
// https://material.io/icons/
const delimiterIndex = icon.indexOf('-')
const isMaterialIcon = delimiterIndex <= -1
if (isMaterialIcon) {
// Material icon uses ligatures.
newChildren.push(icon)
} else {
iconType = icon.slice(0, delimiterIndex)
if (isFontAwesome5(iconType)) iconType = ''
}
data.class[iconType] = true
data.class[icon] = !isMaterialIcon
const fontSize = this.getSize()
if (fontSize) data.style = { fontSize }
this.applyColors(data)
return h(this.tag, data, newChildren)
},
renderSvgIcon (icon: string, h: CreateElement): VNode {
const fontSize = this.getSize()
const wrapperData = {
...this.getDefaultData(),
style: fontSize ? {
fontSize,
height: fontSize,
width: fontSize,
} : undefined,
}
wrapperData.class['v-icon--svg'] = true
this.applyColors(wrapperData)
const svgData: VNodeData = {
attrs: {
xmlns: 'http://www.w3.org/2000/svg',
viewBox: '0 0 24 24',
height: fontSize || '32',
width: fontSize || '32',
role: 'img',
'aria-hidden': !this.attrs$['aria-label'],
'aria-label': this.attrs$['aria-label'],
},
}
return h('span', wrapperData, [
h('svg', svgData, [
h('path', {
attrs: {
d: icon,
},
}),
]),
])
},
renderSvgIconComponent (
icon: VuetifyIconComponent,
h: CreateElement
): VNode {
const data = this.getDefaultData()
data.class['v-icon--is-component'] = true
const size = this.getSize()
if (size) {
data.style = {
fontSize: size,
height: size,
}
}
this.applyColors(data)
const component = icon.component
data.props = icon.props
data.nativeOn = data.on
return h(component, data)
},
},
render (h: CreateElement): VNode {
const icon = this.getIcon()
if (typeof icon === 'string') {
if (isSvgPath(icon)) {
return this.renderSvgIcon(icon, h)
}
return this.renderFontIcon(icon, h)
}
return this.renderSvgIconComponent(icon, h)
},
})
export default Vue.extend({
name: 'v-icon',
$_wrapperFor: VIcon,
functional: true,
render (h, { data, children }): VNode {
let iconName = ''
// Support usage of v-text and v-html
if (data.domProps) {
iconName = data.domProps.textContent ||
data.domProps.innerHTML ||
iconName
// Remove nodes so it doesn't
// overwrite our changes
delete data.domProps.textContent
delete data.domProps.innerHTML
}
return h(VIcon, data, iconName ? [iconName] : children)
},
})