Skip to content

Commit aba556b

Browse files
committed
refactor: remove code duplication in CIcon component
1 parent ae14b3b commit aba556b

File tree

1 file changed

+2
-69
lines changed

1 file changed

+2
-69
lines changed

src/CIcon.vue

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,8 @@
1-
<template>
2-
<svg
3-
v-if="!src"
4-
xmlns="http://www.w3.org/2000/svg"
5-
:viewBox="viewBox"
6-
:class="computedClasses"
7-
v-html="titleCode + iconCode"
8-
role="img"
9-
></svg>
10-
<img
11-
v-else
12-
:src="src"
13-
role="img"
14-
/>
15-
</template>
16-
171
<script>
2+
import CIconRaw from './CIconRaw'
183
export default {
194
name: 'CIcon',
20-
props: {
21-
name: String,
22-
content: [String, Array],
23-
size: {
24-
type: String,
25-
validator: size => [
26-
'custom-size', 'sm', 'lg', 'xl',
27-
'2xl', '3xl', '4xl', '5xl', '6xl', '7xl', '8xl', '9xl'
28-
].includes(size)
29-
},
30-
customClasses: [String, Array, Object],
31-
src: String,
32-
title: String
33-
},
34-
computed: {
35-
iconName () {
36-
const iconNameIsKebabCase = this.name && this.name.includes('-')
37-
return iconNameIsKebabCase ? this.toCamelCase(this.name) : this.name
38-
},
39-
titleCode () {
40-
return this.title ? `<title>${this.title}</title>` : ''
41-
},
42-
code () {
43-
if (this.content) {
44-
return this.content
45-
} else if (this.$root.$options.icons) {
46-
return this.$root.$options.icons[this.iconName]
47-
}
48-
},
49-
iconCode () {
50-
return Array.isArray(this.code) ? this.code[1] || this.code[0] : this.code
51-
},
52-
scale () {
53-
return Array.isArray(this.code) && this.code.length > 1 ? this.code[0] : '64 64'
54-
},
55-
viewBox () {
56-
return this.$attrs.viewBox || `0 0 ${this.scale}`
57-
},
58-
computedSize () {
59-
return this.$attrs.width || this.$attrs.height ? 'custom-size' : this.size
60-
},
61-
computedClasses () {
62-
return this.customClasses ||
63-
['c-icon', { [`c-icon-${this.computedSize}`]: this.computedSize }]
64-
}
65-
},
66-
methods: {
67-
toCamelCase (str) {
68-
return str.replace(/([-_][a-z0-9])/ig, ($1) => {
69-
return $1.toUpperCase().replace('-', '')
70-
})
71-
}
72-
}
5+
extends: CIconRaw
736
}
747
</script>
758

0 commit comments

Comments
 (0)