Components/Avatar

Avatar

An image, initials, or icon that represents a user.

VerifiedSince 0.3.4

Usage

Basic

Loading demo...

Sizes

size takes a preset name or a custom pixel value.

Loading demo...

Text Avatars

Loading demo...

Icon Avatars

Loading demo...

Avatar Group

Avatars beyond max collapse into a +N avatar.

Loading demo...

Hover Effects

hoverEffect sets each avatar's hover feedback; spreadOnHover fans the row apart while the group is hovered.

Loading demo...

Overflow Popover

overflowPopover reveals the collapsed avatars from +N; the overflow slot replaces the panel content.

Loading demo...

Status

Loading demo...

Best Practices

  • Pass name to generate identity marks instead of hard-coding initials.
  • Provide meaningful alt when src shows a real user's image.
  • Use status for presence only; use TxStatusBadge for textual system state.
  • When a group shows status, raise overlap or enable spreadOnHover so dots stay visible.
  • Treat the overflow popover as glance-only; use a list component when members need actions.

API Reference

TxAvatar

Props

PropTypeDefaultDescription
srcstring-Image URL; falls back when it fails to load.
altstring-Image alt text; provide it when src shows a real user.
namestring-Name used to generate initials.
iconstring-Fallback icon name, passed to TxIcon.
size'small' | 'medium' | 'large' | 'xlarge' | number | \${number}` | `${number}px``'medium'Preset name or positive pixel size; invalid values are ignored.
status'online' | 'offline' | 'busy' | 'away'-Status dot in the corner, inset to follow shape.
shape'circle' | 'square' | 'rounded''circle'Avatar shape.
clickablebooleanfalseEnables pointer styling, button semantics, and the click event.
backgroundColorstring-Background of the fallback content.
textColorstring'#ffffff' when backgroundColor is setText color of the fallback content; applies only with backgroundColor.

Events

EventPayloadDescription
click-Fires on click, Enter, or Space when clickable is set.

Slots

SlotPropsDescription
default-Custom fallback content; takes priority over icon and name.

TxAvatarGroup

Props

PropTypeDefaultDescription
maxnumberchild countAvatars shown before collapsing into +N; negative values count as zero.
sizeAvatarSize-Injected into child avatars that don't set their own size.
overlapnumber | string8How far neighbouring avatars overlap; numbers are px.
hoverEffect'none' | 'lift''lift'Per-avatar hover feedback; lift raises, shadows, and brings the avatar forward.
spreadOnHoverbooleanfalseEases the overlap to spreadOverlap while the group is hovered.
spreadOverlapnumber | string0Overlap once spread; negative values leave a gap.
overflowPopoverbooleanfalseAttaches an overflow popover to +N.
overflowPopoverTrigger'hover' | 'click''hover'How the popover opens.
overflowPopoverPlacementPopoverPlacement'top'Popover position relative to +N.

Slots

SlotPropsDescription
default-TxAvatar children or compatible avatar VNodes.
overflow{ nodes: VNode[], count: number }Overflow popover panel content; nodes are the avatars max cut off.

Overview

  • Content falls back from the image to the default slot, icon, initials from name, then the default user icon; a failed image load falls back the same way.
  • Initials are the first characters of the first and last words, uppercased.
  • With clickable, the root gets role="button" and tabindex="0", and Enter / Space emit click like a click does.
  • In a group, z-index rises left to right, so each avatar covers the bottom-right corner — status dot included — of the one before; lift brings the hovered avatar forward.
  • With overflowPopover on and avatars overflowing, +N is wrapped in TxPopover; otherwise it is a plain avatar with no floating layer.
  • Under reduced motion, the hover translate and transitions drop out; the forward stacking and shadow remain.

Technologies

  • The root doesn't clip; the image and fallback layers do, which lets the status dot extend past the shape.
  • Group negative margins and z-index come from the stylesheet (--tx-avatar-group-overlap, --tx-avatar-group-index); children only get their ring inline.
  • Source: packages/tuffex/packages/components/src/avatar/.
查看源码
packages/tuffex/packages/components/src/avatar/index.ts

Customization

VariableWritten byPurpose
--tx-avatar-sizecustom sizeWidth and height.
--tx-avatar-font-sizecustom sizeFallback text and icon size.
--tx-avatar-status-sizecustom sizeStatus dot outer diameter, ring included.
--tx-avatar-status-bordercustom sizeStatus dot ring width.
--tx-avatar-bgbackgroundColorFallback background.
--tx-avatar-texttextColor (with backgroundColor)Fallback text color.
--tx-avatar-ringcaller / themeRing color around the status dot and grouped avatars; defaults to --tx-bg-color.
--tx-avatar-group-overlapoverlapResolved overlap distance.
--tx-avatar-group-spread-overlapspreadOverlapOverlap once spread.
--tx-avatar-group-hover-zcaller / themez-index of the hovered avatar; defaults to 999.
--tx-avatar-group-overflow-widthcaller / themeOverflow panel width before wrapping; defaults to 232px.
--tx-avatar-group-bordercaller / themeRing color of grouped avatars.

--tx-avatar-*-preset, --tx-avatar-status-diameter, --tx-avatar-status-ring, --tx-avatar-status-inset, --tx-avatar-group-gap, --tx-avatar-group-index, and --tx-avatar-group-more-z are internal; don't set them directly.