|
1 | 1 | <template>
|
2 |
| - <svg v-if="currentIcon" :viewBox="viewBox" :style="style" v-bind="svgProps"> |
| 2 | + <svg |
| 3 | + v-if="currentIcon" |
| 4 | + :viewBox="viewBox" |
| 5 | + :style="style" |
| 6 | + v-bind="{ style, color }" |
| 7 | + > |
3 | 8 | <path
|
4 | 9 | v-for="({ d, ...attrs }, index) in paths"
|
5 | 10 | :d="d"
|
|
11 | 16 | </template>
|
12 | 17 |
|
13 | 18 | <script>
|
| 19 | +import { computed, ref, toRefs, watchEffect } from "vue"; |
| 20 | +
|
14 | 21 | export default {
|
15 | 22 | name: "Icomoon",
|
16 | 23 | props: {
|
@@ -51,62 +58,68 @@ export default {
|
51 | 58 | default: {},
|
52 | 59 | },
|
53 | 60 | },
|
54 |
| - setup({ |
55 |
| - iconSet, |
56 |
| - icon, |
57 |
| - name, |
58 |
| - size, |
59 |
| - title, |
60 |
| - disableFill, |
61 |
| - removeInitialStyle, |
62 |
| - ...svgProps |
63 |
| - }) { |
| 61 | + setup(props) { |
| 62 | + const { |
| 63 | + iconSet, |
| 64 | + icon, |
| 65 | + name, |
| 66 | + size, |
| 67 | + title, |
| 68 | + disableFill, |
| 69 | + removeInitialStyle, |
| 70 | + } = toRefs(props); |
| 71 | +
|
64 | 72 | const initialStyle = {
|
65 | 73 | display: "inline-block",
|
66 | 74 | stroke: "currentColor",
|
67 | 75 | fill: "currentColor",
|
68 | 76 | };
|
69 | 77 |
|
70 |
| - const iconName = icon || name; |
| 78 | + const iconName = computed(() => icon.value || name.value); |
71 | 79 |
|
72 |
| - const currentIcon = iconSet.icons.find( |
73 |
| - (item) => item.properties.name === iconName |
| 80 | + const currentIcon = computed(() => |
| 81 | + iconSet.value.icons.find( |
| 82 | + (item) => item.properties.name === iconName.value |
| 83 | + ) |
74 | 84 | );
|
75 | 85 |
|
76 |
| - if (!currentIcon) return {}; |
77 |
| -
|
78 |
| - const { width = "1024" } = currentIcon.icon; |
| 86 | + if (!currentIcon.value) return {}; |
79 | 87 |
|
80 |
| - const viewBox = `0 0 ${width} 1024`; |
| 88 | + const viewBox = computed( |
| 89 | + () => `0 0 ${currentIcon.value.icon.width || "1024"} 1024` |
| 90 | + ); |
81 | 91 |
|
82 |
| - const style = { |
83 |
| - ...(removeInitialStyle ? {} : initialStyle), |
84 |
| - }; |
| 92 | + const style = ref({ |
| 93 | + ...(removeInitialStyle.value ? {} : initialStyle), |
| 94 | + }); |
85 | 95 |
|
86 |
| - if (size) { |
87 |
| - style.width = size; |
88 |
| - style.height = size; |
89 |
| - } |
| 96 | + watchEffect(() => { |
| 97 | + if (size.value) { |
| 98 | + style.value.width = size.value; |
| 99 | + style.value.height = size.value; |
| 100 | + } |
| 101 | + }); |
90 | 102 |
|
91 |
| - const paths = currentIcon.icon.paths.map((path, index) => { |
92 |
| - const attrs = currentIcon.icon.attrs |
93 |
| - ? currentIcon.icon.attrs[index] |
94 |
| - : null; |
| 103 | + const paths = computed(() => |
| 104 | + currentIcon.value.icon.paths.map((path, index) => { |
| 105 | + const attrs = currentIcon.value.icon.attrs |
| 106 | + ? currentIcon.value.icon.attrs[index] |
| 107 | + : null; |
95 | 108 |
|
96 |
| - const pathProps = { |
97 |
| - d: path, |
98 |
| - ...(!disableFill && attrs ? attrs : {}), |
99 |
| - }; |
| 109 | + const pathProps = { |
| 110 | + d: path, |
| 111 | + ...(!disableFill.value && attrs ? attrs : {}), |
| 112 | + }; |
100 | 113 |
|
101 |
| - return pathProps; |
102 |
| - }); |
| 114 | + return pathProps; |
| 115 | + }) |
| 116 | + ); |
103 | 117 |
|
104 | 118 | return {
|
105 | 119 | currentIcon,
|
106 | 120 | viewBox,
|
107 | 121 | style,
|
108 | 122 | paths,
|
109 |
| - svgProps, |
110 | 123 | };
|
111 | 124 | },
|
112 | 125 | };
|
|
0 commit comments