index.vue 1.3 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. // 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. }
  33. return 'svg-icon';
  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. overflow: hidden;
  49. vertical-align: -0.15em;
  50. fill: currentColor;
  51. }
  52. .svg-external-icon {
  53. display: inline-block;
  54. background-color: currentColor;
  55. mask-size: cover !important;
  56. }
  57. </style>