Icon
Display built-in SVG icons from @nscode/icons, or provide your own SVG through the slot. Icons inherit the text color by default.
Import
import { HuiIcon } from '@nscode/handy-ui'
Basic Usage
Set name to an icon name. See the Icons guide and the actual exports of @nscode/icons for available names. The examples below show a selection of built-in icons.
<script setup lang="ts">
import { HuiIcon, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiIcon name="search" />
<HuiIcon name="home" />
<HuiIcon name="setting" />
<HuiIcon name="user" />
<HuiIcon name="bell" />
<HuiIcon name="mail" />
</HuiSpace>
</template>
An unknown name does not throw, but leaves a blank 1em×1em area (16px by default).
Icon Examples
The following selection of built-in icons from @nscode/icons is grouped by purpose. Click an icon to copy its name.
<script setup lang="ts">
import { HuiIcon, HuiMessage } from '@nscode/handy-ui'
const groups = [
{
title: '方向和导航',
names: [
'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'
]
},
{
title: '操作',
names: [
'close',
'check',
'plus',
'minus',
'edit',
'trash',
'refresh',
'copy',
'save',
'share',
'link',
'upload',
'download',
'filter',
'sort',
'drag',
'lock',
'eye',
'eye-off',
'zoom-in',
'zoom-out',
'rotate-left',
'rotate-right',
'fullscreen'
]
},
{
title: '状态',
names: [
'success',
'warning',
'error',
'info',
'help',
'loading',
'star',
'star-half',
'heart',
'clock',
'calendar'
]
},
{
title: '用户和通用',
names: ['user', 'users', 'setting', 'search', 'bell', 'phone', 'location', 'mail']
},
{ title: '文件', names: ['file', 'folder', 'image', 'table'] }
]
const copy = async (name: string) => {
try {
await navigator.clipboard.writeText(name)
HuiMessage.success(`Copied ${name}`)
} catch {
// The clipboard is unavailable outside HTTPS or localhost
HuiMessage.warning(`Copy failed; icon name: ${name}`)
}
}
</script>
<template>
<div v-for="group in groups" :key="group.title" class="icon-group">
<div class="icon-group__title">
{{
{
方向和导航: 'Directions and navigation',
操作: 'Actions',
状态: 'Status',
用户和通用: 'Users and general',
文件: 'Files'
}[group.title]
}}({{ group.names.length }})
</div>
<div class="icon-grid">
<button
v-for="name in group.names"
:key="name"
type="button"
class="icon-cell"
:title="`Copy ${name}`"
@click="copy(name)"
>
<HuiIcon :name="name" size="xl" />
<span class="icon-cell__name">{{ name }}</span>
</button>
</div>
</div>
</template>
<style scoped>
.icon-group + .icon-group {
margin-top: var(--hui-spacing-4);
}
.icon-group__title {
margin-bottom: var(--hui-spacing-2);
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.icon-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
gap: var(--hui-spacing-2);
}
.icon-cell {
display: flex;
flex-direction: column;
align-items: center;
gap: var(--hui-spacing-2);
padding: var(--hui-spacing-3) var(--hui-spacing-1);
border: var(--hui-border-width) solid var(--hui-border-color-lighter);
border-radius: var(--hui-border-radius);
background: none;
color: var(--hui-text-color);
font: inherit;
cursor: pointer;
}
.icon-cell:hover {
color: var(--hui-color-primary);
border-color: var(--hui-color-primary);
}
.icon-cell__name {
font-size: var(--hui-font-size-xs);
color: var(--hui-text-color-secondary);
}
</style>
Sizes
size accepts xs (12px), sm (14px), md (16px), lg (18px), or xl (24px), defaulting to md. These are fixed pixel sizes and do not change with the parent's font size.
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace size="large" align="end">
<HuiSpace direction="vertical">
<HuiIcon name="home" size="xs" />
<span class="size-label">xs 12px</span>
</HuiSpace>
<HuiSpace direction="vertical">
<HuiIcon name="home" size="sm" />
<span class="size-label">sm 14px</span>
</HuiSpace>
<HuiSpace direction="vertical">
<HuiIcon name="home" />
<span class="size-label">md 16px (default)</span>
</HuiSpace>
<HuiSpace direction="vertical">
<HuiIcon name="home" size="lg" />
<span class="size-label">lg 18px</span>
</HuiSpace>
<HuiSpace direction="vertical">
<HuiIcon name="home" size="xl" />
<span class="size-label">xl 24px</span>
</HuiSpace>
</HuiSpace>
<!-- The parent font is 24px; icons retain their own sizes -->
<div style="font-size: 24px">
<HuiSpace>
<span>24px text</span>
<HuiIcon name="star" size="xs" />
<HuiIcon name="star" />
<HuiIcon name="star" size="xl" />
</HuiSpace>
</div>
</HuiSpace>
</template>
<style scoped>
.size-label {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-xs);
}
</style>
Colors
color accepts any CSS color, including theme variables. When omitted, the icon inherits the text color through currentColor.
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<HuiIcon name="success" size="xl" color="var(--hui-color-success)" />
<HuiIcon name="warning" size="xl" color="var(--hui-color-warning)" />
<HuiIcon name="error" size="xl" color="var(--hui-color-danger)" />
<HuiIcon name="info" size="xl" color="var(--hui-color-info)" />
<HuiIcon name="help" size="xl" color="var(--hui-color-help)" />
<HuiIcon name="heart" size="xl" color="#eb2f96" />
<HuiIcon name="star" size="xl" color="rgb(250, 173, 20)" />
</HuiSpace>
<HuiSpace wrap>
<span style="color: var(--hui-color-primary)"
><HuiIcon name="link" /> Inherits blue text</span
>
<span style="color: var(--hui-color-danger)"><HuiIcon name="trash" /> Inherits red text</span>
<span style="color: var(--hui-text-color-secondary)">
<HuiIcon name="clock" /> Inherits secondary text
</span>
</HuiSpace>
</HuiSpace>
</template>
Animation
spin continuously rotates the icon, commonly for loading states. pulse varies opacity between 1 and 0.5 without rotating. Both work with size and color.
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">spin</span>
<HuiIcon name="loading" spin />
<HuiIcon name="refresh" spin size="lg" />
<HuiIcon name="setting" spin size="xl" color="var(--hui-color-primary)" />
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">pulse</span>
<HuiIcon name="bell" pulse />
<HuiIcon name="heart" pulse size="lg" color="var(--hui-color-danger)" />
<HuiIcon name="warning" pulse size="xl" color="var(--hui-color-warning)" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 48px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Inline with Text
The icon is an inline-flex 1em×1em square with vertical-align: -0.15em, roughly centering it alongside text.
size="inherit" follows the parent's font size; the default remains md (16px). Icons are hidden from screen readers by default. Providing an aria-label makes the icon announced as an image. For an icon-only button, set aria-label on the button itself.
<template>
<HuiSpace direction="vertical" align="start">
<span><HuiIcon name="location" /> Chaoyang District, Beijing</span>
<span><HuiIcon name="phone" /> 010-12345678</span>
<span><HuiIcon name="calendar" /> 2026-11-18 <HuiIcon name="clock" /> 14:30</span>
<span style="font-size: 18px"
><HuiIcon name="folder" size="inherit" /> Icon inherits 18px text</span
>
</HuiSpace>
</template>
The component declares no events. A @click listener on HuiIcon is forwarded to its root element. Click the stars below to set a score:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIcon, HuiSpace } from '@nscode/handy-ui'
const score = ref(3)
</script>
<template>
<HuiSpace>
<HuiIcon
v-for="i in 5"
:key="i"
name="star"
size="xl"
:color="i <= score ? 'var(--hui-color-warning)' : 'var(--hui-border-color)'"
style="cursor: pointer"
@click="score = i"
/>
<span>{{ score }} points</span>
</HuiSpace>
</template>
Custom Classes
class is applied to the root element. Use it for custom styling, such as adding a circular background.
<template>
<HuiSpace wrap>
<HuiIcon name="user" size="lg" class="icon-badge" />
<HuiIcon name="mail" size="lg" class="icon-badge" />
<HuiIcon name="bell" size="lg" class="icon-badge icon-badge--danger" />
</HuiSpace>
</template>
<style scoped>
.icon-badge {
box-sizing: content-box;
padding: var(--hui-spacing-2);
border-radius: var(--hui-border-radius-circle);
color: var(--hui-color-primary);
background-color: var(--hui-color-primary-bg);
}
.icon-badge--danger {
color: var(--hui-color-danger);
background-color: var(--hui-color-danger-bg);
}
</style>
Custom Icons
The default slot accepts any SVG, with size, color, spin, and pulse still taking effect. Include a viewBox and use currentColor for the fill. An <svg> without fill uses the text color by default. For stroke-only graphics, set fill="none" on the <svg> or <path>.
<template>
<HuiSpace wrap>
<HuiIcon name="layers" 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>
<HuiIcon name="layers" size="xl" color="var(--hui-color-success)">
<svg viewBox="0 0 24 24">
<path
d="M12 2 2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linejoin="round"
/>
</svg>
</HuiIcon>
<HuiIcon name="spinner" size="xl" spin color="var(--hui-color-primary)">
<svg viewBox="0 0 24 24">
<path
d="M12 3a9 9 0 1 1-9 9"
fill="none"
stroke="currentColor"
stroke-width="3"
stroke-linecap="round"
/>
</svg>
</HuiIcon>
<HuiIcon name="dot" size="xl" pulse color="var(--hui-color-danger)">
<svg viewBox="0 0 24 24" fill="currentColor">
<circle cx="12" cy="12" r="6" />
</svg>
</HuiIcon>
</HuiSpace>
</template>
Slot content takes precedence over the built-in icon selected by name. name remains required, but is not used as the default accessible name. An unknown name without slot content produces an [HandyUI] warning in development.
Inheriting Font Size
size=inherit follows the text's font size. Set aria-label on the button when it contains only an icon.
<script setup lang="ts">
import { HuiIcon, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace align="center"
><span style="font-size: 24px"><HuiIcon name="info" size="inherit" />24px</span
><span style="font-size: 32px"><HuiIcon name="info" size="inherit" />32px</span
><HuiIcon name="info" aria-label="Information"
/></HuiSpace>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| name | HuiIconName | - | Required icon name. Built-in names have editor completion |
| size | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'inherit' | 'md' | Icon size: 12px / 14px / 16px / 18px / 24px; inherit follows the parent's font size |
| color | string | undefined | Icon color; inherits the text color when omitted |
| spin | boolean | false | Whether to rotate continuously |
| pulse | boolean | false | Whether to pulse opacity between 1 and 0.5 |
| class | string | '' | Additional classes on the root element |
Slots
| Slot | Description |
|---|---|
| default | Custom icon content; replaces the built-in icon selected by name |
Types
import type { IconSize, IconProps, HuiIconName } from '@nscode/handy-ui'
import type { IconName } from '@nscode/icons'
type IconSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'inherit'
// IconName is the union of all built-in icon names; string & {} permits other names when using the slot
type HuiIconName = IconName | (string & {})
interface IconProps {
name: HuiIconName
size?: IconSize
color?: string
spin?: boolean
pulse?: boolean
class?: string
}