Icon图标

图标是一幅小型矢量图,大小和颜色跟随周围的文字。该集合共有 848 个图案,大多提供三种风格,每个都是可按名称引入的组件;Icon 可用来绘制你自己的图案。

引入
import { StarOutlined } from 'svelte-ant-design/icons'; import { Icon } from 'svelte-ant-design';
链接
src/lib/Icon 问题反馈 组件 README

何时使用

  • 用图标让按钮、菜单项或状态更容易辨认。含义不明显时,请在旁边保留文字。
  • 只从 svelte-ant-design/icons 引入用到的图标:构建时会保留这些图案,并丢弃其余 848 个中的其他图案。
  • 自己的图案,或你自行从 @ant-design/icons-svg 取得的图案,请使用 Icon。

代码演示

no props: the size and the color of the text
font-size and color set on the parent
inside a line of text

Your settings are saved .

基本用法

图标的大小和颜色跟随周围的文字:在图标或其父元素上设置 font-size 与 color 即可。每个图案都是一个以图案名加风格命名的组件,从 svelte-ant-design/icons 引入。

StarOutlined, StarFilled, StarTwoTone
HeartOutlined, HeartFilled, HeartTwoTone

三种风格

大多数图案有三种风格:Outlined、Filled 和 TwoTone。风格即组件名的结尾部分。

spin: the switch turns it on and off
the loading icon turns by itself
rotate: 0, 90, 180 and 270 degrees
rotate and spin together

旋转与角度

spin 让图标每秒转一圈并持续旋转(用户开启减弱动效时更慢)。loading 图案会自动旋转,spin={false} 可将其停止。rotate 按角度旋转图案,两者可以同时使用。

no twoToneColor: the theme colors
twoToneColor="#eb2f96", twoToneColor="#52c41a": the second color is derived
twoToneColor as a pair: [primary, secondary]
setTwoToneColor: the default of every two-tone icon of the page that has no color of its own
getTwoToneColor(): var(--ads-icon-twotone-primary) / var(--ads-icon-twotone-secondary)

双色图标

twoToneColor 可传入一个颜色(第二种颜色取其色板中最浅的一档),也可传入一对颜色 [主色, 辅色]。不传时由主题令牌着色。setTwoToneColor 设置整页的默认值:所有未单独指定颜色的双色图标都会改变,其他示例中的也一样。

no label: an image named by the drawing (star)
label="Favourite": your own name, in your own language
label="": hidden from assistive technology, next to visible text
Notifications

无障碍名称

默认情况下,图标是一张以图案名(英文)命名的图像。label 可设置自己的名称;label="" 会将其对辅助技术隐藏,适用于紧挨着可见文字的图标。

in a Button with text: the icon is hidden, the text names the button
in an icon-only Button: the button is named, the icon is hidden
an icon with onclick, role="button", tabindex and keys
stars: 0

可点击的图标

图标本身不是按钮。可将它放进 Button 中并由按钮提供名称,或自行为图标设置 role="button"、tabindex 和键盘处理。外层元素的其他属性(如 onclick、style)会原样传入。

icon: a definition, with spin, rotate and label like any other
children: your own svg inside the icon

自定义图案

icon 接受一份定义,它是纯数据:名称、风格以及 svg 的节点。children 接受你自己的 svg。两者都与现成图标一样支持 spin、rotate、class 和 label。

component: a Svelte component that renders an svg (panda-svg.part.svelte, shown below the example)

自定义组件

component 接受一个渲染 <svg> 的 Svelte 组件;它会收到 width、height 和 fill,并绘制在同一个外层元素内。组件代码显示在示例之后。

IconFont type="icon-ring", type="icon-square"
extraCommonProps: style for every icon of the font

iconfont.cn 图标库

createFromIconfontCN 会加载 iconfont.cn 项目的雪碧图脚本,并返回带有 type 属性的图标组件。它会运行你所给地址上的脚本,所以只应使用自己的地址。此处的雪碧图在页面内生成,因此示例无需联网。

集合中的每个图案都以可直接引入的组件形式呈现,分为六组,顺序与 Ant Design 的列表一致:可同时显示全部风格,也可一次显示一种。点击图标即可复制其名称。品牌标志归各自所有者所有。个别图案在某些尺寸下会因像素网格而显得不平整(例如 12 px 的 ShareAltOutlined),这是图案本身的问题,而非组件。

API

现成的图标(如 StarOutlined)支持下表中的属性,但不包括 icon、component 和 children。

