index.vue 1.3 KB

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