Avatar unstable
A user's picture with a fallback, such as initials or an icon, while the picture loads or if it fails.
Avatar is a headless component: it loads the image and waits out the
fallback delay, so it needs an instance.
Anatomy
| Part | Element | Behavior |
|---|---|---|
root |
<span> |
Holds the image and the fallback. |
image |
<img> |
Shown once the current source loads. |
fallback |
<span> |
Shown after the delay if the image isn't loaded. |
Place the image and fallback inside the root. Each part takes an element
builder and returns it, or undefined if the part is hidden. Use
fallback(h.span)?.children(...) to supply the fallback content.
Give the image an alt and the fallback text or a label for its icon.
Loading behavior
Set the image source and fallbackDelay on the avatar. The avatar loads
the source through HtmlImages.Images before the image part is rendered, so
loading="lazy" on that part does not defer the request. The delay
defaults to zero.
Use imageLoadingStatus in the view to style the parts. A source starts
as "loading" (including on the server), then becomes "loaded" or
"error". A missing source is "error" immediately. The parts switch
without enter or exit transitions.
Data attributes
None.
Keyboard
None: an avatar isn't interactive.
Example A user's picture, with their initials until it loads
import { Html, HtmlServer } from "effect/ui/html"import { Avatar } from "effect/ui/kit"import { Effect, Schema } from "effect"
const Profile = Html.component({ name: "Profile", model: Schema.Null, message: Schema.Never}).make({ init: () => null, update: ($) => $.model, view: (h) => Avatar(h, { props: { src: "/ada.png" }, view: (avatar) => avatar.root(h.span.class("avatar")).children( avatar.image(h.img.attr("alt", "Ada Lovelace")), avatar.fallback(h.span.class("initials"))?.children("AL") ) })})
// The server can't load the image, so it draws the fallback.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Profile, {}))if (!html.includes(`<span class="initials">AL</span>`) || html.includes("<img")) { throw new Error("the server didn't draw the fallback in place of the image")}Components
A headless avatar. Call it with the consumer's h and a view closure
that draws the root with avatar.root(h.span), with the image,
avatar.image(h.img), and the fallback, avatar.fallback(h.span)?.children(...),
as its children.
Details
The avatar loads the image through HtmlImages.Images, and the image shows
once it has loaded. Until then, or when it fails, the fallback shows,
after fallbackDelay. On the server, and on a host without a loader,
the image never loads, so the fallback shows. A TestHost test settles
the load with TestImages from effect/ui/html/testing.
The image's own attributes win over the consumer's, set before the part
or chained after it: src, srcset, sizes, crossorigin and
referrerpolicy, including the ones the props leave out. The image
writes them when it's built, so the shown image is the one the avatar
loaded. They come after the consumer's attributes, with src last,
because a browser may start fetching as soon as src is set.
Signature
declare const Avatar: Headless<{ readonly crossOrigin?: "anonymous" | "use-credentials"; readonly fallbackDelay?: number; readonly referrerPolicy?: string; readonly sizes?: string; readonly src?: string; readonly srcSet?: string;}, never, { fallback: <Msg, R = never>(element: AvatarSpan<Msg, R>) => AvatarSpan<Msg, R> | undefined; image: <Msg, R = never>(element: AvatarImg<Msg, R>) => AvatarImg<Msg, R> | undefined; imageLoadingStatus: AvatarImageLoadingStatus; root: <Msg, R = never>(element: AvatarSpan<Msg, R>) => AvatarSpan<Msg, R>;}, never, never, never, never, never, never, { readonly delayElapsed: boolean; readonly report: { readonly source: { readonly crossOrigin?: "anonymous" | "use-credentials"; readonly referrerPolicy?: string; readonly sizes?: string; readonly src?: string; readonly srcSet?: string; }; readonly status: "error" | "loaded" | "loading"; } | null;}, { readonly _tag: "ElapsedFallbackDelay";} | { readonly _tag: "ChangedImageStatus"; readonly source: { readonly crossOrigin?: "anonymous" | "use-credentials"; readonly referrerPolicy?: string; readonly sizes?: string; readonly src?: string; readonly srcSet?: string; }; readonly status: "error" | "loaded" | "loading";}, QueryEntries<{}, {}>>Models
Where the avatar's image stands: "loading" until it settles, then
"loaded" or "error". An image without a src or a srcSet is
"error".
Signature
type AvatarImageLoadingStatus = "loading" | "loaded" | "error"An <img> the avatar's image accepts, from the consumer's h.img.
Signature
type AvatarImg<Msg, R = never> = Html.VoidElement<Msg, R, Html.EventsOf<"img">, HTMLImageElement, "img">The props of an Avatar.
Signature
type AvatarProps = typeof AvatarProps.TypeA <span> the avatar's root and fallback accept, from the consumer's
h.span.
Signature
type AvatarSpan<Msg, R = never> = Html.Element<Msg, R, Html.EventsOf<"span">, HTMLSpanElement, "span">Schemas
AvatarProps
unstableThe image source and fallback delay for an Avatar.
Details
src,srcSet,sizes,crossOriginandreferrerPolicy: the image, as the<img>attributes of the same names. The avatar loads it, and the image part sets them. Without asrcor asrcSet, the status is"error"immediately.fallbackDelay: how long, in milliseconds, the fallback waits before it shows, so a fast image doesn't flash it.0by default. Once it has passed, the fallback shows whenever the image isn't loaded.
Signature
declare const AvatarProps: Struct<{ readonly crossOrigin: optional<Literals<readonly ["anonymous", "use-credentials"]>>; readonly fallbackDelay: optional<Number>; readonly referrerPolicy: optional<String>; readonly sizes: optional<String>; readonly src: optional<String>; readonly srcSet: optional<String>;}>