<script setup lang="ts">
import { computed, unref, ref, watch, nextTick } from 'vue'
import { ElIcon } from 'element-plus'
import { propTypes } from '@/utils/propTypes'
import Iconify from '@purge-icons/generated'
import { useDesign } from '@/hooks/web/useDesign'
const { getPrefixCls } = useDesign()
const prefixCls = getPrefixCls('icon')
const props = defineProps({
// icon name
icon: propTypes.string,
// icon color
color: propTypes.string,
// icon size
size: propTypes.number.def(16)
})
console.log(props, 'propspropspropspropspropsprops')
const elRef = ref<ElRef>(null)
const isLocal = computed(() => props.icon.startsWith('svg-icon:'))
const symbolId = computed(() => {
return unref(isLocal)
? `#icon-${props.icon.split('svg-icon:')[1]}`
: props.icon.replace('fa-', 'fa:')
})
console.log(isLocal.value, 'isLocal1111')
console.log(symbolId.value, 'symbolId1111')
const getIconifyStyle = computed(() => {
const { color, size } = props
return {
fontSize: `${size}px`,
color
}
})
const updateIcon = async (icon: string) => {
if (unref(isLocal)) return
const el = unref(elRef)
if (!el) return
await nextTick()
if (!icon) return
const svg = Iconify.renderSVG(icon, {})
if (svg) {
el.textContent = ''
el.appendChild(svg)
} else {
const span = document.createElement('span')
span.className = 'iconify'
span.dataset.icon = icon
el.textContent = ''
el.appendChild(span)
}
}
watch(
() => props.icon,
(icon: string) => {
consoel.log(icon, 'iconWatch')
updateIcon(icon)
}
)
</script>
<template>
<ElIcon :class="prefixCls" :size="size" :color="color">
<svg v-if="isLocal" aria-hidden="true">
<use :xlink:href="symbolId" />
</svg>
<span v-else ref="elRef" :class="$attrs.class" :style="getIconifyStyle">
<span class="iconify" :data-icon="symbolId"></span>
</span>
</ElIcon>
</template>
<style scoped>
.el-icon {
vertical-align: middle;
}
</style>
字体图标转换(芋道框架自带)
最新推荐文章于 2024-05-20 17:02:17 发布