Skip to content
Slotted

Avatar

Draws the picture of a person or an entity, and stands in with initials or a glyph when there is none.

MC

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes

Picture and fallback chain

src sets the picture. The avatar draws the first of three things that is available: the picture, the initials taken from name, then a person glyph. A picture that is still loading, or that fails, shows the initials; the browser’s broken-image mark never appears. onStatusChange in React and statusChange in Angular report the status derived from the picture. Press the button to make the request fail, then restore it.

MCStatus: loading

Status

status is idle when there is no src, loading while the picture is requested, loaded once it arrives and error when the request fails. The avatar derives it from the picture. A value passed in wins over the derived one and is never reported back, which suits a server that already knows the outcome. Here each status is passed in.

MCidle
MCloading
MCloaded
MCerror

Sizes

size takes xs, sm, md, lg or xl, from the densest rows to a profile header. The xs step has room for one letter, so it draws the first initial only; every other step draws two.

MMCMCMCMC

Shapes

shape="circle", the default, stands for a person. shape="square" stands for a team, an organization or a bot, so a list that mixes both stays readable at a glance.

MCNSNLRB

Tone and variant

tone sets the semantic color of the fallback: accent, secondary, info, success, warning or danger. variant paints that color soft, a tinted ground, or solid, a filled one. The default tone is auto, covered next.

soft
MCMCMCMCMCMC
solid
MCMCMCMCMCMC

Identity

With tone="auto" the avatar derives a color from name, so the same person keeps the same color in a list that is sorted or paged. auto picks from five tones, accent, secondary, success, warning and danger, so two people can share one: the color helps a reader scan, and the name identifies the person. idFor seeds the color in place of the name, for a name that changes or that two people share. initials replaces the derived letters.

From the name
MCMOJWER
Same idFor, different names
MCMC
Own initials
D4

Custom fallback content

Without a name and without src, the avatar draws the person glyph, and assistive technology skips it. Content passed as children in React, or projected with slAvatarFallback in Angular, replaces the initials and the glyph, for a mark that stands for an entity. The name is still the accessible name.

MC

Surface

surface sets the ground behind the avatar. tone, the default, paints the tone behind the fallback and nothing behind a loaded picture. none paints nothing. plate paints a light plate in every color scheme, for a logo made for a white ground: in the dark scheme the first two leave the mark on the page background, and the plate keeps it legible.

NFtone
NFnone
NFplate

Interactive

An avatar is not focusable and not a command. In React, render returns the element the page needs; in Angular, slAvatar is written on that element. An anchor or a button then takes the platform’s own keyboard behavior, and the avatar draws the focus ring and the hover edge. The name is the accessible name of that element, which keeps its own role: the first avatar is announced as a link and the second as a button. disabled marks a deactivated account and withholds the ring and the edge; a disabled button also needs the native disabled.

MCP

Beside a name

When the page already writes the name next to the avatar, decorative removes the avatar from the accessibility tree so the person is read once. The name still drives the initials and the color. avatar-group has its own page.

Maria ChenWarehouse supervisor

Defaults

A defaults provider sets size, shape, tone, variant and locale once for every avatar under it: AvatarDefaultsProvider in React, provideAvatarDefaults in Angular. An avatar that states a value of its own keeps it, as the last one does here.

NSRMC

