2022-06-16 01:17:31 +08:00
|
|
|
<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
|
|
|
|
2024-04-09 07:27:48 +08:00
|
|
|
defineOptions({ name: 'SvgIcon', inheritAttrs: false });
|
2022-07-10 14:02:00 +08:00
|
|
|
|
2022-09-23 00:15:00 +08:00
|
|
|
/**
|
2023-12-14 21:45:29 +08:00
|
|
|
* Props
|
|
|
|
*
|
|
|
|
* - Support iconify and local svg icon
|
|
|
|
* - If icon and localIcon are passed at the same time, localIcon will be rendered first
|
2022-09-23 00:15:00 +08:00
|
|
|
*/
|
2022-06-16 01:17:31 +08:00
|
|
|
interface Props {
|
2023-12-14 21:45:29 +08:00
|
|
|
/** Iconify icon name */
|
2022-09-23 00:15:00 +08:00
|
|
|
icon?: string;
|
2023-12-14 21:45:29 +08:00
|
|
|
/** Local svg icon name */
|
2022-09-23 00:15:00 +08:00
|
|
|
localIcon?: string;
|
2022-06-16 01:17:31 +08:00
|
|
|
}
|
|
|
|
|
2024-01-25 23:24:35 +08:00
|
|
|
const props = defineProps<Props>();
|
|
|
|
|
2022-09-23 00:15:00 +08:00
|
|
|
const attrs = useAttrs();
|
|
|
|
|
|
|
|
const bindAttrs = computed<{ class: string; style: string }>(() => ({
|
|
|
|
class: (attrs.class as string) || '',
|
|
|
|
style: (attrs.style as string) || ''
|
|
|
|
}));
|
|
|
|
|
|
|
|
const symbolId = computed(() => {
|
2023-08-03 23:59:00 +08:00
|
|
|
const { VITE_ICON_LOCAL_PREFIX: prefix } = import.meta.env;
|
2022-09-23 00:15:00 +08:00
|
|
|
|
|
|
|
const defaultLocalIcon = 'no-icon';
|
|
|
|
|
|
|
|
const icon = props.localIcon || defaultLocalIcon;
|
|
|
|
|
2023-08-03 23:59:00 +08:00
|
|
|
return `#${prefix}-${icon}`;
|
2022-06-16 01:17:31 +08:00
|
|
|
});
|
|
|
|
|
2023-12-14 21:45:29 +08:00
|
|
|
/** If localIcon is passed, render localIcon first */
|
2022-09-23 00:15:00 +08:00
|
|
|
const renderLocalIcon = computed(() => props.localIcon || !props.icon);
|
2022-06-16 01:17:31 +08:00
|
|
|
</script>
|
|
|
|
|
2023-11-17 08:45:00 +08:00
|
|
|
<template>
|
|
|
|
<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>
|
|
|
|
<Icon v-if="icon" :icon="icon" v-bind="bindAttrs" />
|
|
|
|
</template>
|
|
|
|
</template>
|
|
|
|
|
2022-06-16 01:17:31 +08:00
|
|
|
<style scoped></style>
|