Skip to content
TopClass UI

Avatar

A picture, initials or an icon in a circle, on the same five sizes as every control so an avatar and a button line up.

split - size not measured yet

Avatar

split - size not measured yet

A picture, initials or an icon in a circle, on the same five sizes as every control so an avatar and a button line up.

Install
Omar Abed

Try it

size

The code

avatar.tsx
import { AvatarFallback, AvatarImage, AvatarRoot } from 'topclassui/avatar'

<AvatarRoot>
  <AvatarImage src={PORTRAIT} alt="Omar Abed" />
  <AvatarFallback>OA</AvatarFallback>
</AvatarRoot>

The fallback shows while the picture loads and after it fails; initials are its children.

Every state

5 states
Omar AbedImage
Initials
Icon
Sara KhalilFailed
Loading

Import from the package

topclassui/avatar
import { AvatarRoot, AvatarImage, AvatarFallback, AvatarSkeleton } from 'topclassui/avatar'

Install once:npm i topclassui@beta zod

Examples

  • Every part is a flat export: AvatarRoot, AvatarImage, AvatarFallback and AvatarSkeleton. There is no Avatar, and only AvatarRoot takes size.
  • Always render the fallback: the picture is laid over it, so it shows until the picture paints, with no JavaScript.
  • AvatarImage is the one client part. A picture that fails turns transparent and stays, so its alt still names the avatar, and the fallback shows.
  • An avatar of initials or an icon stays server HTML. Every avatar with a picture hydrates, so a table of fifty rows with pictures is fifty client parts.
  • For next/image, pass it as render on AvatarImage; it carries its own picture and alt text.
basic
Omar Abed
with-name
Omar AbedMaintainer
sizes
icon
failed
Omar AbedOmar AbedOmar AbedOmar AbedOmar Abed
skeleton
custom-size
Omar Abed
In Arabic, right to left
عمر فتحي وليلى حسن

Props

AvatarRoot

The props of AvatarRoot
PropTypeDefaultLimitsDescription
childrenReactNode--The parts: an AvatarFallback, and an AvatarImage when there is a picture.
sizexs | sm | md | lg | xl | custom"md"-The control scale on both axes, so an avatar lines up with a button of the same size; custom emits no size classes.

AvatarImage

The props of AvatarImage
PropTypeDefaultLimitsDescription
altstring--Who the picture shows. Pass an empty string when the name is written beside it.
renderReactElement--Render the picture as another element, such as next/image, which carries its own src and alt.

AvatarFallback

The props of AvatarFallback
PropTypeDefaultLimitsDescription
childrenReactNode--Initials or an Svg icon, shown until the image paints. Decorative: aria-hidden unless you pass false.

AvatarSkeleton

The props of AvatarSkeleton
PropTypeDefaultLimitsDescription
sizexs | sm | md | lg | xl | custom"md"-The control scale on both axes, so an avatar lines up with a button of the same size; custom emits no size classes.

Keyboard and accessibility

Nothing to operate: it is not interactive on its own.

  • AvatarImage requires alt, or a render element that carries its own. Pass alt="" when the name is written beside the avatar, so it is not read twice.
  • The fallback renders aria-hidden="true", so the picture's alt is the one name a screen reader reads.
  • With no picture and no name beside it, put aria-label on AvatarRoot: it then renders role="img" itself.

Right to left

The circle has no side to mirror; in a row, the avatar sits at the inline start and Arabic initials read right to left.

Server and client

Renders on the server; the interactive part is a client component that ships only when it is used.

Files: index.tsx, client.tsx.

On the chip: split - size not measured yet.