Accessibility

  • The WAI-ARIA Authoring Practices have no avatar pattern. The guidance applied is the img role in WAI-ARIA 1.2, the W3C WAI alt decision tree, and WCAG 2.2 success criteria 1.1.1 Non-text Content, 1.4.11 Non-text Contrast and 2.5.8 Target Size (Minimum).
  • The name is on the root as aria-label with role="img", and the picture takes an empty alt. The avatar announces the same name in all four statuses; naming the image instead would announce the initials before the photograph loads and the name after.
  • An avatar with no name is aria-hidden, the rule that Icon takes. A present name sets role="img"; an absent name leaves nothing to announce.
  • decorative covers a name written beside the avatar, where announcing it would read the person twice. The name still drives the initials and the color.
  • The color derived from a name is an aid to scanning a list, not an identifier. The palette holds five colors, so collisions occur, and color alone is not a signal. The name identifies the person and is present in every status.
  • An avatar is not focusable and not a command. render can make the root an anchor or a button; the avatar then draws the focus ring and the hover edge, and withholds both when disabled. The element that render returns keeps its own role and takes the name as aria-label; a generic element returned there passes role="img".
  • An interactive avatar smaller than 24 by 24 CSS pixels grows a centered overlay to --slotted-target-min, which meets WCAG 2.2 2.5.8 at the two tighter densities without changing the layout. Inside a stack the overlay can reach under a neighbor; the raised member receives the pointer.
  • The fallback glyph is the person Glyph in an aria-hidden Icon, not a character: no font reliably carries a silhouette, and a character would be read aloud.
  • Under forced colors the avatar draws its edge in currentColor, because the theme maps every tone ground to one system color. The photograph is left unchanged.
  • The picture crosses over the initials in --slotted-duration-short, which prefers-reduced-motion reduces to 1ms in the theme. Nothing loops.

API

avatar

Props

namestring
The person or entity. It is the accessible name in every status, the source of the initials and the seed of the color.
srcstring
The picture. When absent, the avatar draws its fallback and requests nothing.
initialsstring
The letters to draw in place of those derived from the name. The box holds two letters, and xs holds one.
tone'auto' | 'accent' | 'secondary' | 'info' | 'success' | 'warning' | 'danger'Default auto
The semantic color of the fallback. auto derives one of accent, secondary, success, warning or danger from the name; info applies only when set.
idForstring
A stable identifier to derive the color from, for a name that is not stable or that two people share. Defaults to name.
variant'soft' | 'solid'Default soft
How the color is painted: soft is tinted, solid is filled.
surface'tone' | 'none' | 'plate'Default tone
The ground behind the avatar. tone paints the tone behind the fallback and nothing behind a loaded picture, none paints nothing, and plate paints a light plate in every color scheme, for a logo made for a white ground (--slotted-avatar-plate-surface).
size'xs' | 'sm' | 'md' | 'lg' | 'xl'Default md
One of five steps. --slotted-avatar-size sets a size that no step holds.
shape'circle' | 'square'Default circle
circle is for a person; square is for a team, an organization or a bot.
status'idle' | 'loading' | 'loaded' | 'error'
Where the picture is. A supplied value wins over the status derived from the img element.
onStatusChange(status) => void
Called with the derived status when the picture loads or fails. Not called for a status passed in.
disabledbooleanDefault false
Marks a deactivated account, or an avatar inside a control that the page has turned off.
decorativebooleanDefault false
Removes the avatar from the accessibility tree, for an avatar beside the name it belongs to.
localestring
BCP 47 tag that sets the case of the initials. Falls back to the defaults provider, then to the runtime locale.
childrenReactNode
Content for the fallback in place of the initials and the glyph, such as a logo or an Icon.
render(props) => ReactNode
Renders the avatar into another element, such as an anchor for an avatar that opens a profile.
renderImage(props) => ReactNode
Renders the picture into the consumer’s own element, such as a Next.js Image, while the avatar keeps reporting the load.
refRef<HTMLElement>
Reaches the native element. The props type is the element’s own, extended.
AvatarDefaultsProvider{ size, shape, tone, variant, locale }
On a subtreeSets the defaults for every avatar under it. An explicit prop wins.

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every avatar inside it.

Avatar tokens

