Skip to main content

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​

Basic usage
avataravatarID
IIIII
Ink washavatar
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​

PropDescriptionTypeOptionsDefault
srcImage address; falls back to icon, text, then custom content when absentstring——
altAlternative text for the failed image; also the accessibility name of the icon/text branchesstring——
iconIcon fallback: a string picks a built-in LhIcon name, or pass any ReactNode for a custom iconReactNode——
textText avatar content (the first character is shown when both src and icon are absent)string——
shapeShape'circle' | 'square'—'circle'
sizeSize'mini' | 'small' | 'medium' | 'large' | 'x-large'—'medium'
fitImage fit mode'fill' | 'contain' | 'cover' | 'none' | 'scale-down'—'cover'
sxLayout shorthandSxProps——

Avatar events​

EventDescriptionCallback signatureVue equivalent
onErrorFired when the image fails to load(event: SyntheticEvent<HTMLImageElement>) => void@error
Fallback order

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".

Vue equivalent

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.

Accessibility

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'