index.vue 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <template>
  2. <img
  3. v-if="isExternal"
  4. :src="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. import { isExternal } from '@/utils/validate'
  14. export default {
  15. name: 'VabColorfulIcon',
  16. props: {
  17. iconClass: {
  18. type: String,
  19. required: true,
  20. },
  21. className: {
  22. type: String,
  23. default: '',
  24. },
  25. },
  26. computed: {
  27. isExternal() {
  28. return isExternal(this.iconClass)
  29. },
  30. iconName() {
  31. return `#colorful-icon-${this.iconClass}`
  32. },
  33. svgClass() {
  34. if (this.className) {
  35. return 'svg-icon ' + this.className
  36. } else {
  37. return 'svg-icon'
  38. }
  39. },
  40. styleExternalIcon() {
  41. return this.iconClass
  42. },
  43. },
  44. }
  45. </script>
  46. <style lang="scss" scoped>
  47. .svg-icon {
  48. width: 1em;
  49. height: 1em;
  50. overflow: hidden;
  51. vertical-align: -0.15em;
  52. fill: currentColor;
  53. &:hover {
  54. opacity: 0.8;
  55. }
  56. }
  57. .svg-external-icon {
  58. display: inline-block;
  59. }
  60. </style>