Components
Avatar
An easily stylable avatar component.
Anatomy#
Import the component and assemble its parts:
import { Avatar } from '@egoist/solid-base/avatar';
<Avatar.Root>
<Avatar.Image src="" />
<Avatar.Fallback>LT</Avatar.Fallback>
</Avatar.Root>;Optimized and lazy-loaded images#
By default, <Avatar.Image> preloads src and renders the image only once it has loaded. This
doesn't compose with image optimizers, which serve a different URL than the raw src, or with
loading="lazy".
Add the keepMounted prop to render the image element right away and let it load in place. Only
the image that is actually displayed is requested:
<Avatar.Root>
<Avatar.Fallback>LT</Avatar.Fallback>
<Avatar.Image keepMounted src="/avatar.png" loading="lazy" width="32" height="32" />
</Avatar.Root>To use an image component (such as an image optimizer's), compose it with the render prop and
spread the props it receives:
<Avatar.Root>
<Avatar.Fallback>LT</Avatar.Fallback>
<Avatar.Image
keepMounted
render={(props) => <Image {...props} src="/avatar.png" width={32} height={32} alt="" />}
/>
</Avatar.Root>Stacking#
With keepMounted, the image and the fallback are both present until the image loads. The image
is hidden from assistive technology until then, so the fallback provides the accessible name on
its own.
Stack the two in the same box, and place <Avatar.Image> after <Avatar.Fallback>. Both are
positioned, so whichever comes later in the DOM paints on top. The fallback then shows through
until the image covers it.
A loading image paints nothing, so the fallback shows through on its own. An image that failed to
load paints a broken-image icon on top of it. Hide the image in either state with the
data-loading and data-error attributes:
.Root {
position: relative;
}
.Image,
.Fallback {
position: absolute;
inset: 0;
}
.Image[data-loading],
.Image[data-error] {
visibility: hidden;
}Avoid display: none here: an element without a box never intersects the viewport, so
loading="lazy" would never fetch the image. visibility and opacity both keep lazy loading
working.
Server rendering#
With keepMounted, the image is part of the server-rendered HTML and starts loading before
hydration. So is the fallback, which stays visible until hydration resolves the loading status. A
cached image is displayed immediately, without an enter animation.
API reference#
<Avatar.Root>
Displays a user's profile picture, initials, or fallback icon. Renders a <span> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<Avatar.Image>
The image to be displayed in the avatar. Renders an <img> element.
onLoadingStatusChange((status: ImageLoadingStatus) => void)—Callback fired when the loading status changes.
keepMountedbooleanfalseWhether the image element stays mounted and loads in place instead of being preloaded. Supports loading="lazy".
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
data-loadingPresent while the image is loading.
data-errorPresent when the image failed to load.
<Avatar.Fallback>
Rendered when the image fails to load or when no image is provided. Renders a <span> element.
delaynumber0How long to wait before showing the fallback. Specified in milliseconds.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
Additional types#
ImageLoadingStatus is the status passed to onLoadingStatusChange:
type ImageLoadingStatus = 'idle' | 'loading' | 'loaded' | 'error';