<template>
  <i
    v-if="unicodeIcon"
    class="erpIconfont erp unicode-icon"
    v-html="unicodeIcon"
  ></i>
  <svg v-else class="svg-icon erp" aria-hidden="true">
    <slot name="title"></slot>
    <use :xlink:href="svgIcon"></use>
  </svg>
</template>
<script>
export default {
  name: 'Icon',
  props: {
    name: {
      type: String,
      required: true,
    },
  },
  computed: {
    unicodeIcon() {
      if (this.name.match(/^x[a-f0-9]{4}$/)) {
        return `&#${this.name};`
      }
      return undefined
    },
    svgIcon() {
      if (this.unicodeIcon) return undefined
      return `#erp-${this.name}`
    },
  },
}
</script>
<style>
.unicode-icon {
  width: 1em;
  height: 1em;
  color: inherit;
}
.svg-icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}
</style>