<template>
  <component
    :is="iconComponent"
    class="svg-icon"
    :class="className"
    :style="{
      fontSize: size + 'px',
      color: color,
      '--svg-color': color
    }"
    v-bind="$attrs"
  />
</template>

<script setup>
import { computed, defineAsyncComponent } from 'vue'

const props = defineProps({
  name: {
    type: String,
    required: true,
    // 例如: 'arrow', 'heart'
  },
  size: {
    type: [Number, String],
    default: 14,
  },
  color: {
    type: String,
    default: 'currentColor',
  },
  className: {
    type: String,
    default: '',
  },
})


// 动态导入 SVG 组件
const iconComponent = computed(() => {
  // Vite 在编译时处理这个动态导入
  return defineAsyncComponent(() =>
    import(`@/static/svgs/${props.name}.svg`)
  )
})
</script>

<style scoped>
.svg-icon {
  display: inline-block;
  vertical-align: middle;
  flex-shrink: 0;
  line-height: 1;
  width: 1em;
  height: 1em;
}

/* color prop → currentColor，子元素继承 */
.svg-icon :deep(*:not([fill="none"])) {
  fill: currentColor !important;
}

.svg-icon :deep([stroke]) {
  stroke: currentColor !important;
}
</style>