属性说明类型默认值
icon仅 Icon 有。单个图案的数据:{ name, theme, icon },其中 icon 是 svg 的节点,双色图案则是接收两种颜色的函数。可直接使用从 @ant-design/icons-svg 引入的定义。IconDefinition—
component仅 Icon 有。渲染 <svg> 的 Svelte 组件。它会收到 1em 的 width 和 height,以及 currentColor 的 fill。优先级高于 icon。Component<IconComponentProps>—
children仅 Icon 有。自定义内容(一个 <svg>),绘制在外层元素内。优先级高于 component 和 icon。Snippet—
spin让图标(其外层元素)每秒转一圈并持续旋转;在 prefers-reduced-motion 下更慢。loading 图案无需它也会旋转,spin={false} 可停止。booleanfalse
rotate图案旋转的角度。外层元素不会转动,因此 spin 与 rotate 可同时生效。非有限数字的值会被忽略。number—
twoToneColor双色图案的颜色:一个颜色(第二种取其色板最浅的一档)或一对颜色 [主色, 辅色]。非字面量颜色(如 var(--x))会保留默认的辅色。线框与实心图案会忽略它。string | [string, string]—
label无障碍名称。缺省时为图案名(英文,如 star);自定义图案没有名称,会被隐藏。label="" 会对辅助技术隐藏图标。传入的 aria-label 也视为名称。string—
class外层元素的附加 CSS 类名,与 ads-icon 及 ads-icon-<名称> 并存。string—
style外层元素的内联样式。图案是 1em 的正方形,所以 font-size 决定大小,color 决定颜色。string—
native attributes外层 <span> 的原生属性(onclick、tabindex、role、title、data-*、aria-*)会被转发。图标本身不会加入 Tab 顺序。HTMLAttributes<HTMLSpanElement>—
与 Ant Design 的差异:外层元素的类名是 ads-icon 而不是 anticon;减弱动效时 spin 更慢,spin={false} 可停止 loading 图案;没有 label 的自定义图案会被隐藏;双色图标的默认颜色跟随主题令牌。不采用通过网络加载的字符串名称方式(Angular)以及图标默认值的 Provider。图案及其名称来自 @ant-design/icons-svg(MIT),Ant Design 的 React 与 Vue 图标包同样使用这个数据包。

函数

函数说明类型
setTwoToneColor为所有未单独设置 twoToneColor 的双色图标设置颜色。已显示的图标会立即更新。请在启动时调用一次:服务端的该值被所有请求共享,所以它适合设置某个主题,而不是某个用户。(color: string | [string, string]) => void
getTwoToneColor当前的一对颜色 [主色, 辅色]。在调用 setTwoToneColor 之前,它是以 var(...) 字符串表示的两个主题令牌。返回的数组是副本。() => [string, string]
createFromIconfontCN加载 iconfont.cn 项目的雪碧图脚本(每个地址只加载一个 <script>,按列表顺序,因此同名时后者覆盖前者),并返回带有 type 属性的图标组件。extraCommonProps 会传给每个图标,图标自身的属性优先。服务端不加载任何内容。除非设置 label,图标没有名称。({ scriptUrl, extraCommonProps }) => Component

设计令牌

令牌说明默认值
--ads-icon-twotone-primary未单独设置颜色的双色图标的主色,跟随主题的主色。var(--ads-color-primary)
--ads-icon-twotone-secondary此类图标的浅色:主色的最浅一档。深色主题有独立的取值。#e6f4ff

无障碍

  • 每个图标都是 <span role="img">,以英文图案名命名(aria-label="star");内部的 svg 对辅助技术隐藏。
  • 在使用其他语言的网站上,英文名称并不合适。请用 label 提供自己的文字;紧挨着可见文字的图标可设为 label="" 将其隐藏,此时它既没有角色也没有名称。
  • 自定义图案(component 或 children)没有名称,因此在设置 label 之前它是隐藏的。
  • 图标不能获得焦点,也不在 Tab 顺序中。需要点击时,请使用 Button 并由它提供名称,或自行添加 role="button"、tabindex 以及 Enter 和 Space 键处理。
  • 双色图标的浅色部分只是装饰。按设计,它在白色页面上的对比度远低于 3:1,因此切勿让它单独承载含义。
  • 旋转不会被朗读;请像 Spin 那样用文字说明正在加载。在 prefers-reduced-motion 下旋转更慢(2.5 秒),这一点 Ant Design 没有。
  • 已用真实读屏软件(Orca 搭配 Firefox)验证:它把图案读作其名称加「image」;设置 label 时读该文字,label="" 时什么都不读;内部图标被隐藏时,按钮按其文字或自身名称读出;带 role="button" 的图标读作带该标签的按钮。未验证:NVDA、JAWS、VoiceOver 和手机读屏软件。