forty-cdk
llms.txt

Primitives

Avatar

A user image with a graceful fallback across its loading lifecycle.

forty-cdk/avatar

The image loads, so data-status settles on loaded and the initials never render: fallbackDelayMs holds the fallback back for 500 ms, so a fast load never flashes it. Failed load below breaks the URL to show the error path.

Ada Lovelace

It is headless and presentational: it tracks the load lifecycle of an <img> and lets you choose what to show while loading or after an error. There is no WAI-ARIA pattern for avatars, so the directive imposes no role of its own.

Anatomy

<span forAvatar #avatar="forAvatar">
  <img forAvatarImage [src]="src" [alt]="name" />
  <!-- rendered only when avatar.shouldShowFallback() is true -->
  <span forAvatarFallback>{{ initials }}</span>
</span>

Examples

Failed load

When the image errors, the directive flips shouldShowFallback() and the initials render in its place. An error shows the fallback at once, skipping the fallbackDelayMs wait.

GH

API

ForAvatar

PropertyTypeDescription
fallbackDelayMs
input
ms to wait before shouldShowFallback() flips to true while idle/loading.
Default: 0
status
Signal
Read-only current status.
Default: —
shouldShowFallback
Signal
true when the consumer should render the fallback. Drives @if.
Default: —
Data attributeValues
data-statusidle | loading | loaded | error

ForAvatarImage

PropertyTypeDescription
(loadStatusChange)
output
Output. Emits whenever the lifecycle transitions.
Default: —
Data attributeValues
data-statusidle | loading | loaded | error

ForAvatarFallback

Data attributeValues
data-statusidle | loading | loaded | error

Scoped defaults

provideForAvatarDefaults configures avatar defaults for an injector subtree, either at the application root or in any component's providers array. Partial overrides inherit unspecified keys from the parent scope (or the library fallbacks at the root).

KeyLibrary fallbackMeaning
fallbackDelayMs0Milliseconds to wait before the fallback shows while the image is idle or loading, for avatars that don't set fallbackDelayMs themselves. An error status always shows the fallback at once.

Per-instance inputs always win over the scope defaults.

import { type ApplicationConfig } from '@angular/core';
import { provideForAvatarDefaults } from 'forty-cdk/defaults';

export const appConfig: ApplicationConfig = {
  providers: [provideForAvatarDefaults({ fallbackDelayMs: 500 })],
};

Accessibility

The directive does not impose a role. Pair the avatar with visible name text or aria-label on the surrounding element when identity matters. Set alt="" on the <img> for purely decorative avatars next to a name, or provide a meaningful alt description if the avatar stands alone.

A meaningful alt names the avatar only while the image is shown: once it fails, the image is hidden and the fallback initials are all that is left to announce. When an avatar stands alone, name the root instead (role="img" with an aria-label) and give the <img> alt="", so the fallback carries the same name as the loaded image.

Styling

forty-cdk ships no styles: put your own class on each piece and key your CSS off the data-* attributes listed under API, not off the for* selectors (Styling forty-cdk explains why).

.avatar-image:not([data-status='loaded']) {
  display: none;
}
.avatar-fallback[data-status='error'] {
  color: #b00020;
}

Behavior notes

  • Cached images are detected on first render. If the browser already has the image cached, load/error may not fire. The directive checks <img>.complete and naturalWidth after the first render and reports loaded / error accordingly. A cached image that is complete but has zero intrinsic width (e.g. an SVG without explicit dimensions) is ambiguous, so the directive stays loading and confirms validity with img.decode() rather than pessimistically flagging error.
  • Multiple images per avatar are not supported. Each [forAvatar] expects exactly one [forAvatarImage]. If you need cascading sources (CDN → fallback URL → fallback content), swap src on a single image.
  • alt is consumer territory. Because <img> is the host element, the consumer keeps full control of alt. Set "" for purely decorative avatars next to a name, or describe the person if the avatar stands alone.
  • The image stays in the DOM. Hide it via CSS [data-status="loading"], [data-status="error"] { display: none } if your consumer-side styling needs it gone. The fallback uses @if, so it only mounts when needed.

Wrapping in a design system

Subclass the root and re-provide FOR_AVATAR_CONTEXT with useExisting pointing at the subclass, since Angular does not inherit a directive's providers; Wrapping non-form roots walks the pattern.