Skip to content

Commit f032d05

Browse files
authored
fix: add computed getters and fix props reactivity (#14)
1 parent e1815eb commit f032d05

File tree

2 files changed

+56
-39
lines changed

2 files changed

+56
-39
lines changed

Diff for: lib.d.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ import { AllowedComponentProps, ComponentCustomProps, VNodeProps } from "vue";
22

33
export interface IcomoonProps {
44
iconSet: Record<any, any>;
5-
icon: string;
5+
icon?: string;
6+
name: string;
67
color?: string;
7-
size?: number;
8+
size?: number | string;
89
disableFill?: boolean;
910
removeInitialStyle?: boolean;
1011
style?: Record<any, any>;
1112
}
1213

1314
export declare const Icomoon: {
1415
new (): {
15-
$props: AllowedComponentProps & ComponentCustomProps & VNodeProps & IcomoonProps;
16+
$props: AllowedComponentProps &
17+
ComponentCustomProps &
18+
VNodeProps &
19+
IcomoonProps;
1620
};
1721
};
1822

Diff for: src/components/Icomoon.vue

+49-36
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<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+
>
38
<path
49
v-for="({ d, ...attrs }, index) in paths"
510
:d="d"
@@ -11,6 +16,8 @@
1116
</template>
1217

1318
<script>
19+
import { computed, ref, toRefs, watchEffect } from "vue";
20+
1421
export default {
1522
name: "Icomoon",
1623
props: {
@@ -51,62 +58,68 @@ export default {
5158
default: {},
5259
},
5360
},
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+
6472
const initialStyle = {
6573
display: "inline-block",
6674
stroke: "currentColor",
6775
fill: "currentColor",
6876
};
6977
70-
const iconName = icon || name;
78+
const iconName = computed(() => icon.value || name.value);
7179
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+
)
7484
);
7585
76-
if (!currentIcon) return {};
77-
78-
const { width = "1024" } = currentIcon.icon;
86+
if (!currentIcon.value) return {};
7987
80-
const viewBox = `0 0 ${width} 1024`;
88+
const viewBox = computed(
89+
() => `0 0 ${currentIcon.value.icon.width || "1024"} 1024`
90+
);
8191
82-
const style = {
83-
...(removeInitialStyle ? {} : initialStyle),
84-
};
92+
const style = ref({
93+
...(removeInitialStyle.value ? {} : initialStyle),
94+
});
8595
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+
});
90102
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;
95108
96-
const pathProps = {
97-
d: path,
98-
...(!disableFill && attrs ? attrs : {}),
99-
};
109+
const pathProps = {
110+
d: path,
111+
...(!disableFill.value && attrs ? attrs : {}),
112+
};
100113
101-
return pathProps;
102-
});
114+
return pathProps;
115+
})
116+
);
103117
104118
return {
105119
currentIcon,
106120
viewBox,
107121
style,
108122
paths,
109-
svgProps,
110123
};
111124
},
112125
};

0 commit comments

Comments
 (0)