index.vue 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // doc: https://panjiachen.github.io/vue-element-admin-site/feature/component/svg-icon.html#usage
  9. import { isExternal } from '@/utils/validate'
  10. export default {
  11. name: 'SvgIcon',
  12. props: {
  13. iconClass: {
  14. type: String,
  15. required: true
  16. },
  17. className: {
  18. type: String,
  19. default: ''
  20. }
  21. },
  22. computed: {
  23. isExternal() {
  24. return isExternal(this.iconClass)
  25. },
  26. iconName() {
  27. return `#icon-${this.iconClass}`
  28. },
  29. svgClass() {
  30. if (this.className) {
  31. return 'svg-icon ' + this.className
  32. } else {
  33. return 'svg-icon'
  34. }
  35. },
  36. styleExternalIcon() {
  37. return {
  38. mask: `url(${this.iconClass}) no-repeat 50% 50%`,
  39. '-webkit-mask': `url(${this.iconClass}) no-repeat 50% 50%`
  40. }
  41. }
  42. }
  43. }
  44. </script>
  45. <style scoped>
  46. .svg-icon {
  47. width: 1em;
  48. height: 1em;
  49. vertical-align: -0.15em;
  50. fill: currentColor;
  51. overflow: hidden;
  52. }
  53. .svg-external-icon {
  54. background-color: currentColor;
  55. mask-size: cover!important;
  56. display: inline-block;
  57. }
  58. </style>