Avatar β
A generic, accessible avatar chip. It renders an image when you give it one, and gracefully falls back to name-derived initials (on a deterministic colour) or a custom node when the image is missing or fails to load β so a list never shows a broken-image icon.
π§ͺ Try every prop live in the Storybook.
Usage β
import { Avatar } from '@whereq/react'
;<Avatar src="/users/ada.jpg" name="Ada Lovelace" />Initials fallback (no src, or the image 404s) β the background colour is derived deterministically from name:
<Avatar name="Grace Hopper" /> // β "GH" on a stable colourHead-crop a portrait so the face fills the circle, with a ring:
<Avatar src="/mascot.png" name="Q" position="top" ring="#e4e8ee" ringWidth={2} />Shapes and a presence dot:
<Avatar name="Ada" shape="rounded" />
<Avatar src="/ada.jpg" name="Ada" status="online" />AvatarGroup β
Overlap several avatars and collapse the overflow into a +N chip. Size, shape and the separator ring are set once on the group and applied to every child.
import { Avatar, AvatarGroup } from '@whereq/react'
;<AvatarGroup size={32} max={3}>
<Avatar src="/a.jpg" name="Ada" />
<Avatar src="/b.jpg" name="Ben" />
<Avatar name="Cai" />
<Avatar name="Dee" />
</AvatarGroup>Props β
<Avatar> extends React.HTMLAttributes<HTMLSpanElement> (minus color) β className, style, ref and any other span prop pass through.
| Prop | Type | Default | Description |
|---|---|---|---|
src | string | β | Image URL. Falls back to initials if missing / errors. |
name | string | β | Drives auto-initials, the colour and the a11y label. |
initials | string | from name | Explicit initials override. |
fallback | ReactNode | β | Shown when there's no image and no initials (an icon). |
size | number | 40 | Diameter in px. |
shape | 'circle' | 'rounded' | 'square' | 'circle' | Outline shape. |
bg | string | from name | Chip background (behind transparent art / initials). |
color | string | '#fff' | Initials colour. |
ring | string | β | Inset ring colour. |
ringWidth | number | 1 | Ring width in px. |
fit | CSSProperties['objectFit'] | 'cover' | Image object-fit. |
position | CSSProperties['objectPosition'] | 'center' | Image object-position β use 'top' to head-crop. |
status | 'online' | 'offline' | 'busy' | 'away' | β | Presence dot in the corner. |
statusColors | Partial<Record<AvatarStatus, string>> | preset | Per-status dot colour overrides. |
statusRing | string | '#fff' | Separator ring around the status dot. |
alt | string | name | Image alt text. |
<AvatarGroup> props β
Extends React.HTMLAttributes<HTMLDivElement>.
| Prop | Type | Default | Description |
|---|---|---|---|
size | number | 40 | Diameter applied to every child. |
max | number | β | Show this many, then a +N chip. |
spacing | number | size * 0.35 | Overlap between adjacent avatars in px. |
ring | string | '#fff' | Separator ring around each avatar. |
ringWidth | number | 2 | Ring width in px. |
shape | 'circle' | 'rounded' | 'square' | 'circle' | Outline shape applied to every child. |
Helpers β
The pure functions behind the component are exported for reuse:
import { initialsFromName, colorFromName } from '@whereq/react'
initialsFromName('Ada Lovelace') // 'AL'
initialsFromName('εΌ δΌ') // 'εΌ δΌ'
colorFromName('Ada Lovelace') // a stable palette hexNotes β
- Accessibility β when an image is shown, the
<img>carries thealt; otherwise the wrapper is exposed asrole="img"witharia-label={name}, and the initials arearia-hidden. - SSR-safe β the component is pure inline styles with no stylesheet injection or DOM access, so it renders identically on the server and hydrates cleanly.
- Image errors β a failed load flips to the initials/fallback, and the state resets automatically when
srcchanges (safe for reused list rows).