index.vue 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <div v-if="isExternal" :style="styleExternalIcon" class="svg-external-icon svg-icon" v-on="$listeners" />
  3. <svg v-else :class="svgClass" aria-hidden="true" v-on="$listeners">
  4. <use :xlink:href="iconName" />
  5. </svg>
  6. </template>
  7. <script>
  8. import { isExternal } from '@/utils/validate'
  9. export default {
  10. name: 'SvgIcon',
  11. props: {
  12. iconClass: {
  13. type: String,
  14. required: true
  15. },
  16. className: {
  17. type: String,
  18. default: ''
  19. }
  20. },
  21. computed: {
  22. isExternal() {
  23. return isExternal(this.iconClass)
  24. },
  25. iconName() {
  26. return `#icon-${this.iconClass}`
  27. },
  28. svgClass() {
  29. if (this.className) {
  30. return 'svg-icon ' + this.className
  31. } else {
  32. return 'svg-icon'
  33. }
  34. },
  35. styleExternalIcon() {
  36. return {
  37. mask: `url(${this.iconClass}) no-repeat 50% 50%`,
  38. '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
  39. }
  40. }
  41. }
  42. }
  43. </script>
  44. <style scoped>
  45. .svg-icon {
  46. width: 1em;
  47. height: 1em;
  48. vertical-align: -0.15em;
  49. fill: currentColor;
  50. overflow: hidden;
  51. }
  52. .svg-external-icon {
  53. background-color: currentColor;
  54. mask-size: cover!important;
  55. display: inline-block;
  56. }
  57. </style>