index.vue 1.3 KB

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