- Owner
- Maintainer
- Reviewer
- Automation
GhostAvatar
GhostAvatar shows a person or entity as a photo, with initials or slot content as the fallback when the photo is missing or fails to load. Import it from @ghostvue/themes for the default circular treatment. To style your own avatar, see Advanced: headless composition.
Team roster
Member list showing a loaded photo, initials after a failed or missing photo, and an icon fallback, with role badges.
Each row pairs a themed GhostAvatar from @ghostvue/themes with the member's name and a role GhostBadge. The four rows cover each fallback case:
- Ada Lovelace has a photo that loads, so the avatar swaps to the
<img>. - Grace Hopper has a photo URL that fails, so the initials stay.
- Alan Turing has no photo, so the initials show from the first paint.
- Release bot uses the
fallbackslot to show an icon instead of initials.
Rows stack on narrow previews and align on one line from 24rem, using @container ghost-demo.
Initials
Provide name to derive fallback initials, up to two characters ("Alan Turing" becomes "AT"). With no src, the fallback is shown immediately and the root has no data-state.
Loaded image
When src is set, the server and the first client paint show the fallback with data-state="loading". After mount, the component probes the URL. It swaps to the <img> and sets data-state="loaded" when the load succeeds, or keeps the fallback and sets data-state="error" when it fails, as for Grace Hopper above.
Set alt for the image's accessible name, or rely on name. Use alt="" when visible text, a parent link or a heading already names the person. The roster prints each name beside its avatar, so it marks the whole avatar aria-hidden="true" with alt="". That way neither the photo nor the fallback initials are read before the name.
Custom fallback
Use the fallback slot when initials are not enough, such as for a service account or a team. Hide decorative icon content with aria-hidden="true" and keep a real name nearby, as the release bot row does. If no visible name is nearby, put the name in visually hidden text.
Sizes
Size hooks on the @ghostvue/themes wrapper.
@ghostvue/themes adds these to the headless GhostAvatar API, which it forwards:
| Prop | Type | Default | Description |
|---|---|---|---|
size | 'sm' | 'md' | 'lg' | "md" | Square dimensions. Default omits data-size. |
Default md omits data-size.
Accessibility
The loaded <img> uses alt. Fallback initials are ordinary text. Do not add aria-label on the root when text or alt already names the entity. Wrap the avatar in a link or button when it should activate something.
SSR
SSR and hydration render the fallback first when src is set, avoiding mismatches from client-only load state. See avatar-spec.
When not to use it
- As a button or link. Wrap with
GhostButtonor a native<a>. - For presence or counts. Compose badges outside the avatar.
Advanced: headless composition
For teams that bring their own design system, the headless GhostAvatar from @ghostvue/vue has the same image probe and fallback behavior with no visuals. These examples render unstyled; size, shape and color are your CSS.
Headless initials
Fallback initials from name without an image URL.
Headless image
Client image probe after SSR-safe loading fallback.
Headless fallback slot
Custom fallback when src is missing or fails to load.
Composable
useGhostAvatar returns rootProps, imageProps, initials, showImage and showFallback for a host you render yourself. The GhostAvatar reference page renders the avatar/composable example with its source.
deriveGhostAvatarInitials(name) is exported for tests and custom hosts.
Styling
- class
ghost-avatar data-part:root,image,fallbackdata-state:loading,loaded,error(omitted withoutsrc)- themed
data-size:sm,md,lg
API
| Prop | Type | Default | Description |
|---|---|---|---|
alt | string | — | img accessible name; defaults to name. |
as | GhostAvatarElement | "span" | Root host. |
name | string | — | Initials source and default alt. |
src | string | — | Image URL. Load is confirmed after mount on the client. |
| Slot | Slot props | Description |
|---|---|---|
fallback | — | Shown when the image is not displayed. |
Template ref: element (HTMLElement | null).