Avatar
Display an image, icon, or text as a user avatar. Arrange multiple avatars with HuiAvatarGroup.
Import
import { HuiAvatar, HuiAvatarGroup } from '@nscode/handy-ui'
Basic Usage
Pass an image URL to src. When src, icon, and label are all absent, a default user icon is displayed.
<script setup lang="ts">
import { HuiAvatar, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiAvatar src="/images/user.png" alt="张三" />
<HuiAvatar />
</HuiSpace>
</template>
Icons and Text
icon displays an icon and label displays text. The label takes the first two Unicode characters and uppercases them through Array.from(label).slice(0, 2).join('').toUpperCase(); it does not display the entire string. Custom default-slot content appears only when src, icon, and label are absent.
<script setup lang="ts">
import { HuiAvatar, HuiIcon, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<HuiAvatar icon="user" />
<HuiAvatar icon="users" bg-color="#13c2c2" />
<HuiAvatar icon="star" bg-color="#faad14" />
<HuiAvatar icon="setting" bg-color="#722ed1" />
</HuiSpace>
<HuiSpace wrap>
<HuiAvatar label="张" bg-color="#1890ff" />
<HuiAvatar label="李四" bg-color="#52c41a" />
<HuiAvatar label="ns" bg-color="#fa541c" />
<HuiAvatar>U</HuiAvatar>
<HuiAvatar bg-color="#eb2f96">
<HuiIcon name="heart" />
</HuiAvatar>
</HuiSpace>
</HuiSpace>
</template>
Display priority: image > fallback image > icon > label > default slot, which falls back to a user icon when omitted.
Sizes and Shapes
size accepts large (40px), default (32px), small (24px), or a pixel number. Numeric sizes scale the text and icon to 0.4 times the size. When omitted, size follows the avatar group, then HuiConfigProvider, falling back to default.
shape accepts circle (the default) or square (a rounded square). The square's radius follows its size: 8px for large, 6px for default, 4px for small, and 6px for numeric sizes.
Each row below uses a shape, showing large, default, small, and numeric sizes 48 and 64 from left to right:
<script setup lang="ts">
import { HuiAvatar, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap align="center">
<span class="row-label">circle</span>
<HuiAvatar icon="user" size="large" />
<HuiAvatar icon="user" />
<HuiAvatar icon="user" size="small" />
<HuiAvatar label="字" :size="48" />
<HuiAvatar src="/images/user.png" alt="张三" :size="64" />
</HuiSpace>
<HuiSpace wrap align="center">
<span class="row-label">square</span>
<HuiAvatar icon="user" shape="square" size="large" />
<HuiAvatar icon="user" shape="square" />
<HuiAvatar icon="user" shape="square" size="small" />
<HuiAvatar label="字" shape="square" :size="48" />
<HuiAvatar src="/images/user.png" alt="张三" shape="square" :size="64" />
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 56px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Image Fit
fit corresponds to the image's object-fit, defaulting to cover. Compare the five values below using a 3:1 image inside a 64px square avatar:
fill: stretches to fill the area, distorting the image.contain: displays the whole image, leaving the background visible above and below.cover: fills the area while preserving proportions, cropping the left and right sides.none: uses the original image size, revealing only a central portion.scale-down: chooses the smaller result ofnoneandcontain. This image is larger than the avatar, so it behaves likecontain.
<script setup lang="ts">
import { HuiAvatar, HuiSpace } from '@nscode/handy-ui'
import type { AvatarFit } from '@nscode/handy-ui'
const fits: AvatarFit[] = ['fill', 'contain', 'cover', 'none', 'scale-down']
</script>
<template>
<HuiSpace wrap size="large">
<HuiSpace v-for="fit in fits" :key="fit" direction="vertical" align="center">
<HuiAvatar src="/images/wide.png" :fit="fit" shape="square" :size="64" />
<span class="caption">{{ fit }}</span>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Loading Failures and Fallbacks
When the primary image fails, fallback-src is tried if provided. If the fallback also fails or is absent, the component tries icon, label, and then the default slot. Each image failure emits error once. Changing src or fallback-src reloads the image.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiAvatar, HuiSpace } from '@nscode/handy-ui'
const errorCount = ref(0)
</script>
<template>
<HuiSpace wrap align="center">
<HuiAvatar src="/broken.png" fallback-src="/images/default-user.png" @error="errorCount++" />
<HuiAvatar src="/broken.png" icon="user" />
<HuiAvatar src="/broken.png" label="李" bg-color="#1890ff" />
<HuiAvatar src="/broken.png" />
<span>Failed {{ errorCount }} times</span>
</HuiSpace>
</template>
A successful image load emits load. Toggle between a valid and a broken URL below to observe the events and rendered fallback:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiAvatar, HuiButton, HuiSpace } from '@nscode/handy-ui'
const good = '/images/avatar.jpg'
const src = ref(good)
const status = ref('Loading')
const toggle = () => {
src.value = src.value === good ? '/broken.png' : good
status.value = 'Loading'
}
</script>
<template>
<HuiSpace wrap align="center">
<HuiAvatar
:src="src"
label="王"
bg-color="#52c41a"
size="large"
@load="status = 'load emitted: showing the image'"
@error="status = 'error emitted: showing the label'"
/>
<HuiButton size="small" @click="toggle">Change URL</HuiButton>
</HuiSpace>
<p style="margin-top: 12px">Current URL: {{ src }}, {{ status }}</p>
</template>
Backgrounds and Borders
bg-color sets the background, using the placeholder gray by default. border=true adds a 2px solid border in the page background color (--hui-bg-color), visible against a tinted background. An object customizes its style, width, and color; show: false hides it.
<script setup lang="ts">
import { HuiAvatar, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<HuiAvatar label="A" />
<HuiAvatar label="A" bg-color="#ff4d4f" />
<HuiAvatar label="B" bg-color="#1890ff" />
<HuiAvatar icon="user" bg-color="#52c41a" />
</HuiSpace>
<div class="tinted">
<HuiSpace wrap>
<HuiAvatar icon="user" />
<HuiAvatar icon="user" border />
<HuiAvatar icon="user" :border="{ show: false }" />
</HuiSpace>
</div>
</HuiSpace>
</template>
<style scoped>
.tinted {
padding: var(--hui-spacing-3);
background-color: var(--hui-fill-color);
border-radius: var(--hui-border-radius);
}
</style>
The border object's style accepts solid, dashed, dotted, double, or none. width defaults to 2 pixels and color to var(--hui-bg-color):
<script setup lang="ts">
import { HuiAvatar, HuiSpace } from '@nscode/handy-ui'
import type { AvatarBorderStyle } from '@nscode/handy-ui'
const borderStyles: AvatarBorderStyle[] = ['solid', 'dashed', 'dotted', 'double', 'none']
</script>
<template>
<HuiSpace wrap size="large">
<HuiSpace v-for="item in borderStyles" :key="item" direction="vertical" align="center">
<HuiAvatar label="B" size="large" :border="{ style: item, width: 4, color: '#1890ff' }" />
<span class="caption">{{ item }}</span>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.caption {
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Avatar Groups
Place multiple HuiAvatar components in HuiAvatarGroup. By default, avatars overlap by 8px to the left and have an outline in the background color. :overlap="false" places them side by side. Use v-for and mix image, icon, and text avatars.
<script setup lang="ts">
import { HuiAvatar, HuiAvatarGroup, HuiSpace } from '@nscode/handy-ui'
const users = [
{ id: 1, name: '张' },
{ id: 2, name: '李' },
{ id: 3, name: '王' }
]
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<HuiAvatarGroup>
<HuiAvatar v-for="u in users" :key="u.id" :label="u.name" />
</HuiAvatarGroup>
<HuiAvatarGroup :overlap="false">
<HuiAvatar v-for="u in users" :key="u.id" :label="u.name" />
</HuiAvatarGroup>
</HuiSpace>
<HuiAvatarGroup>
<HuiAvatar src="/images/user.png" alt="张三" />
<HuiAvatar icon="user" bg-color="#13c2c2" />
<HuiAvatar label="李" bg-color="#1890ff" />
<HuiAvatar label="王" bg-color="#52c41a" />
<HuiAvatar />
</HuiAvatarGroup>
</HuiSpace>
</template>
Maximum Count
max limits the visible avatar count, combining the rest into +N. Click +N, or Tab to it and press Enter/Space, to emit excess-click. Click it below to show all avatars:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiAvatar, HuiAvatarGroup, HuiButton, HuiSpace } from '@nscode/handy-ui'
const users = ['张', '李', '王', '赵', '钱', '孙', '周']
const max = ref<number | undefined>(3)
const showAll = () => {
max.value = undefined
}
</script>
<template>
<HuiSpace wrap align="center">
<HuiAvatarGroup :max="max" @excess-click="showAll">
<HuiAvatar v-for="name in users" :key="name" :label="name" />
</HuiAvatarGroup>
<HuiButton v-if="max === undefined" size="small" @click="max = 3">Collapse</HuiButton>
</HuiSpace>
<p style="margin-top: 12px">
{{
max === undefined ? 'All members are shown' : `Showing the first ${max}; click +N to show all`
}}
</p>
</template>
Group Defaults
The group's size, shape, bg-color, and border provide defaults for its avatars. An explicit value on an avatar takes precedence. These settings also apply to +N.
<script setup lang="ts">
import { HuiAvatar, HuiAvatarGroup, HuiSpace } from '@nscode/handy-ui'
const users = ['张', '李', '王', '赵', '钱']
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap align="center">
<span class="row-label">large</span>
<HuiAvatarGroup size="large" :max="3" bg-color="#1890ff">
<HuiAvatar v-for="name in users" :key="name" :label="name" />
</HuiAvatarGroup>
</HuiSpace>
<HuiSpace wrap align="center">
<span class="row-label">default</span>
<HuiAvatarGroup :max="3" bg-color="#1890ff">
<HuiAvatar v-for="name in users" :key="name" :label="name" />
</HuiAvatarGroup>
</HuiSpace>
<HuiSpace wrap align="center">
<span class="row-label">small</span>
<HuiAvatarGroup size="small" :max="3" bg-color="#1890ff">
<HuiAvatar v-for="name in users" :key="name" :label="name" />
</HuiAvatarGroup>
</HuiSpace>
<HuiSpace wrap align="center">
<span class="row-label">square</span>
<HuiAvatarGroup shape="square" :max="3" bg-color="#52c41a" :overlap="false">
<HuiAvatar v-for="name in users" :key="name" :label="name" />
</HuiAvatarGroup>
</HuiSpace>
<HuiSpace wrap align="center">
<span class="row-label">Explicit override</span>
<HuiAvatarGroup size="small" shape="square">
<HuiAvatar label="张" />
<!-- This avatar is large and circular -->
<HuiAvatar label="李" size="large" shape="circle" />
<HuiAvatar label="王" bg-color="#fa541c" />
</HuiAvatarGroup>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 64px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
HuiAvatar Props
| Prop | Type | Default | Description |
|---|---|---|---|
| src | string | - | Image URL |
| fallbackSrc | string | - | Fallback image URL if the primary image fails |
| icon | string | - | Icon name, used without an image or after all image loads fail |
| label | string | - | Text shown without an image or icon; uppercased |
| size | 'large' | 'default' | 'small' | number | 'default' | Size, with numbers in pixels. Follows the avatar group's size, then HuiConfigProvider.size, falling back to 'default' |
| shape | 'circle' | 'square' | 'circle' | Shape. Follows the avatar group's shape when omitted, otherwise defaults to 'circle' |
| fit | 'fill' | 'contain' | 'cover' | 'none' | 'scale-down' | 'cover' | Image fit mode |
| alt | string | - | Image alternative text |
| bgColor | string | - | Background color. Follows the avatar group's bgColor when omitted |
| border | AvatarBorder | boolean | - | Border. true gives a 2px solid background-colored outline. Follows the avatar group's border when omitted |
HuiAvatar Events
| Event | Callback arguments | Description |
|---|---|---|
| load | (event: Event) | Emitted when the primary or fallback image loads successfully |
| error | (event: Event) | Emitted once for each primary or fallback image failure |
HuiAvatar Slots
| Slot | Scope arguments | Description |
|---|---|---|
| default | - | Shown without src, icon, or label, or after all images fail without an icon or label. Defaults to a user icon |
HuiAvatarGroup Props
| Prop | Type | Default | Description |
|---|---|---|---|
| max | number | - | Maximum visible avatar count; overflow is combined into +N. Omit to show all |
| overlap | boolean | true | Whether avatars overlap |
| size | 'large' | 'default' | 'small' | number | - | Default size for avatars without their own size; also used by +N |
| shape | 'circle' | 'square' | - | Default shape for avatars without their own shape; also used by +N |
| bgColor | string | - | Default background for avatars without their own bgColor; also used by +N |
| border | AvatarBorder | boolean | - | Default border for avatars without their own border; also used by +N |
HuiAvatarGroup Events
| Event | Callback arguments | Description |
|---|---|---|
| excess-click | (event: MouseEvent) | Emitted on clicking +N or activating it with Enter/Space while focused |
HuiAvatarGroup Slots
| Slot | Scope arguments | Description |
|---|---|---|
| default | - | Place HuiAvatar components, including those generated by v-for, v-if, or template. Other content is filtered out |
Types
import type {
AvatarProps,
AvatarEmits,
AvatarSize,
AvatarShape,
AvatarFit,
AvatarBorder,
AvatarBorderStyle,
AvatarGroupProps,
AvatarGroupEmits
} from '@nscode/handy-ui'
type AvatarSize = 'large' | 'default' | 'small' | number
type AvatarShape = 'circle' | 'square'
type AvatarFit = 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'
type AvatarBorderStyle = 'solid' | 'dashed' | 'dotted' | 'double' | 'none'
interface AvatarBorder {
show?: boolean // Whether visible, default true
style?: AvatarBorderStyle // Default 'solid'
width?: number // Pixels, default 2
color?: string // Default 'var(--hui-bg-color)'
}
interface AvatarProps {
src?: string
fallbackSrc?: string
icon?: string
label?: string
size?: AvatarSize
shape?: AvatarShape
fit?: AvatarFit
alt?: string
bgColor?: string
border?: AvatarBorder | boolean
}
interface AvatarGroupProps {
max?: number
overlap?: boolean
size?: AvatarSize
shape?: AvatarShape
bgColor?: string
border?: AvatarBorder | boolean
}
AVATAR_GROUP_INJECTION_KEY is also exported for custom children integrated with HuiAvatarGroup.