Avatar
Overview
The Avatar component represents a person or entity as a compact image. It falls back
automatically from a photo (src) to initials (from name/initials) to a custom icon, and
finally to a generic default icon — so you never have to hand-roll the broken-image or
missing-photo case.
Import
import { Avatar } from '@allxsmith/bestax-bulma';
Props
| Prop | Type | Default | Description |
|---|---|---|---|
src | string | — | Image URL. On load error (or if absent), falls back to initials, then icon. |
alt | string | — | Alternate text for the image (used for the accessible name in every render mode). |
name | string | — | Derives initials and a deterministic background color when no src is shown. |
initials | string | — | Explicit initials override (else derived from name). |
icon | React.ReactNode | — | Final fallback, rendered when there is no src, name, or initials. |
size | '16x16' | '24x24' | '32x32' | '48x48' | '64x64' | '96x96' | '128x128' | number | — | Preset size, or a pixel size when a number. |
shape | 'circle' | 'rounded' | 'square' | 'circle' | Avatar shape. |
color | 'primary' | 'link' | 'info' | 'success' | 'warning' | 'danger' | 'black' | 'dark' | 'light' | 'white' | — | Background color for initials/icon avatars (else auto-derived from name). |
as | React.ElementType | — | Element/component to render as. Defaults to 'a' when href is set, else 'figure'. |
href | string | — | When set, renders the avatar as a link. |
target | string | — | Anchor target — forwarded only when rendering a link (an a or a custom as component). |
rel | string | — | Anchor rel — forwarded only when rendering a link (an a or a custom as component). |
imageProps | React.ImgHTMLAttributes<HTMLImageElement> | — | Extra props forwarded to the underlying <img> (e.g. loading, crossOrigin); its onError is chained before the fallback fires. |
className | string | — | Additional CSS classes. |
| ... | All standard HTML and Bulma helper props | (See Helper Props) |
Usage
Photo with Automatic Fallback
A working photo renders as an image; if the src fails to load, Avatar swaps to initials
derived from name automatically — no broken-image icon.
<Avatars spaced> <Avatar src="https://github.com/allxsmith.png" name="Al Smith" size="64x64" /> <Avatar src="https://example.invalid/missing.jpg" name="Grace Hopper" size="64x64" /> </Avatars>
Initials
With no src, initials render on a deterministic auto background color derived from name.
<Avatars spaced> <Avatar name="Ada Lovelace" /> <Avatar name="Grace Hopper" /> <Avatar name="Katherine Johnson" /> </Avatars>
Icon Fallback
Pass an icon to control the final fallback when there is no photo, name, or initials.
<Avatar icon={<Icon name="user" />} color="info" shape="rounded" />
Shapes
The shape prop switches between a circle, a rounded square, and a plain square.
<Avatars spaced> <Avatar name="Circle" shape="circle" /> <Avatar name="Rounded" shape="rounded" /> <Avatar name="Square" shape="square" /> </Avatars>
Sizes
Preset sizes mirror Image's fixed-size list; a number renders a pixel size.
<Avatars spaced> <Avatar name="Ada Lovelace" size="24x24" /> <Avatar name="Ada Lovelace" size="32x32" /> <Avatar name="Ada Lovelace" size="48x48" /> <Avatar name="Ada Lovelace" size="64x64" /> <Avatar name="Ada Lovelace" size={20} /> </Avatars>
Clickable Avatar
Set href to render the avatar as a link (or pass as for a custom element).
<Avatar name="Ada Lovelace" href="https://bestax.io" />
Forwarding Props to the Image
imageProps is spread onto the underlying <img> — handy for native attributes like
loading, crossOrigin, or referrerPolicy. A custom onError is chained before the
automatic initials/icon fallback runs.
<Avatar src="https://github.com/allxsmith.png" name="Al Smith" size="64x64" imageProps={{ loading: 'lazy' }} />
Accessibility
- Image avatars use
alt(falling back toname) for their accessible name. - Initials/icon avatars expose
role="img"andaria-label(fromalt/name) — unless rendered as a link or button, where the native link/button role andaria-labelare used instead. - The default fallback icon is
aria-hidden.
Related Components
Avatars: An overlapping group ofAvatars with a "+N" surplus bubble.Badge: A status/count indicator that overlays anAvatar(or any element).Image: Bulma's fixed-ratio image container.- Helper Props: Bulma helper props for spacing, color, etc.