2024-04-25 23:25:28 +08:00

63 lines
1.3 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<svg
aria-hidden="true"
:class="svgClass"
:style="{
color: color,
fill: color,
width: iconSize,
height: iconSize
}"
v-bind="$attrs"
>
<use :xlink:href="iconName" :fill="color" />
</svg>
</template>
<script lang="ts" setup>
import { computed } from "vue";
defineOptions({ name: "SvgIcon" });
const props = defineProps({
name: {
type: String,
default: ""
},
color: {
type: String,
default: ""
},
size: {
type: [Number, String],
default: 15
}
});
// 判断传入的值是否带有单位如果没有就默认用px单位
const getUnitValue = (value: string | number): string | number => {
return /(px|em|rem|%)$/.test(value.toString()) ? value : value + "px";
};
// svg大小
const iconSize = computed<string | number>(() => {
return getUnitValue(props.size);
});
// svg名称-对应资源文件夹的svg名称
const iconName = computed<string>(() => `#icon-${props.name}`);
// svg动态类名
const svgClass = computed<string>(() => {
if (props.name) return `svg-icon icon-${props.name}`;
return "svg-icon";
});
</script>
<style lang="scss" scope>
.svg-icon {
width: auto;
height: auto;
vertical-align: middle;
flex-shrink: 0;
}
</style>