e-icon.vue 1.69 KB
Newer Older
Tippi.Rao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
<template>
  <i
    v-if="fontClass"
    :class="iconName"
    @click="click(iconName,$event)"
  />
  <svg
    v-else-if="svg"
    :class="svgClass"
    aria-hidden="true"
    @click="click(iconName,$event)"
  >
    <use :xlink:href="iconName" />
  </svg>
  <div
    v-else-if="isExternal"
    :style="styleExternalIcon"
    :class="className"
    class="icon external-icon"
    @click="click(iconName,$event)"
  />
</template>

<script>
import { isExternal } from '../utils/index'

export default {
  name: 'EIcon',
  props: {
    iconName: {
      type: String,
      required: true
    },
    className: {
      type: String,
      default: ''
    }
  },
  computed: {
    fontClass() {
      return this.iconName && this.iconName.trim().length > 2 && (!isExternal(this.iconName) && !this.iconName.startsWith('#'))
    },
    svg() {
      return this.iconName && this.iconName.trim().length > 2 && (!isExternal(this.iconName) && this.iconName.startsWith('#'))
    },
    isExternal() {
      return isExternal(this.iconName)
    },
    svgClass() {
      if (this.className) {
        return 'icon ' + this.className
      } else {
        return 'icon'
      }
    },
    styleExternalIcon() {
      return {
        'background-image': `url(${this.iconName})`,
        'background-repeat': 'no-repeat',
        'background-size': '100% 100%',
        '-moz-background-size': '100% 100%'
      }
    }
  },
  methods: {
    click(iconName, event) {
      if (event) event.preventDefault()
      this.$emit('click', iconName)
    }
  }
}
</script>

<style scoped>
.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}

.external-icon {
  display: inline-block;
}
</style>