Skip to content

Avatar

YhAvatar displays a user or object with an image, icon, or text fallback.

Basic Usage

Display content through src, the icon prop, or the default slot.

USER
Basic Usage

Sizes

size supports small, default, large, and custom numeric pixel values.

ABCDYH
Sizes

Shapes

Use shape to switch between circle and square avatars.

CS
Shapes

Image Fit

fit follows native object-fit semantics for image content inside the avatar.

Image Fit

Custom Background Color

Use background-color, or the compatible color prop, to customize the avatar background.

ABYH
Custom Background Color

Error Handling

When the image fails to load, the component emits error, and the default slot becomes the fallback content.

Error Handling

Use in Nuxt

After installing @yh-ui/nuxt, YhAvatar can be used directly. Size, shape, image source, and text fallback render during SSR, while image loading and error fallback continue on the client.

J
Use in Nuxt

API

Props

NameDescriptionTypeDefault
shapeAvatar shape'circle' | 'square''circle'
sizeAvatar sizenumber | 'large' | 'default' | 'small''default'
srcImage URLstringundefined
src-setImage srcsetstringundefined
altImage alt textstringundefined
fitImage fit mode'fill' | 'contain' | 'cover' | 'none' | 'scale-down''cover'
iconIcon component or icon name used when no image is shownstring | Componentundefined
colorCompatible background color fieldstringundefined
background-colorBackground colorstringundefined
styleInline custom styleCSSPropertiesundefined
crossoriginNative image crossorigin attribute'' | 'anonymous' | 'use-credentials''anonymous'
theme-overridesComponent-level theme overridesComponentThemeVarsundefined

Events

NameDescriptionParameters
errorTriggered when the image fails to load(event: Event) => void

Slots

NameDescription
defaultCustom avatar content, such as text or an icon

Expose

This component does not expose public instance methods or properties.

Type Exports

NameDescription
YhAvatarPropsProps type for YhAvatar
YhAvatarEmitsEmits type for YhAvatar
YhAvatarSlotsSlots type for YhAvatar
YhAvatarShapeShape union type
YhAvatarSizeSize union type
YhAvatarFitImage fit union type
YhAvatarInstanceComponent instance type

Theme Variables

VariableDescriptionDefault
--yh-avatar-bg-colorAvatar background colorvar(--yh-color-primary-light-8, #d9ecff)
--yh-avatar-text-colorAvatar text colorvar(--yh-color-primary, #409eff)
--yh-avatar-radiusBorder radius for square avatarsvar(--yh-border-radius-base, 4px)

Released under the MIT License.