<!--
 * @Author: 曹捷
 * @Date: 2022-01-06 11:10:12
 * @LastEditors: 曹捷
 * @LastEditTime: 2022-01-08 11:35:06
 * @Description: fileContent
-->
<template>
  <svg :class="svgClass" :style="svgStyle" @click="svgClick" aria-hidden="true">
    <use :xlink:href="iconName" />
  </svg>
</template>

<script>
import { computed } from "vue";
export default {
  name: 'svgIcon',
  emits: ['click'],
  props: {
    iconClass: {
      type: String,
      required: true
    },
    className: {
      type: String,
      default: ''
    },
    size: {
      type: [String, Number],
      default: 1
    }
  },
  setup (props, context) {
    let _methods = {
      svgClick () {
        context.emit('click')
      }
    }
    let _computed = {
      svgStyle: computed(() => {
        let style = {}
        if (props.size) {
          style.fontSize = `${props.size}em`
        }
        return style
      }),
      iconName: computed(() => {
        return `#icon-${props.iconClass}`
      }),
      svgClass: computed(() => {
        if (props.className) {
          return 'svg-icon ' + props.className
        } else {
          return 'svg-icon'
        }
      }),
      styleExternalIcon: computed(() => {
        return {
          mask: `url(${props.iconClass}) no-repeat 50% 50%`,
          '-webkit-mask': `url(${props.iconClass}) no-repeat 50% 50%`
        }
      })
    }
    return {
      ..._methods,
      ..._computed,
    };
  }

}
</script>

<style >
.svg-icon {
  width: 1.2em;
  height: 1.2em;
  vertical-align: -0.18em;
  fill: currentColor;
  overflow: hidden;
}

.svg-external-icon {
  background-color: currentColor;
  mask-size: cover !important;
  display: inline-block;
}
</style>
