DisplayReact
An easily stylable avatar: the person's picture, with initials or an icon in its place while the image loads or when it fails.
Rendered by React 19.3.1
Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Import the component and assemble its parts:
<Avatar.Root>
<Avatar.Image />
<Avatar.Fallback />
</Avatar.Root>Examples
The demo at the top is the one example this component has so far.
API reference
3 props across 3 parts, in React syntax. className, style, render are listed once, under Shared props.
FallbackAvatar.Fallback
Rendered when the image fails to load or when no image is provided. Renders a <span>.
| Prop | Type | Default |
|---|---|---|
delayHow long to wait before showing the fallback. | number | 0 |
ImageAvatar.Image
The image to be displayed in the avatar. Renders an <img>.
| Prop | Type | Default |
|---|---|---|
onLoadingStatusChangeCallback fired when the loading status changes. | ((status: ImageLoadingStatus) => void) | — |
keepMountedWhether the image element stays mounted and loads in place instead of being preloaded. | boolean | false |
| Data attribute | Description |
|---|---|
data-loading | Present while the image is loading. |
data-error | Present when the image failed to load. |
data-starting-style | Present when the image begins animating in. |
data-ending-style | Present when the image is animating out. |
RootAvatar.Root
Displays a user's profile picture, initials, or fallback icon. Renders a <span>.
Shared props
Every part that renders an element takes these.
| Prop | Type |
|---|---|
classNameCSS class applied to the element, or a function that
returns a class based on the component's state. | string | ((state: State) => string | undefined) |
styleStyle applied to the element, or a function that
returns a style object based on the component's state. | React.CSSProperties | ((state: State) => React.CSSProperties | undefined) |
renderAllows you to replace the component's HTML element
with a different tag, or compose it with another component. | React.ReactElement<unknown, string | React.JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, State> |
Keyboard and accessibility
Keyboard
Avatar takes no focus and has no keys of its own.
Accessibility
| Part | Sets | So that |
|---|---|---|
| Image | renders an <img>, with the alt you give it | the picture has a text alternative |
| Image | aria-hidden="true" until the image has loaded | a screen reader does not read an image that is not there |
| Fallback | renders a <span> with your content | initials or a label stand in when there is no image |
Focus order
- Avatar is not focusable; focus passes over it.