Skip to main content

Starwind UI v3.7 is here with stable Vue and Svelte, the new Chart component, and full RTL support. Read the release notes

Avatar Primitive

Use Avatar to show a person or account image. The fallback can show initials while the image loads or when it fails to load.

AM

Alex Morgan

Anatomy

Use the Astro primitive adapter to render Avatar anatomy with the Runtime wiring included.

---
import { Avatar } from "@starwind-ui/astro/avatar";
---
<Avatar.Root>
<Avatar.Image alt="Starwind UI" src="/avatar.png" />
<Avatar.Fallback>SW</Avatar.Fallback>
</Avatar.Root>

Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโ€™s types for the available props and events. Plain HTML uses DOM events and Runtime methods.

API Reference

Root

The main element that owns the Avatar Runtime instance.

Default element
span
Discovery hook
data-sw-avatar
Role
-

Events

Event Callback Value Toggle
loadingStatusChange onLoadingStatusChange status: AvatarImageLoadingStatus
Description
Fires when the image loading status changes for Avatar.
DOM event
starwind:loading-status-change
Details type
AvatarLoadingStatusChangeDetails
Timing
after-state-commit
Cancelable
No

State

State Type Props Toggle
imageLoadingStatus AvatarImageLoadingStatus -
Description
Tracks whether the Avatar image is loading, loaded, or failed.
Initial attribute
data-image-loading-status
Runtime getter
getImageLoadingStatus
Runtime setter
setImageLoadingStatus
React

The adapter coordinates this state without a dedicated state prop, and onLoadingStatusChange for state changes.

  • onLoadingStatusChange
Astro

listen for starwind:loading-status-change and call setImageLoadingStatus for later updates.

  • starwind:loading-status-change
  • setImageLoadingStatus
Runtime / HTML

Use data-image-loading-status for initial state, listen for starwind:loading-status-change, and call setImageLoadingStatus for later updates.

  • data-image-loading-status
  • starwind:loading-status-change
  • setImageLoadingStatus

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-avatar - Marks the Root part so Starwind Runtime can find it.
State
Attribute Value Description
data-image-loading-status - Reflects the image loading status state on the Root part.

Image

The image element managed by Avatar.

Default element
img
Discovery hook
data-sw-avatar-image
Role
-

Props

Prop Type Default Toggle
alt string -
Description
Provides accessible alternative text for the image.
Kind
attribute
Targets
image
Full type
string
image ImageMetadata -
Description
Provides image loading state to the avatar image part.
Kind
rendering
Targets
image
Full type
ImageMetadata
src string -
Description
Sets the image source.
Kind
attribute
Targets
image
Full type
string
onLoadingStatusChange (status: AvatarImageLoadingStatus, details: AvatarLoadingStatusChangeDetails) => void -
Description
Runs when on loading status change changes for Avatar.
Kind
callback
Targets
image
Full type
(status: AvatarImageLoadingStatus, details: AvatarLoadingStatusChangeDetails) => void
React

The adapter coordinates this state without a dedicated state prop, and onLoadingStatusChange for state changes.

  • onLoadingStatusChange
Astro

listen for starwind:loading-status-change and call setImageLoadingStatus for later updates.

  • starwind:loading-status-change
  • setImageLoadingStatus
Runtime / HTML

Use data-image-loading-status for initial state, listen for starwind:loading-status-change, and call setImageLoadingStatus for later updates.

  • data-image-loading-status
  • starwind:loading-status-change
  • setImageLoadingStatus

State

State Type Props Toggle
imageLoadingStatus AvatarImageLoadingStatus -
Description
Tracks whether the Avatar image is loading, loaded, or failed.
Initial attribute
data-image-loading-status
Runtime getter
getImageLoadingStatus
Runtime setter
setImageLoadingStatus
React

The adapter coordinates this state without a dedicated state prop, and onLoadingStatusChange for state changes.

  • onLoadingStatusChange
Astro

listen for starwind:loading-status-change and call setImageLoadingStatus for later updates.

  • starwind:loading-status-change
  • setImageLoadingStatus
Runtime / HTML

Use data-image-loading-status for initial state, listen for starwind:loading-status-change, and call setImageLoadingStatus for later updates.

  • data-image-loading-status
  • starwind:loading-status-change
  • setImageLoadingStatus

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-avatar-image - Marks the Image part so Starwind Runtime can find it.
State
Attribute Value Description
data-image-loading-status - Reflects the image loading status state on the Image part.

Fallback

Fallback content shown when Avatar cannot load.

Default element
span
Discovery hook
data-sw-avatar-fallback
Role
-

Props

Prop Type Default Toggle
delay number -
Description
Sets how long to wait before showing fallback content.
Kind
option
Targets
fallback
Full type
number

State

State Type Props Toggle
imageLoadingStatus AvatarImageLoadingStatus -
Description
Tracks whether the Avatar image is loading, loaded, or failed.
Initial attribute
data-image-loading-status
Runtime getter
getImageLoadingStatus
Runtime setter
setImageLoadingStatus
React

The adapter coordinates this state without a dedicated state prop, and onLoadingStatusChange for state changes.

  • onLoadingStatusChange
Astro

listen for starwind:loading-status-change and call setImageLoadingStatus for later updates.

  • starwind:loading-status-change
  • setImageLoadingStatus
Runtime / HTML

Use data-image-loading-status for initial state, listen for starwind:loading-status-change, and call setImageLoadingStatus for later updates.

  • data-image-loading-status
  • starwind:loading-status-change
  • setImageLoadingStatus

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-avatar-fallback - Marks the Fallback part so Starwind Runtime can find it.
State
Attribute Value Description
data-image-loading-status - Reflects the image loading status state on the Fallback part.
Metadata
Attribute Value Description
data-delay - Reflects the delay prop on the Fallback part.

Runtime API

Factory
createAvatar
Import
@starwind-ui/runtime/avatar
Root hook
root data-sw-avatar
Option props
-
Component Relationship
Avatar Direct primitive

Changelog

View version history v1.0.4 8 releases

v1.0.4

  • In Svelte, `onLoadingStatusChange` now runs before listeners on ancestor elements, as in the other frameworks.
  • In Vue, this component now needs Vue 3.5.21 or newer.

v1.0.3

  • In Astro, an attribute that you set on an Avatar part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.

v1.0.2

  • Replacing an avatar image or fallback now updates the displayed content. Removed images no longer affect its loading state.
  • Updated the shared setup file included with the Astro Primitive. Its public API and behavior are unchanged.

v1.0.1

  • Removed old comments from the Vue Primitive files that said the code was internal and not for release. The CLI also removes the blank line that these comments left in the Vue Primitive `index.ts` files that it copies into your project.

v1.0.0

  • This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.

v0.1.2

  • The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.

v0.1.1

  • Avatar images now work with the browser's lazy loading. Starwind still hides an image while it loads and when it fails to load. This includes Astro images from imported assets.

v0.1.0

  • First release. Avatar tracks image loading, can wait before it shows the fallback, and keeps the image status correct for screen readers.