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.
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>Use the React primitive adapter when Avatar state participates in React rendering.
import { Avatar } from "@starwind-ui/react/avatar";
export function Example() { return ( <Avatar.Root> <Avatar.Image alt="Starwind UI" src="/avatar.png" /> <Avatar.Fallback>SW</Avatar.Fallback> </Avatar.Root> );}Use the Vue components to build Avatar.
<script setup lang="ts">import Avatar from "@starwind-ui/vue/avatar";</script>
<template> <Avatar.Root> <Avatar.Image alt="Starwind UI" src="/avatar.png" /> <Avatar.Fallback>SW</Avatar.Fallback> </Avatar.Root></template>Use the Svelte components to build Avatar.
<script lang="ts"> import { Avatar } from "@starwind-ui/svelte/avatar";</script>
<Avatar.Root> <Avatar.Image alt="Starwind UI" src="/avatar.png" /> <Avatar.Fallback>SW</Avatar.Fallback></Avatar.Root>Render the Avatar data-sw-* contract yourself, then initialize createAvatar.
<span data-sw-avatar> <img data-sw-avatar-image style="visibility: hidden" alt="Starwind UI" src="/avatar.png" /> <span data-sw-avatar-fallback hidden>SW</span></span>
<script type="module"> import { createAvatar } from "@starwind-ui/runtime/avatar";
const root = document.querySelector("[data-sw-avatar]"); if (root) { createAvatar(root); }</script>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
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
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-changesetImageLoadingStatus
- 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-statusstarwind:loading-status-changesetImageLoadingStatus
Data Attributes
Runtime hooks
State
Image
The image element managed by Avatar.
- Default element
- img
- Discovery hook
- data-sw-avatar-image
- Role
- -
Props
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-changesetImageLoadingStatus
- 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-statusstarwind:loading-status-changesetImageLoadingStatus
State
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-changesetImageLoadingStatus
- 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-statusstarwind:loading-status-changesetImageLoadingStatus
Data Attributes
Runtime hooks
State
Fallback
Fallback content shown when Avatar cannot load.
- Default element
- span
- Discovery hook
- data-sw-avatar-fallback
- Role
- -
Props
delay number -
- Description
- Sets how long to wait before showing fallback content.
- Kind
- option
- Targets
- fallback
- Full type
- number
State
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-changesetImageLoadingStatus
- 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-statusstarwind:loading-status-changesetImageLoadingStatus
Data Attributes
Runtime hooks
State
Metadata
Runtime API
- Factory
createAvatar- Import
@starwind-ui/runtime/avatar- Root hook
- root
data-sw-avatar - Option props
- -
Related Styled Components
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.