圖示
圖示使用行內 SVG,色彩跟隨文字的 currentColor。HuiIcon 的 size 可指定 xs 12px、sm 14px、md 16px、lg 18px、xl 24px,預設為 md。這些尺寸採固定像素值;設為 inherit 時,字級跟隨父元素。詳細參數請參閱 Icon 圖示。
基本用法
<script setup lang="ts">
import { HuiIcon, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace>
<HuiIcon name="search" />
<HuiIcon name="success" color="#52c41a" size="lg" />
<HuiIcon name="loading" spin />
</HuiSpace>
</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,尺寸、色彩與旋轉等屬性仍會生效:
<script setup lang="ts">
import { HuiIcon } from '@nscode/handy-ui'
</script>
<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'] // 沿用操作分組的退出全螢幕圖示。
每個圖示亦可依 camelCase 名稱單獨匯入,例如 import { arrowUp, chevronLeft } from '@nscode/icons'。