Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/vue-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ export function svg({ name = 'Icon', component, filledComponent = null, deprecat
context,
extend
})
},
mounted() {
// 如果属性没有主副色,则遍历svg的所有内部元素,移除 fill 属性。
// 注意:移除后不能还原,所以: 如果用户使用时不传入主副色,后来修改主副色有值,也无法再把fill还原回来的。
if (this.firstColor === '' && this.secondColor === '') {
this.$el?.querySelectorAll('*').forEach((path) => path.removeAttribute('fill'))
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Non-reactive behavior may break user expectations.

The mounted hook runs only once. If firstColor or secondColor props change from empty to non-empty values after mount, the fill attributes won't be restored. This breaks Vue's reactivity model and could confuse users who expect props to work reactively.

Consider using a watcher or computed property to handle this reactively:

-        },
-        mounted() {
-          // 如果属性没有主副色,则遍历svg的所有内部元素,移除 fill 属性。
-          // 注意:移除后不能还原,所以:  如果用户使用时不传入主副色,后来修改主副色有值,也无法再把fill还原回来的。
-          if (this.firstColor === '' && this.secondColor === '') {
-            this.$el?.querySelectorAll('*').forEach((path) => path.removeAttribute('fill'))
-          }
+        },
+        watch: {
+          firstColor: {
+            handler(newVal, oldVal) {
+              if (newVal === '' && this.secondColor === '') {
+                this.$el?.querySelectorAll('path, circle, rect, polygon, ellipse, line, polyline').forEach((el) => el.removeAttribute('fill'))
+              }
+            },
+            immediate: false
+          },
+          secondColor: {
+            handler(newVal, oldVal) {
+              if (newVal === '' && this.firstColor === '') {
+                this.$el?.querySelectorAll('path, circle, rect, polygon, ellipse, line, polyline').forEach((el) => el.removeAttribute('fill'))
+              }
+            },
+            immediate: false
+          }
+        },
+        mounted() {
+          // Remove fill attributes on initial mount if no colors specified
+          if (this.firstColor === '' && this.secondColor === '') {
+            this.$el?.querySelectorAll('path, circle, rect, polygon, ellipse, line, polyline').forEach((el) => el.removeAttribute('fill'))
+          }
         }

Alternatively, if the one-way behavior is intentional to match legacy icon behavior, document this clearly in the component's public API.

Committable suggestion skipped: line range outside the PR's diff.

})
)
Expand Down
Loading