2022-06-16 01:17:31 +08:00
|
|
|
|
<template>
|
2022-09-23 00:15:00 +08:00
|
|
|
|
<template v-if="renderLocalIcon">
|
|
|
|
|
<svg aria-hidden="true" width="1em" height="1em" v-bind="bindAttrs">
|
|
|
|
|
<use :xlink:href="symbolId" fill="currentColor" />
|
|
|
|
|
</svg>
|
|
|
|
|
</template>
|
|
|
|
|
<template v-else>
|
2022-12-07 01:11:45 +08:00
|
|
|
|
<Icon v-if="icon" :icon="icon" v-bind="bindAttrs" />
|
2022-09-23 00:15:00 +08:00
|
|
|
|
</template>
|
2022-06-16 01:17:31 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2022-09-23 00:15:00 +08:00
|
|
|
|
import { computed, useAttrs } from 'vue';
|
|
|
|
|
import { Icon } from '@iconify/vue';
|
2022-06-16 01:17:31 +08:00
|
|
|
|
|
2022-07-10 14:02:00 +08:00
|
|
|
|
defineOptions({ name: 'SvgIcon' });
|
|
|
|
|
|
2022-09-23 00:15:00 +08:00
|
|
|
|
/**
|
|
|
|
|
* 图标组件
|
|
|
|
|
* - 支持iconify和本地svg图标
|
|
|
|
|
* - 同时传递了icon和localIcon,localIcon会优先渲染
|
|
|
|
|
*/
|
2022-06-16 01:17:31 +08:00
|
|
|
|
interface Props {
|
2022-09-23 00:15:00 +08:00
|
|
|
|
/** 图标名称 */
|
|
|
|
|
icon?: string;
|
|
|
|
|
/** 本地svg的文件名 */
|
|
|
|
|
localIcon?: string;
|
2022-06-16 01:17:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-23 00:15:00 +08:00
|
|
|
|
const props = defineProps<Props>();
|
|
|
|
|
|
|
|
|
|
const attrs = useAttrs();
|
|
|
|
|
|
|
|
|
|
const bindAttrs = computed<{ class: string; style: string }>(() => ({
|
|
|
|
|
class: (attrs.class as string) || '',
|
|
|
|
|
style: (attrs.style as string) || ''
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
const symbolId = computed(() => {
|
|
|
|
|
const { VITE_ICON_LOCAL_PREFFIX: preffix } = import.meta.env;
|
|
|
|
|
|
|
|
|
|
const defaultLocalIcon = 'no-icon';
|
|
|
|
|
|
|
|
|
|
const icon = props.localIcon || defaultLocalIcon;
|
|
|
|
|
|
|
|
|
|
return `#${preffix}-${icon}`;
|
2022-06-16 01:17:31 +08:00
|
|
|
|
});
|
|
|
|
|
|
2022-09-23 00:15:00 +08:00
|
|
|
|
/** 渲染本地icon */
|
|
|
|
|
const renderLocalIcon = computed(() => props.localIcon || !props.icon);
|
2022-06-16 01:17:31 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style scoped></style>
|