svgIcon.vue 856 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <template>
  2. <svg :class="svgClass" :aria-hidden="true" :style="{ color: color, width: computedWidth, height: computedWidth }">
  3. <use :xlink:href="iconName" :fill="color" />
  4. </svg>
  5. </template>
  6. <script setup>
  7. import { defineProps, computed } from 'vue'
  8. const props = defineProps({
  9. name: {
  10. type: String,
  11. required: true
  12. },
  13. color: {
  14. type: String,
  15. default: ''
  16. },
  17. size: {
  18. type: Number,
  19. },
  20. })
  21. const iconName = computed(() => `#icon-${props.name}`)
  22. const svgClass = computed(() => {
  23. if (props.name) {
  24. return `svg-icon icon-${props.name}`
  25. }
  26. return 'svg-icon'
  27. })
  28. const computedWidth = computed(() => {
  29. const result = props.width || props.size
  30. return result ? result + 'px' : '1em'
  31. })
  32. </script>
  33. <style lang='scss'>
  34. .svg-icon {
  35. width: 1em;
  36. height: 1em;
  37. fill: currentColor;
  38. vertical-align: middle;
  39. }
  40. </style>