Avatar
Draws the picture of a person or an entity, and stands in with initials or a glyph when there is none.
import { Avatar } from '@slotted/react/avatar';export default function AvatarBasic() { return <Avatar name="Maria Chen" />;}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.
pnpm add @slotted/react @slotted/theme @slotted/themesPicture 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.
import { useState } from 'react';import { Avatar } from '@slotted/react/avatar';import type { AvatarStatus } from '@slotted/react/avatar';import { Button } from '@slotted/react/button';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const PORTRAIT = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' fill='%23c7d2fe'/%3E%3Ccircle cx='32' cy='25' r='11' fill='%234338ca'/%3E%3Cpath d='M10 64a22 22 0 0 1 44 0Z' fill='%234338ca'/%3E%3C/svg%3E";const UNREADABLE = 'data:image/png;base64,AAAA';export default function AvatarPicture() { const [failed, setFailed] = useState(false); const [status, setStatus] = useState<AvatarStatus>('loading'); return ( <Stack gap={4}> <Avatar name="Maria Chen" onStatusChange={setStatus} size="xl" src={failed ? UNREADABLE : PORTRAIT} /> <Text size="sm" tone="muted"> Status: {status} </Text> <Inline> <Button onClick={() => setFailed(!failed)} tone="secondary" variant="outline"> {failed ? 'Restore the picture' : 'Fail the request'} </Button> </Inline> </Stack> );}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.
import { Avatar } from '@slotted/react/avatar';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const PORTRAIT = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' fill='%23c7d2fe'/%3E%3Ccircle cx='32' cy='25' r='11' fill='%234338ca'/%3E%3Cpath d='M10 64a22 22 0 0 1 44 0Z' fill='%234338ca'/%3E%3C/svg%3E";const STATUSES = ['idle', 'loading', 'loaded', 'error'] as const;export default function AvatarStatusExample() { return ( <Inline gap={6}> {STATUSES.map((status) => ( <Stack gap={2} key={status}> <Avatar name="Maria Chen" size="lg" src={PORTRAIT} status={status} /> <Text size="sm" tone="muted"> {status} </Text> </Stack> ))} </Inline> );}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.
import { Avatar } from '@slotted/react/avatar';import { Inline } from '@slotted/react/inline';export default function AvatarSizes() { return ( <Inline align="center" gap={4}> <Avatar name="Maria Chen" size="xs" /> <Avatar name="Maria Chen" size="sm" /> <Avatar name="Maria Chen" size="md" /> <Avatar name="Maria Chen" size="lg" /> <Avatar name="Maria Chen" size="xl" /> </Inline> );}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.
import { Avatar } from '@slotted/react/avatar';import { Inline } from '@slotted/react/inline';export default function AvatarShapes() { return ( <Inline align="center" gap={4}> <Avatar name="Maria Chen" shape="circle" size="lg" /> <Avatar name="Night shift" shape="square" size="lg" /> <Avatar name="Northwind Logistics" shape="square" size="lg" /> <Avatar name="Replenishment bot" shape="square" size="lg" /> </Inline> );}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.
import { Avatar } from '@slotted/react/avatar';import type { AvatarTone, AvatarVariant } from '@slotted/react/avatar';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const TONES: AvatarTone[] = ['accent', 'secondary', 'info', 'success', 'warning', 'danger'];const VARIANTS: AvatarVariant[] = ['soft', 'solid'];export default function AvatarTones() { return ( <Stack gap={4}> {VARIANTS.map((variant) => ( <Stack gap={2} key={variant}> <Text size="sm" tone="muted"> {variant} </Text> <Inline gap={3}> {TONES.map((tone) => ( <Avatar key={tone} name="Maria Chen" tone={tone} variant={variant} /> ))} </Inline> </Stack> ))} </Stack> );}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.
import { Avatar } from '@slotted/react/avatar';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';export default function AvatarIdentity() { return ( <Stack gap={4}> <Stack gap={2}> <Text size="sm" tone="muted"> From the name </Text> <Inline gap={3}> <Avatar name="Maria Chen" /> <Avatar name="Marta Okafor" /> <Avatar name="Jonas Weber" /> <Avatar name="Elena Rossi" /> </Inline> </Stack> <Stack gap={2}> <Text size="sm" tone="muted"> Same idFor, different names </Text> <Inline gap={3}> <Avatar idFor="emp-1042" name="Maria Chen" /> <Avatar idFor="emp-1042" name="M. Chen" /> </Inline> </Stack> <Stack gap={2}> <Text size="sm" tone="muted"> Own initials </Text> <Inline gap={3}> <Avatar initials="D4" name="Dock 4 inbound" shape="square" /> </Inline> </Stack> </Stack> );}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.
import { Avatar } from '@slotted/react/avatar';import { Glyph, Icon } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';export default function AvatarFallback() { return ( <Inline align="center" gap={4}> <Avatar name="Maria Chen" size="lg" /> <Avatar size="lg" /> <Avatar name="Receiving dock" shape="square" size="lg" tone="info"> <Icon> <Glyph name="inbox" /> </Icon> </Avatar> </Inline> );}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.
import { Avatar } from '@slotted/react/avatar';import type { AvatarSurface } from '@slotted/react/avatar';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const LOGO = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Cpath d='M8 52V12h8l32 28V12h8v40h-8L16 24v28Z' fill='%23111827'/%3E%3C/svg%3E";const SURFACES: AvatarSurface[] = ['tone', 'none', 'plate'];export default function AvatarSurfaceExample() { return ( <Inline gap={6}> {SURFACES.map((surface) => ( <Stack gap={2} key={surface}> <Avatar name="Nordic Freight" shape="square" size="lg" src={LOGO} surface={surface} /> <Text size="sm" tone="muted"> {surface} </Text> </Stack> ))} </Inline> );}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.
import { Avatar } from '@slotted/react/avatar';import { Inline } from '@slotted/react/inline';export default function AvatarInteractive() { return ( <Inline align="center" gap={4}> <Avatar name="Maria Chen" render={(props) => <a {...props} href="#maria-chen" />} size="lg" /> <Avatar name="Tomas Reyes" render={(props) => <button {...props} type="button" />} size="lg" /> <Avatar name="Priya Raman" render={(props) => <a {...props} href="#priya-raman" />} size="xs" /> <Avatar disabled name="Samuel Okafor" render={(props) => <button {...props} disabled type="button" />} size="lg" /> </Inline> );}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.
import { Avatar } from '@slotted/react/avatar';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Text } from '@slotted/react/typography';const PORTRAIT = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 64 64'%3E%3Crect width='64' height='64' fill='%23c7d2fe'/%3E%3Ccircle cx='32' cy='25' r='11' fill='%234338ca'/%3E%3Cpath d='M10 64a22 22 0 0 1 44 0Z' fill='%234338ca'/%3E%3C/svg%3E";export default function AvatarDecorative() { return ( <Inline align="center" gap={3}> <Avatar decorative name="Maria Chen" size="lg" src={PORTRAIT} /> <Stack gap={1}> <Text weight="medium">Maria Chen</Text> <Text size="sm" tone="muted"> Warehouse supervisor </Text> </Stack> </Inline> );}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.
import { Avatar, AvatarDefaultsProvider } from '@slotted/react/avatar';import { Inline } from '@slotted/react/inline';export default function AvatarDefaults() { return ( <AvatarDefaultsProvider shape="square" size="lg" variant="solid"> <Inline align="center" gap={4}> <Avatar name="Night shift" /> <Avatar name="Receiving" /> <Avatar name="Maria Chen" shape="circle" size="sm" /> </Inline> </AvatarDefaultsProvider> );}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'Defaultauto- 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'Defaultsoft- How the color is painted: soft is tinted, solid is filled.
surface'tone' | 'none' | 'plate'Defaulttone- 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'Defaultmd- One of five steps. --slotted-avatar-size sets a size that no step holds.
shape'circle' | 'square'Defaultcircle- 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.
disabledbooleanDefaultfalse- Marks a deactivated account, or an avatar inside a control that the page has turned off.
decorativebooleanDefaultfalse- 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
| Token | Default |
|---|---|
--slotted-avatar-border-width | var(--slotted-border-width-thin, 1px) |
--slotted-avatar-disabled-image-filter | grayscale(1) opacity(0.5) |
--slotted-avatar-elevation | 0 0 transparent |
--slotted-avatar-font-family | var(--slotted-control-font-family, sans-serif) |
--slotted-avatar-font-weight | var(--slotted-font-weight-medium, 500) |
--slotted-avatar-glyph-size | calc(var(--_size) * 0.68) |
--slotted-avatar-initials-size | calc(var(--_size) * 0.4) |
--slotted-avatar-plate-color | #1f2937 |
--slotted-avatar-plate-surface | #ffffff |
--slotted-avatar-radius | round(nearest, calc(var(--_size) / 6), 1px) |
--slotted-avatar-ring-color | var(--slotted-surface, Canvas) |
--slotted-avatar-ring-width | 0 |
--slotted-avatar-size | var(--slotted-avatar-size-md, 36px) |
--slotted-avatar-size-lg | comfortable 48px · compact 42px · dense 36px |
--slotted-avatar-size-md | comfortable 36px · compact 32px · dense 28px |
--slotted-avatar-size-sm | comfortable 28px · compact 26px · dense 24px |
--slotted-avatar-size-xl | comfortable 64px · compact 56px · dense 48px |
--slotted-avatar-size-xs | comfortable 24px · compact 22px · dense 20px |
--slotted-avatar-state-layer-active | 14% |
--slotted-avatar-state-layer-hover | 8% |
Theme tokens it reads
| Token | Default |
|---|---|
--slotted-border-strong | Set per scheme by the theme |
--slotted-border-subtle | Set per scheme by the theme |
--slotted-border-width-thick | 2px |
--slotted-border-width-thin | 1px |
--slotted-control-font-family | var(--slotted-font-family-sans) |
--slotted-disabled-background | Set per scheme by the theme |
--slotted-disabled-border | Set per scheme by the theme |
--slotted-disabled-foreground | Set per scheme by the theme |
--slotted-duration-short | 120ms |
--slotted-easing-enter | cubic-bezier(0.05, 0.7, 0.1, 1) |
--slotted-easing-exit | cubic-bezier(0.3, 0, 0.8, 0.15) |
--slotted-easing-standard | cubic-bezier(0.2, 0, 0, 1) |
--slotted-focus-ring-color | Set per scheme by the theme |
--slotted-focus-ring-offset | var(--slotted-border-width-thick) |
--slotted-focus-ring-width | var(--slotted-border-width-thick) |
--slotted-font-weight-medium | 500 |
--slotted-radius-full | 9999px |
--slotted-surface | Set per scheme by the theme |
--slotted-target-min | 24px |
--slotted-text-box-edge | cap alphabetic |
--slotted-tone-accent-border | Set per scheme by the theme |
--slotted-tone-accent-on-solid | Set per scheme by the theme |
--slotted-tone-accent-solid | Set per scheme by the theme |
--slotted-tone-accent-surface | Set per scheme by the theme |
--slotted-tone-accent-text | Set per scheme by the theme |
--slotted-tone-danger-border | Set per scheme by the theme |
--slotted-tone-danger-on-solid | Set per scheme by the theme |
--slotted-tone-danger-solid | Set per scheme by the theme |
--slotted-tone-danger-surface | Set per scheme by the theme |
--slotted-tone-danger-text | Set per scheme by the theme |
--slotted-tone-info-border | Set per scheme by the theme |
--slotted-tone-info-on-solid | Set per scheme by the theme |
--slotted-tone-info-solid | Set per scheme by the theme |
--slotted-tone-info-surface | Set per scheme by the theme |
--slotted-tone-info-text | Set per scheme by the theme |
--slotted-tone-secondary-border | Set per scheme by the theme |
--slotted-tone-secondary-on-solid | Set per scheme by the theme |
--slotted-tone-secondary-solid-hover | Set per scheme by the theme |
--slotted-tone-secondary-surface | Set per scheme by the theme |
--slotted-tone-secondary-text | Set per scheme by the theme |
--slotted-tone-success-border | Set per scheme by the theme |
--slotted-tone-success-on-solid | Set per scheme by the theme |
--slotted-tone-success-solid | Set per scheme by the theme |
--slotted-tone-success-surface | Set per scheme by the theme |
--slotted-tone-success-text | Set per scheme by the theme |
--slotted-tone-warning-border | Set per scheme by the theme |
--slotted-tone-warning-on-solid | Set per scheme by the theme |
--slotted-tone-warning-solid | Set per scheme by the theme |
--slotted-tone-warning-surface | Set per scheme by the theme |
--slotted-tone-warning-text | Set per scheme by the theme |