跳到主要内容

Avatar 头像

头像组件,支持图片、图标、文字三种内容;图片为空或加载失败时依次自动回退。

基础用法​

基础用法
avataravatar水丹
水水水水水
水墨avatar
import { LhAvatar } from '@lh-ui/react'

export default function Demo() {
return (
<>
<LhAvatar src="/avatars/ink.png" />
<LhAvatar src="/avatars/ink.png" shape="square" />
<LhAvatar text="水墨" />
<LhAvatar icon="user" />
</>
)
}

Avatar Props​

属性说明类型可选值默认值
src图片地址;为空时依次回退到图标、文字、自定义内容string——
alt图片加载失败的替代文本,同时作为图标/文字分支的无障碍名称string——
icon图标回退:字符串取内置 LhIcon 图标名,也可传任意 ReactNode 自定义图标ReactNode——
text文字头像内容(src 与 icon 都缺省时取首字展示)string——
shape头像形状'circle' | 'square'—'circle'
size头像大小'mini' | 'small' | 'medium' | 'large' | 'x-large'—'medium'
fit图片填充模式'fill' | 'contain' | 'cover' | 'none' | 'scale-down'—'cover'
sx快速布局属性SxProps——

Avatar 事件​

事件说明回调签名Vue 对照
onError图片加载失败时触发(event: SyntheticEvent<HTMLImageElement>) => void@error
回退顺序

内容按 src 图片 → icon 图标 → text 首字 → children 依次回退:src 为空或图片加载失败时走图标分支,都没有才取 text 首字。文字头像取第一个字符(按码点提取,emoji 亦适用),如「水墨」→「水」、「Alice」→「A」。

与 Vue 版的对应

src / alt / text / shape / size / fit / sx 与 lh-ui-plus 的 LhAvatar 同名同义;React 无插槽,plus 的 #icon 与 icon prop 合并为一个 ReactNode prop(字符串按内置图标名解析),#default 兜底插槽映射为 children。

无障碍

图片分支由 <img alt> 自身命名(未传时依次取 alt / text / 'avatar');图标与文字分支输出 role="img",无障碍名称取 alt——请为非图片头像传入有语义的 alt。

样式​

import '@lh-ui/react/style/avatar'