Avatar
LhAvatar is used to display user avatars. Supports image, icon, and text modes with automatic fallback.
Basic Usage
Image Avatar
vue
<script setup lang="ts">
import { LhAvatar } from '@lh-ui/vue3'
</script>
<template>
<LhAvatar src="https://api.dicebear.com/9.x/notionists/svg?seed=mo" />
</template>vue
<script>
import { LhAvatar } from '@lh-ui/vue3'
export default {
components: { LhAvatar },
}
</script>
<template>
<LhAvatar src="https://api.dicebear.com/9.x/notionists/svg?seed=mo" />
</template>fit sets the crop mode (native object-fit) and alt provides alternative text.
Fit & alt
vue
<script setup lang="ts">
</script>
<template>
<!-- Fit & alt -->
<LhAvatar
src="https://api.dicebear.com/9.x/notionists/svg?seed=f"
fit="cover"
alt="User avatar"
/>
</template>vue
<script>
export default {}
</script>
<template>
<!-- Fit & alt -->
<LhAvatar
src="https://api.dicebear.com/9.x/notionists/svg?seed=f"
fit="cover"
alt="User avatar"
/>
</template>Shape & Size
Circle & Square, Four Sizes
SMLC
vue
<script setup lang="ts">
</script>
<template>
<div style="display: flex; align-items: center; gap: 12px">
<LhAvatar shape="square" size="small" text="S" />
<LhAvatar shape="square" size="medium" text="M" />
<LhAvatar shape="square" size="large" text="L" />
<LhAvatar size="large" text="Circle" />
</div>
</template>vue
<script>
export default {}
</script>
<template>
<div style="display: flex; align-items: center; gap: 12px">
<LhAvatar shape="square" size="small" text="S" />
<LhAvatar shape="square" size="medium" text="M" />
<LhAvatar shape="square" size="large" text="L" />
<LhAvatar size="large" text="Circle" />
</div>
</template>Text & Icon Fallback
Fallback to icon or text when no image
HA
vue
<script setup lang="ts">
</script>
<template>
<div style="display: flex; align-items: center; gap: 12px">
<LhAvatar text="Hello" />
<LhAvatar icon="user" />
<LhAvatar text="Alice Chen" />
</div>
</template>vue
<script>
export default {}
</script>
<template>
<div style="display: flex; align-items: center; gap: 12px">
<LhAvatar text="Hello" />
<LhAvatar icon="user" />
<LhAvatar text="Alice Chen" />
</div>
</template>Text avatars automatically show the first character (first Chinese character, or first letter capitalized for English).
Image Load Error
Fallback to text on error and trigger error event
vue
<script setup lang="ts">
function onError(e: Event) {
console.log('avatar load error', e)
}
</script>
<template>
<LhAvatar
src="/broken-avatar.png"
text="F"
@error="onError"
/>
</template>vue
<script>
export default {
methods: {
onError(e) {
console.log('avatar load error', e)
},
},
}
</script>
<template>
<LhAvatar
src="/broken-avatar.png"
text="F"
@error="onError"
/>
</template>Props
| Prop | Description | Type | Default |
|---|---|---|---|
| src | Image URL, fallback to icon/text when empty | string | — |
| alt | Alt text | string | — |
| icon | Icon name (a built-in LhIcon name, shown when src is missing) | "star" | "search" | "check" | "close" | "arrow-up" | "arrow-down" | "arrow-left" | "arrow-right" | "warning" | "error" | "info" | "loading" | "plus" | "minus" | "edit" | "delete" | "setting" | "user" | "home" | "menu" | "more" | "download" | "upload" | "refresh" | "folder" | "file" | "date" | "clock" | "email" | "phone" | "location" | "image" | "share" | "bell" | "filter" | "eye" | "link" | — |
| text | Text content (first char shown when src and icon missing) | string | — |
| shape | Shape | "circle" | "square" | 'circle' |
| size | Size | "small" | "medium" | "large" | "mini" | "x-large" | 'medium' |
| fit | Image fill mode | "fill" | "contain" | "cover" | "none" | "scale-down" | 'cover' |
| sx | Shorthand layout props (e.g. { mt: 2, w: '50%' }), applied as inline styles on the root element | LhSxProps | — |
Events
| Event | Description | Callback |
|---|---|---|
| error | Image load failed | (e: Event) => void |
Accessibility
- Image mode relies on the
<img alt>attribute; whenaltis omitted it falls back totext, then toavatar - Icon and text modes carry
role="img"on the root element, usingaltas the accessible name when provided; the icon itself isaria-hidden
Type Definitions
The TypeScript type structures exported by this component (auto-extracted from packages/components/avatar/src/types.ts and @lh-ui/share/core/* sources; click each entry to expand; see the Props table above for attribute descriptions):
LhAvatarProps
ts
export interface LhAvatarProps {
src?: string
alt?: string
icon?: LhIconName
text?: string
shape?: LhAvatarComponentShape
size?: LhAvatarSize
fit?: 'fill' | 'contain' | 'cover' | 'none' | 'scale-down'
sx?: LhSxProps
}LhAvatarSize
ts
export type LhAvatarSize = LhComponentSizeLhAvatarShape
ts
export type LhAvatarShape = LhAvatarComponentShapeLhAvatarComponentShape (from @lh-ui/share/core/avatar)
ts
export type LhAvatarShape = 'circle' | 'square';LhIconName (from @lh-ui/share/core/icon (svgs.d.ts))
ts
export type LhIconName = keyof typeof LH_ICON_SVGS_CONST;
