You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/v2/cookbook/editable-svg-icons.md
+35-35
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,30 @@
1
1
---
2
-
title: Editable SVG Icon Systems
2
+
title: 可编辑的 SVG 图标系统
3
3
type: cookbook
4
4
order: 4
5
5
---
6
6
7
-
## Base Example
7
+
## 简单的示例
8
8
9
-
There are many ways to create an SVG Icon System, but one method that takes advantage of Vue's capabilities is to create editable inline icons as components. Some of the advantages of this way of working is:
*You can use standard props and defaults to keep them to a typical size or alter them if you need to
14
-
*They are inline, so no HTTP requests are necessary
15
-
*They can be made accessible dynamically
11
+
*图标易于实时修改
12
+
*图标可以带动画
13
+
*你可以使用标准的 prop 和默认值来将图标保持在一个典型的尺寸并随时按需改变它们
14
+
*图标是内联的,所以不需要额外的 HTTP 请求
15
+
*可以动态地使得图标可访问
16
16
17
-
First, we'll create a folder for all of the icons, and name them in a standardized fashion for easy retrieval:
17
+
首先,我们将为所有的图标创建一个文件夹,并将这些图标以一种标准化的方式命名以便获取它们:
18
18
19
-
> components/icons/IconBox.vue
20
-
> components/icons/IconCalendar.vue
21
-
> components/icons/IconEnvelope.vue
19
+
> `components/icons/IconBox.vue`
20
+
> `components/icons/IconCalendar.vue`
21
+
> `components/icons/IconEnvelope.vue`
22
22
23
-
Here's an example repo to get you going, where you can see the entire setup: [https://github.com/sdras/vue-sample-svg-icons/](https://github.com/sdras/vue-sample-svg-icons/)
We'll create a base icon (`IconBase.vue`) component that uses a scoped slot.
27
+
我们将会创建一个基础图标 (`IconBase.vue`) 组件,并使用了一个无作用域的插槽。
28
28
29
29
```html
30
30
<template>
@@ -43,9 +43,9 @@ We'll create a base icon (`IconBase.vue`) component that uses a scoped slot.
43
43
</template>
44
44
```
45
45
46
-
You can use this base icon as is- the only thing you might need to update is the `viewBox`depending on the `viewBox` of your icons. In the base, we're making the `width`, `height`, `color`, and name of the icon props so that it can be dynamically updated with props. The name will be used for both the title`id` for accessibility, and the `title`.
Our script will look like this, we'll have some defaults so that our icon will be rendered consistently unless we state otherwise:
48
+
我们的脚本是下面这样的,我们设了一些默认值,这样在没有特别设置的情况下图标渲染出来就是一致的:
49
49
50
50
```js
51
51
exportdefault {
@@ -70,32 +70,32 @@ export default {
70
70
}
71
71
```
72
72
73
-
The currentColor property that's the default on the fill will make the icon inherit the color of whatever text surrounds it. We could also pass in a different color as a prop if we wish.
Keeping icons in components comes in very handy when you'd like to animate them, especially on an interaction. Inline SVGs have the highest support for interaction of any method. Here's a very basic example of an icon that's animated on click:
We're applying `refs` to the groups of paths we need to move, and as both sides of the scissors have to move in tandem, we'll create a function we can reuse where we'll pass in the `refs`. The use of GreenSock helps resolve animation support and transform-origin issues across browser.
Designers may change their minds. Product requirements change. Keeping the logic for the entire icon system in one base component means you can quickly update all of your icons and have it propagate through the whole system. Even with the use of an icon loader, some situations require you to recreate or edit every SVG to make global changes. This method can save you that time and pain.
This type of SVG icon system is really useful when you have a number of icons that are used in different ways throughout your site. If you're repeating the same icon many times on one page (e.g. a giant table a delete icon in each row), it might make more sense to have all of the sprites compiled into a sprite sheet and use `use` tags to load them.
These tools bundle SVGs at compile time, but make them a little harder to edit during runtime, because `use` tags can have strange cross-browser issues when doing anything more complex. They also leave you with two nested `viewBox`properties and thus two coordinate systems. This makes the implementation a little more complex.
0 commit comments