소스 검색

feat[SvgIcon]: support import svg from url

Pan 5 년 전
부모
커밋
79eeed7d59
1개의 변경된 파일20개의 추가작업 그리고 1개의 파일을 삭제
  1. 20 1
      src/components/SvgIcon/index.vue

+ 20 - 1
src/components/SvgIcon/index.vue

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