<template>
  <el-icon v-if="elIcons[props.name]">
    <component :style="iconStyle" :is="props.name"></component>
  </el-icon>
  <svg v-else :class="svgClass" :style="iconStyle" aria-hidden="true">
    <use :xlink:href="symbolId" :fill="color" />
  </svg>
</template>

<script setup lang="ts" name="SvgIcon">
import * as Icons from '@element-plus/icons-vue'
import { CSSProperties } from 'vue'
const elIcons: { [key: string]: any } = Icons
interface SvgProps {
  name: string // 图标的名称 ==> 必传
  prefix?: string // 图标的前缀 ==> 非必传（默认为"icon"）
  iconStyle?: CSSProperties // 图标的样式 ==> 非必传
  color?: string // 图标的样式填充颜色 ==> 非必传
  className?: string // 图标的class ==> 非必传
}

const props = withDefaults(defineProps<SvgProps>(), {
  prefix: 'icon',
  iconStyle: () => ({})
})

const symbolId = computed(() => `#${props.prefix}-${props.name}`)
const svgClass = computed(() => {
  if (props.className) {
    return `svg-icon ${props.className}`
  }
  return 'svg-icon'
})
</script>

<style scope lang="scss">
.sub-el-icon,
.nav-icon {
  position: relative;
  display: inline-block;
  margin-right: 12px;
  font-size: 15px;
}
.svg-icon {
  position: relative;
  width: 1em;
  height: 1em;
  margin-right: 5px;
  color: var(--color);
  fill: currentColor;
}
</style>