TokenDefault
--slotted-avatar-border-widthvar(--slotted-border-width-thin, 1px)
--slotted-avatar-disabled-image-filtergrayscale(1) opacity(0.5)
--slotted-avatar-elevation0 0 transparent
--slotted-avatar-font-familyvar(--slotted-control-font-family, sans-serif)
--slotted-avatar-font-weightvar(--slotted-font-weight-medium, 500)
--slotted-avatar-glyph-sizecalc(var(--_size) * 0.68)
--slotted-avatar-initials-sizecalc(var(--_size) * 0.4)
--slotted-avatar-plate-color#1f2937
--slotted-avatar-plate-surface#ffffff
--slotted-avatar-radiusround(nearest, calc(var(--_size) / 6), 1px)
--slotted-avatar-ring-colorvar(--slotted-surface, Canvas)
--slotted-avatar-ring-width0
--slotted-avatar-sizevar(--slotted-avatar-size-md, 36px)
--slotted-avatar-size-lgcomfortable 48px · compact 42px · dense 36px
--slotted-avatar-size-mdcomfortable 36px · compact 32px · dense 28px
--slotted-avatar-size-smcomfortable 28px · compact 26px · dense 24px
--slotted-avatar-size-xlcomfortable 64px · compact 56px · dense 48px
--slotted-avatar-size-xscomfortable 24px · compact 22px · dense 20px
--slotted-avatar-state-layer-active14%
--slotted-avatar-state-layer-hover8%

Theme tokens it reads

TokenDefault
--slotted-border-strongSet per scheme by the theme
--slotted-border-subtleSet per scheme by the theme
--slotted-border-width-thick2px
--slotted-border-width-thin1px
--slotted-control-font-familyvar(--slotted-font-family-sans)
--slotted-disabled-backgroundSet per scheme by the theme
--slotted-disabled-borderSet per scheme by the theme
--slotted-disabled-foregroundSet per scheme by the theme
--slotted-duration-short120ms
--slotted-easing-entercubic-bezier(0.05, 0.7, 0.1, 1)
--slotted-easing-exitcubic-bezier(0.3, 0, 0.8, 0.15)
--slotted-easing-standardcubic-bezier(0.2, 0, 0, 1)
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-font-weight-medium500
--slotted-radius-full9999px
--slotted-surfaceSet per scheme by the theme
--slotted-target-min24px
--slotted-text-box-edgecap alphabetic
--slotted-tone-accent-borderSet per scheme by the theme
--slotted-tone-accent-on-solidSet per scheme by the theme
--slotted-tone-accent-solidSet per scheme by the theme
--slotted-tone-accent-surfaceSet per scheme by the theme
--slotted-tone-accent-textSet per scheme by the theme
--slotted-tone-danger-borderSet per scheme by the theme
--slotted-tone-danger-on-solidSet per scheme by the theme
--slotted-tone-danger-solidSet per scheme by the theme
--slotted-tone-danger-surfaceSet per scheme by the theme
--slotted-tone-danger-textSet per scheme by the theme
--slotted-tone-info-borderSet per scheme by the theme
--slotted-tone-info-on-solidSet per scheme by the theme
--slotted-tone-info-solidSet per scheme by the theme
--slotted-tone-info-surfaceSet per scheme by the theme
--slotted-tone-info-textSet per scheme by the theme
--slotted-tone-secondary-borderSet per scheme by the theme
--slotted-tone-secondary-on-solidSet per scheme by the theme
--slotted-tone-secondary-solid-hoverSet per scheme by the theme
--slotted-tone-secondary-surfaceSet per scheme by the theme
--slotted-tone-secondary-textSet per scheme by the theme
--slotted-tone-success-borderSet per scheme by the theme
--slotted-tone-success-on-solidSet per scheme by the theme
--slotted-tone-success-solidSet per scheme by the theme
--slotted-tone-success-surfaceSet per scheme by the theme
--slotted-tone-success-textSet per scheme by the theme
--slotted-tone-warning-borderSet per scheme by the theme
--slotted-tone-warning-on-solidSet per scheme by the theme
--slotted-tone-warning-solidSet per scheme by the theme
--slotted-tone-warning-surfaceSet per scheme by the theme
--slotted-tone-warning-textSet per scheme by the theme