图标
图标是内联的 SVG,颜色跟随文字颜色(currentColor),大小由 HuiIcon 的 size 控制:可选 xs 12px、sm 14px、md 16px、lg 18px、xl 24px,默认 md。这些尺寸是固定的像素值;设为 inherit 时,字号跟随父元素。组件的详细参数见 Icon 图标。
基本用法
<script setup lang="ts">
import { HuiIcon } from '@nscode/handy-ui'
</script>
<template>
<HuiIcon name="search" />
<HuiIcon name="success" color="#52c41a" size="lg" />
<HuiIcon name="loading" spin />
</template>
很多组件有 icon 这类属性,传的也是图标名,例如 <HuiButton icon="plus">新增</HuiButton>。
图标清单
name 使用下面这些内置名字。没有提供自定义插槽时,未知名字会在开发环境输出警告,并留下 1em×1em(默认 16px)的空白位置。
| 分组 | 图标名 |
|---|---|
| 方向和导航 | home menu apps top arrow-up arrow-down arrow-left arrow-right chevron-up chevron-down chevron-left chevron-right double-left double-right caret-up caret-down caret-left caret-right more-horizontal more-vertical |
| 操作 | close check plus minus edit trash refresh copy save share link upload download filter sort drag lock eye eye-off eye-dropper zoom-in zoom-out rotate-left rotate-right fullscreen fullscreen-exit density pin |
| 状态 | success warning error info help loading star star-half heart clock calendar |
| 用户和通用 | user users setting search bell phone location mail |
| 文件 | file folder image table |
| 媒体 | play pause replay skip-previous skip-next fast-rewind fast-forward volume-up volume-down volume-off pip subtitles |
| 编辑、裁切和对话 | undo redo format-bold format-italic format-underline format-strikethrough format-code format-quote format-list-bulleted format-list-numbered format-indent-increase format-indent-decrease format-align-left format-align-center format-align-right format-align-justify format-clear format-color-text format-color-fill format-heading horizontal-rule code-block link-off crop flip-horizontal flip-vertical send stop attach thumb-up thumb-down sparkle |
编辑、裁切和对话的 32 个通用图标归 editor 分组;图标可独立用于按钮、工具栏和内容展示。退出全屏复用“操作”分组已有的 fullscreen-exit。
播放器图标
用于播放、暂停、快进快退、音量、画中画和字幕。全屏退出使用已有的 fullscreen-exit。
<script setup lang="ts">
import { HuiIcon, HuiSpace } from '@nscode/handy-ui'
const names = [
'play',
'pause',
'replay',
'skip-previous',
'skip-next',
'fast-rewind',
'fast-forward',
'volume-up',
'volume-down',
'volume-off',
'pip',
'subtitles'
]
</script>
<template>
<HuiSpace wrap>
<HuiSpace v-for="name in names" :key="name" direction="vertical" align="center">
<HuiIcon :name="name" size="xl" /><span>{{ name }}</span>
</HuiSpace>
</HuiSpace>
</template>
用自己的图标
HuiIcon 的默认插槽可以放任意 SVG,尺寸、颜色、旋转这些属性照样生效:
<template>
<HuiIcon name="custom" size="lg" color="#1890ff">
<svg viewBox="0 0 24 24" fill="currentColor">
<path d="M12 2 2 7l10 5 10-5-10-5z" />
</svg>
</HuiIcon>
</template>
SVG 要带 viewBox,填充色写 currentColor,这样才能跟着 color 属性变色。
传了插槽时插槽内容优先,不再渲染 name 对应的内置图标。name 仍然必填,但不会作为默认朗读名称。图标默认带 aria-hidden="true";显式传入 aria-label 后才使用 role="img" 并朗读该名称。只有图标的操作按钮应在按钮本身填写 aria-label。
直接使用图标数据
@nscode/icons 导出了图标的原始数据,需要自己渲染时可以用:
import { icons, iconList, iconGroups } from '@nscode/icons'
icons['arrow-up'] // { name: 'arrow-up', svg: '<svg ...>...</svg>' }
iconList // 全部图标的数组,适合做图标选择器
iconGroups.navigation // 按分组取:navigation、action、status、user、file、media、editor
iconGroups.media // 播放、音量、画中画和字幕图标
iconGroups.editor // 32 个编辑、裁切和对话通用图标
icons['fullscreen-exit'] // 复用操作分组的退出全屏图标
每个图标也可以按驼峰名单独导入:import { arrowUp, chevronLeft } from '@nscode/icons'。