--- localeCode: zh-CN order: 12 category: 基础 title: Icon 图标 icon: doc-icons brief: 语义化的矢量图形。 --- ## 图标列表 ```icon ``` ## 代码演示 ### 如何引入 ```jsx import import Icon, { IconHome } from '@douyinfe/semi-icons'; ``` ### 基础使用 从`@douyinfe/semi-icons`包中引入图标 ```jsx live=true import React from 'react'; import { IconHome } from '@douyinfe/semi-icons'; () => ; ``` ### 旋转 从`@douyinfe/semi-icons`包中引入图标,自带尺寸、旋转、spin功能 ```jsx live=true import React from 'react'; import { IconHome, IconEmoji, IconSpin } from '@douyinfe/semi-icons'; () => (
); ``` ### 尺寸 > 可以改变`font-size`来更改图标大小 > Icon组件封装了size属性,可以更方便地定义图标尺寸,支持 `extra-small` (8x8),`small` (12x12), `default` (16x16), `large` (20x20), `extra-large` (24x24),当size指定为`inherit`时,图标大小继承当前上下文字体大小 ```jsx live=true import React from 'react'; import { IconSearch, IconHelpCircle, IconAlertCircle, IconMinusCircle, IconPlusCircle, IconPlus, IconRefresh } from '@douyinfe/semi-icons'; () => { // eslint-disable-next-line react/jsx-key const types = [, , , , , , ]; const sizes = ['extra-small', 'small', 'default', 'large', 'extra-large']; let icons = types.map((type, i) => { return
{sizes.map(size => React.cloneElement(type, {size, key:size}))}
; }); return icons; }; ``` ### 颜色 图标会自动继承外部容器 CSS 的 `color` 属性 你还可以通过给 Icon 设置 style props 来修改图标的颜色。 ```jsx live=true import React from 'react'; import { IconLikeHeart, IconFlag, IconLock, IconUnlock } from '@douyinfe/semi-icons'; () => (

); ``` ### 自定义图标 可以使用自定义图标传入Icon组件 Icon组件支持size、rotate、spin等属性 ```jsx live=true import React from 'react'; import { Icon } from '@douyinfe/semi-ui'; () => { function CustomIcon(){ return ; } return (
} /> } rotate={180} />
); }; ``` ### 使用svgr将svg文件转成ReactComponent 如果 Semi 提供的图标不足以满足业务需求,你也可以通过@svgr/webpack引入自定义图标,并以React组件形式使用 ``` // webpack.config.js { test: /\.svg$/, use: ['@svgr/webpack'], } import { Icon } from '@douyinfe/semi-ui'; import StarIcon from './star.svg'; } /> ``` ## API参考 ### Icon | 属性 | 说明 | 类型 | 默认值 | |-------|-------------|-----------------|--------| | className | 类名 | string | 无 | | onClick | 单击图标的回调事件 | (e: Event) => void | 无 | | onMouseDown | 鼠标按钮按下的回调事件 >=v1.21 | (e: Event) => void | 无 | | onMouseEnter | 进入图标的回调事件 | (e: Event) => void | 无 | | onMouseLeave | 离开图标的回调事件 | (e: Event) => void | 无 | | onMouseMove | 移动鼠标的回调事件 >=v1.21 | (e: Event) => void | 无 | | onMouseUp | 鼠标按钮抬起的回调事件 >=v1.21 | (e: Event) => void | 无 | | rotate | 旋转度数 | number | | | size | 尺寸,支持`inherit`,`extra-small`,`small`, `default`, `large`, `extra-large` | string | `default` | | spin | 旋转动画 | boolean | | | style | 图标样式 | CSSProperties | 无 | | svg | 图标内容 | ReactNode | 无 | ## Accessibility ### ARIA - Icon 组件 role 为 img,它的 aria-label 默认为组件的文件名。例如 IconHome 的 aria-label 为 `home`,如果你有更好的语义化名字,可以通过 aria-label 传入。 ```jsx live=true import React from 'react'; import { IconHome } from '@douyinfe/semi-icons'; () => ; ``` - Icon 内部的 svg 元素为装饰元素,默认设置了 aria-hidden 以不被屏幕阅读器阅读