Avatar
Avatar component supporting three kinds of content: image, icon and text; when the image is absent or fails to load, it falls back automatically.
Basic usage
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="Ink wash" />
<LhAvatar icon="user" />
</>
)
}
Avatar Props
| Prop | Description | Type | Options | Default |
|---|---|---|---|---|
| src | Image address; falls back to icon, text, then custom content when absent | string | — | — |
| alt | Alternative text for the failed image; also the accessibility name of the icon/text branches | string | — | — |
| icon | Icon fallback: a string picks a built-in LhIcon name, or pass any ReactNode for a custom icon | ReactNode | — | — |
| text | Text avatar content (the first character is shown when both src and icon are absent) | string | — | — |
| shape | Shape | 'circle' | 'square' | — | 'circle' |
| size | Size | 'mini' | 'small' | 'medium' | 'large' | 'x-large' | — | 'medium' |
| fit | Image fit mode | 'fill' | 'contain' | 'cover' | 'none' | 'scale-down' | — | 'cover' |
| sx | Layout shorthand | SxProps | — | — |
Avatar events
| Event | Description | Callback signature | Vue equivalent |
|---|---|---|---|
| onError | Fired when the image fails to load | (event: SyntheticEvent<HTMLImageElement>) => void | @error |
Content falls back in the order src image → icon icon → text first character → children: an absent src or a failed image takes the icon branch, and only when all are missing is the first text character used. Text avatars take the first character (extracted by code point, so emoji work too): "Ink wash" → "I".
src / alt / text / shape / size / fit / sx match lh-ui-plus's
LhAvatar props; React has no slots, so plus's #icon slot and icon prop merge into one ReactNode prop (a string is parsed as a built-in icon name), and the #default fallback slot maps to children.
The image branch is named by <img alt> itself (falling back to alt / text / 'avatar'); the icon and text branches output role="img" and take their accessibility name from alt — pass a meaningful alt for non-image avatars.
Styles
import '@lh-ui/react/style/avatar'