Avatar
v0.0.10
mix mishka.ui.gen.headless avatar
Overview of the Phoenix and LiveView Headless Avatar Component
The Mishka Chelekom Headless Avatar is an image with a text fallback for Phoenix LiveView, with no styling of its own. Its Avatar hook tracks the image's loading status and keeps it on the root as data-status: idle when there is no src, then loading, then loaded or error. The image stays hidden until it has loaded. The fallback, usually initials, shows whenever the image is not loaded, after an optional delay so a fast image never flashes it.
The root takes class, the image takes image_class and the fallback takes fallback_class, so you style each part with your own Tailwind classes.
Base UI Avatar example (Phoenix headless)
The first avatar loads a photo. The second has no src, so it shows its fallback straight away.
Base UI Avatar example (Phoenix headless)
<.avatar
id="baseui-avatar-hero-1"
src="https://images.unsplash.com/photo-1543610892-0b1f7e6d8ac1?w=128&h=128&dpr=2&q=80"
alt="Lisa Turner"
width="48"
height="48"
delay={600}
class="inline-flex size-8 items-center justify-center overflow-hidden rounded-full bg-neutral-200 align-middle text-sm leading-none font-normal text-neutral-950 select-none dark:bg-neutral-800 dark:text-white"
image_class="size-full object-cover"
fallback_class="flex size-full items-center justify-center text-sm"
>
LT
</.avatar>
<.avatar
id="baseui-avatar-hero-2"
class="inline-flex size-8 items-center justify-center overflow-hidden rounded-full bg-neutral-200 align-middle text-sm leading-none font-normal text-neutral-950 select-none dark:bg-neutral-800 dark:text-white"
fallback_class="flex size-full items-center justify-center"
>
LT
</.avatar>Avatar example - image that fails to load (Phoenix headless)
The src below points at a file that does not exist. The request fails, the hook sets data-status="error", the image stays hidden and the fallback stays on screen.
Avatar example - image that fails to load (Phoenix headless)
<.avatar
id="avatar-missing-image"
src="/images/missing-avatar.png"
alt="Sara Hosseini"
class="inline-flex size-8 items-center justify-center overflow-hidden rounded-full bg-neutral-200 align-middle text-sm leading-none font-normal text-neutral-950 select-none dark:bg-neutral-800 dark:text-white"
image_class="size-full object-cover"
fallback_class="flex size-full items-center justify-center text-sm"
>
SH
</.avatar>Attributes and slots
id is required. src is the image address. Without it only the fallback renders, and the status stays idle. alt is the image's alternative text and defaults to an empty string, which marks the image as decorative. width and height go to the img as they are, and so do crossorigin and referrer_policy, which becomes referrerpolicy.
delay is the number of milliseconds the fallback waits before it appears. on_loading_status_change is a LiveView event pushed with %{"status" => status} on every status change, to the LiveView or LiveComponent that renders the avatar. Any global attribute, such as aria-label, lands on the root.
The default slot is the fallback. It is drawn only when the slot has content.
Parts & data attributes
Each part carries a stable data-part and a structural chelekom-avatar__* class you can target, plus a status attribute on the root that drives the reveal and the fallback.
| Part | data-part | State / ARIA | Purpose |
|---|---|---|---|
| Root | - | id (required), data-status (idle, loading, loaded, error) | Carries the Avatar hook and the current loading status; takes class. |
| Image | image | alt | The image element, hidden until it has finished loading; takes image_class. |
| Fallback | fallback | data-delay | Shown until the image loads; data-delay (ms) defers its appearance to avoid a flash; takes fallback_class. |
Accessibility
There is no formal WAI-ARIA APG pattern for avatars. The image has an empty alt by default, which marks it as decorative. Pass a meaningful alt when the avatar conveys information, for example a name that is not shown elsewhere on the page.
Attributes
| Name | Type | Default | Values | Required | Description |
|---|---|---|---|---|---|
id
|
string
| — | — | Yes | Unique id (required by the hook) |
src
|
string
| — | — | No | Image source; when nil only the fallback renders |
alt
|
string
| — | — | No | Alternative text for the image |
width
|
any
| — | — | No | Image width attribute |
height
|
any
| — | — | No | Image height attribute |
referrer_policy
|
string
| — | — | No | referrerpolicy for the <img> |
crossorigin
|
string
| — | — | No | crossorigin for the <img> |
delay
|
integer
| — | — | No | ms to wait before showing the fallback (avoids a flash) |
on_loading_status_change
|
string
| — | — | No | LiveView event pushed on status change ({status}) |
class
|
any
| — | — | No | Extra classes for the root |
image_class
|
any
| — | — | No | Extra classes for the image part |
fallback_class
|
any
| — | — | No | Extra classes for the fallback part |
rest
|
global
| — | — | No | — |
Slots
| Name | Required | Description |
|---|---|---|
inner_block
| No | Fallback content shown until the image loads (e.g. initials) |
Options
Each option takes one or more of its values, separated by commas.
| Option | Values |
|---|---|
--type
|
avatar
|
--module
| A module name of your own |
What it generates
mix mishka.ui.gen.headless avatar