Icon图标
图标是一幅小型矢量图,大小和颜色跟随周围的文字。该集合共有 848 个图案,大多提供三种风格,每个都是可按名称引入的组件;Icon 可用来绘制你自己的图案。
何时使用
- 用图标让按钮、菜单项或状态更容易辨认。含义不明显时,请在旁边保留文字。
- 只从
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 .
<script lang="ts">
import { HomeOutlined, SettingOutlined, SmileOutlined } from 'svelte-ant-design/icons';
</script>
<div class="stack">
<div class="field">
<span class="caption">no props: the size and the color of the text</span>
<div class="row">
<HomeOutlined />
<SettingOutlined />
<SmileOutlined />
</div>
</div>
<div class="field">
<span class="caption">font-size and color set on the parent</span>
<div class="row big">
<HomeOutlined />
<SettingOutlined />
<SmileOutlined />
</div>
</div>
<div class="field">
<span class="caption">inside a line of text</span>
<p class="line">Your settings <SettingOutlined /> are saved <SmileOutlined />.</p>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
}
.big {
color: var(--ads-color-primary);
font-size: 28px;
}
.line {
margin: 0;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
StarOutlined, StarFilled, StarTwoTone
HeartOutlined, HeartFilled, HeartTwoTone
<script lang="ts">
import { HeartFilled, HeartOutlined, HeartTwoTone, StarFilled, StarOutlined, StarTwoTone } from 'svelte-ant-design/icons';
</script>
<div class="stack">
<div class="field">
<span class="caption">StarOutlined, StarFilled, StarTwoTone</span>
<div class="row">
<StarOutlined />
<StarFilled />
<StarTwoTone />
</div>
</div>
<div class="field">
<span class="caption">HeartOutlined, HeartFilled, HeartTwoTone</span>
<div class="row">
<HeartOutlined />
<HeartFilled />
<HeartTwoTone />
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
font-size: 28px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
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
<script lang="ts">
import { Switch } from 'svelte-ant-design';
import { ArrowUpOutlined, LoadingOutlined, RocketOutlined, SyncOutlined } from 'svelte-ant-design/icons';
let spinning = $state(true);
</script>
<div class="stack">
<div class="field">
<span class="caption">spin: the switch turns it on and off</span>
<div class="row">
<Switch bind:checked={spinning} aria-label="Spin" />
<SyncOutlined spin={spinning} />
</div>
</div>
<div class="field">
<span class="caption">the loading icon turns by itself</span>
<div class="row">
<LoadingOutlined />
</div>
</div>
<div class="field">
<span class="caption">rotate: 0, 90, 180 and 270 degrees</span>
<div class="row">
<ArrowUpOutlined rotate={0} />
<ArrowUpOutlined rotate={90} />
<ArrowUpOutlined rotate={180} />
<ArrowUpOutlined rotate={270} />
</div>
</div>
<div class="field">
<span class="caption">rotate and spin together</span>
<div class="row">
<RocketOutlined rotate={45} spin />
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
font-size: 24px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
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)
<script lang="ts">
import { Button, getTwoToneColor, setTwoToneColor } from 'svelte-ant-design';
import { CheckCircleTwoTone, HeartTwoTone, SmileTwoTone } from 'svelte-ant-design/icons';
import { onDestroy } from 'svelte';
// The colors the library starts with: the theme tokens. `setTwoToneColor` with them puts the default back.
const THEME = ['var(--ads-icon-twotone-primary)', 'var(--ads-icon-twotone-secondary)'] as [string, string];
// The global color belongs to the whole page; leaving this example puts the default back.
onDestroy(() => setTwoToneColor(THEME));
</script>
<div class="stack">
<div class="field">
<span class="caption">no twoToneColor: the theme colors</span>
<div class="row">
<SmileTwoTone />
</div>
</div>
<div class="field">
<span class="caption">twoToneColor="#eb2f96", twoToneColor="#52c41a": the second color is derived</span>
<div class="row">
<HeartTwoTone twoToneColor="#eb2f96" />
<CheckCircleTwoTone twoToneColor="#52c41a" />
</div>
</div>
<div class="field">
<span class="caption">twoToneColor as a pair: [primary, secondary]</span>
<div class="row">
<SmileTwoTone twoToneColor={['#1677ff', '#ffe58f']} />
<HeartTwoTone twoToneColor={['#722ed1', '#d3adf7']} />
</div>
</div>
<div class="field">
<span class="caption">setTwoToneColor: the default of every two-tone icon of the page that has no color of its own</span>
<div class="row">
<HeartTwoTone />
<Button size="small" onclick={() => setTwoToneColor('#fa8c16')}>Orange</Button>
<Button size="small" onclick={() => setTwoToneColor(THEME)}>Theme</Button>
</div>
<span role="status">getTwoToneColor(): {getTwoToneColor().join(' / ')}</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
font-size: 24px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
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
<script lang="ts">
import { BellOutlined, StarOutlined } from 'svelte-ant-design/icons';
</script>
<div class="stack">
<div class="field">
<span class="caption">no label: an image named by the drawing (star)</span>
<div class="row">
<StarOutlined />
</div>
</div>
<div class="field">
<span class="caption">label="Favourite": your own name, in your own language</span>
<div class="row">
<StarOutlined label="Favourite" />
</div>
</div>
<div class="field">
<span class="caption">label="": hidden from assistive technology, next to visible text</span>
<div class="row">
<BellOutlined label="" />
<span>Notifications</span>
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
font-size: 20px;
}
.row span {
font-size: 14px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
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
<script lang="ts">
import { Button } from 'svelte-ant-design';
import { DeleteOutlined, SearchOutlined, StarOutlined } from 'svelte-ant-design/icons';
let stars = $state(0);
// An icon with a click handler is not a button by itself: it gets a role, a place in the tab order and the keys.
function onkeydown(event: KeyboardEvent) {
if (event.key === 'Enter' || event.key === ' ') {
event.preventDefault();
stars += 1;
}
}
</script>
{#snippet search()}
<SearchOutlined label="" />
{/snippet}
{#snippet remove()}
<DeleteOutlined label="" />
{/snippet}
<div class="stack">
<div class="field">
<span class="caption">in a Button with text: the icon is hidden, the text names the button</span>
<div class="row">
<Button type="primary" icon={search}>Search</Button>
</div>
</div>
<div class="field">
<span class="caption">in an icon-only Button: the button is named, the icon is hidden</span>
<div class="row">
<Button shape="circle" icon={remove} aria-label="Delete" />
</div>
</div>
<div class="field">
<span class="caption">an icon with onclick, role="button", tabindex and keys</span>
<div class="row">
<StarOutlined label="Add a star" role="button" tabindex={0} onclick={() => (stars += 1)} {onkeydown} style="cursor: pointer; font-size: 24px" />
</div>
<span role="status">stars: {stars}</span>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
icon: a definition, with spin, rotate and label like any other
children: your own svg inside the icon
<script lang="ts">
import { Icon, type IconDefinition } from 'svelte-ant-design';
// A drawing is data: a name, a theme and the nodes of an svg. This is the shape that `@ant-design/icons-svg` uses for its own.
const flag: IconDefinition = {
name: 'flag',
theme: 'filled',
icon: {
tag: 'svg',
attrs: { viewBox: '0 0 24 24', focusable: 'false' },
children: [{ tag: 'path', attrs: { d: 'M5 2h2v20H5zM8 3h12l-3 5 3 5H8z' } }]
}
};
</script>
{#snippet drop()}
<svg viewBox="0 0 24 24" width="1em" height="1em" fill="currentColor"><path d="M12 2C8 8 5 11 5 15a7 7 0 0 0 14 0c0-4-3-7-7-13z" /></svg>
{/snippet}
<div class="stack">
<div class="field">
<span class="caption">icon: a definition, with spin, rotate and label like any other</span>
<div class="row">
<Icon icon={flag} />
<Icon icon={flag} rotate={90} label="Flag, turned" />
</div>
</div>
<div class="field">
<span class="caption">children: your own svg inside the icon</span>
<div class="row">
<Icon label="Drop">
{@render drop()}
</Icon>
<Icon spin label="Drop, spinning">
{@render drop()}
</Icon>
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
font-size: 28px;
color: var(--ads-color-primary);
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
component: a Svelte component that renders an svg (panda-svg.part.svelte, shown below the example)
<script lang="ts">
import { Icon } from 'svelte-ant-design';
import PandaSvg from './panda-svg.part.svelte';
</script>
<div class="stack">
<div class="field">
<span class="caption">component: a Svelte component that renders an svg (panda-svg.part.svelte, shown below the example)</span>
<div class="row">
<Icon component={PandaSvg} label="Panda" />
<Icon component={PandaSvg} label="Panda, big" style="font-size: 40px" />
<Icon component={PandaSvg} spin rotate={20} label="Panda, spinning" />
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
font-size: 28px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
<!-- panda-svg.part.svelte -->
<script lang="ts">
// A custom drawing for Icon's `component` prop: it renders one svg and uses the width, the height and the fill it is given.
let { width = '1em', height = '1em', fill = 'currentColor' }: { width?: string; height?: string; fill?: string } = $props();
</script>
<svg viewBox="0 0 24 24" {width} {height} aria-hidden="true">
<circle cx="5.5" cy="5.5" r="3.5" {fill} />
<circle cx="18.5" cy="5.5" r="3.5" {fill} />
<circle cx="12" cy="13" r="9" fill="none" stroke={fill} stroke-width="2" />
<circle cx="8.5" cy="12" r="1.6" {fill} />
<circle cx="15.5" cy="12" r="1.6" {fill} />
</svg>
IconFont type="icon-ring", type="icon-square"
extraCommonProps: style for every icon of the font
<script lang="ts">
import { createFromIconfontCN } from 'svelte-ant-design';
// In your project `scriptUrl` is the address that your iconfont.cn project gives you, for example '//at.alicdn.com/t/font_xxx.js'.
// Here the sprite script is made on the page, so that the example works without a network. It does what that script does:
// it puts a <svg> with a <symbol id="icon-..."> for every icon at the start of the body.
const sprite = `(function () {
var holder = document.createElement('div');
holder.innerHTML = '<svg><symbol id="icon-ring" viewBox="0 0 24 24"><circle cx="12" cy="12" r="9" fill="none" stroke="currentColor" stroke-width="3"/></symbol>' +
'<symbol id="icon-square" viewBox="0 0 24 24"><rect x="4" y="4" width="16" height="16" rx="3" fill="currentColor"/></symbol></svg>';
var svg = holder.firstChild;
svg.style.position = 'absolute';
svg.style.width = 0;
svg.style.height = 0;
document.body.insertBefore(svg, document.body.firstChild);
})();`;
const scriptUrl = typeof document === 'undefined' ? '' : URL.createObjectURL(new Blob([sprite], { type: 'text/javascript' }));
const IconFont = createFromIconfontCN({ scriptUrl });
// `extraCommonProps` go to every icon of this font; the props of one icon win.
const Marker = createFromIconfontCN({ scriptUrl, extraCommonProps: { style: 'color: var(--ads-color-primary)' } });
</script>
<div class="stack">
<div class="field">
<span class="caption">IconFont type="icon-ring", type="icon-square"</span>
<div class="row">
<IconFont type="icon-ring" label="Ring" />
<IconFont type="icon-square" label="Square" />
<IconFont type="icon-ring" spin label="Ring, spinning" />
</div>
</div>
<div class="field">
<span class="caption">extraCommonProps: style for every icon of the font</span>
<div class="row">
<Marker type="icon-square" label="Square" />
<Marker type="icon-ring" label="Ring" rotate={45} />
</div>
</div>
</div>
<style>
.stack {
display: grid;
justify-items: start;
gap: 16px;
}
.field {
display: grid;
justify-items: start;
gap: 8px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 16px;
font-size: 28px;
}
.caption {
color: var(--ads-color-text-secondary);
font-size: 12px;
}
</style>
全部图标
集合中的每个图案都以可直接引入的组件形式呈现,分为六组,顺序与 Ant Design 的列表一致:可同时显示全部风格,也可一次显示一种。点击图标即可复制其名称。品牌标志归各自所有者所有。个别图案在某些尺寸下会因像素网格而显得不平整(例如 12 px 的 ShareAltOutlined),这是图案本身的问题,而非组件。
共 848 个图标
方向性图标91
提示建议性图标54
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} 可停止。 | boolean | false |
| 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 和手机读屏软件。