Avatar
An avatar component that is easy to style.
<script setup lang="ts">
import { Avatar } from '@vase-ui/vue/avatar';
import styles from './index.module.css';
</script>
<template>
<div style="display: flex; gap: 1rem">
<Avatar.Root :class="styles.Root">
<Avatar.Image
src="https://images.unsplash.com/photo-1543610892-0b1f7e6d8ac1?w=128&h=128&dpr=2&q=80"
width="48"
height="48"
:class="styles.Image"
/>
<Avatar.Fallback :delay="600" :class="styles.Fallback">LT</Avatar.Fallback>
</Avatar.Root>
<Avatar.Root :class="styles.Root">LT</Avatar.Root>
</div>
</template>.Root {
display: inline-flex;
justify-content: center;
align-items: center;
vertical-align: middle;
border-radius: 100%;
-webkit-user-select: none;
user-select: none;
font-weight: 400;
color: oklch(14.5% 0 0deg);
background-color: oklch(92.2% 0 0deg);
font-size: 0.875rem;
line-height: 1;
overflow: hidden;
height: 2rem;
width: 2rem;
@media (prefers-color-scheme: dark) {
color: white;
background-color: oklch(26.9% 0 0deg);
}
}
.Image {
object-fit: cover;
height: 100%;
width: 100%;
}
.Fallback {
align-items: center;
display: flex;
justify-content: center;
height: 100%;
width: 100%;
font-size: 0.875rem;
}<script setup lang="ts">
import { Avatar } from '@vase-ui/vue/avatar';
</script>
<template>
<div class="flex gap-4">
<Avatar.Root
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"
>
<Avatar.Image
src="https://images.unsplash.com/photo-1543610892-0b1f7e6d8ac1?w=128&h=128&dpr=2&q=80"
width="48"
height="48"
class="size-full object-cover"
/>
<Avatar.Fallback :delay="600" class="flex size-full items-center justify-center text-sm"
>LT</Avatar.Fallback
>
</Avatar.Root>
<Avatar.Root
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"
>LT</Avatar.Root
>
</div>
</template>Anatomy
Import the component and assemble its parts:
<script setup lang="ts">
import { Avatar } from '@vase-ui/vue/avatar';
</script>
<template>
<Avatar.Root>
<Avatar.Image src="" />
<Avatar.Fallback>LT</Avatar.Fallback>
</Avatar.Root>
</template>Optimized and lazy-loaded images
By default, <Avatar.Image> loads src with an image outside the document, and it renders the <img> only after the picture loads. An image component that serves another URL than src thus does not work, and loading="lazy" has no effect.
Add the keepMounted prop to render the image element at once and let it load in place. The browser then requests only the image that it shows. Put an image component or your own <img> in the element slot, with v-bind="props" and :ref="elementRef":
<script setup lang="ts">
import { Avatar } from '@vase-ui/vue/avatar';
const photo = 'https://images.unsplash.com/photo-1543610892-0b1f7e6d8ac1';
</script>
<template>
<Avatar.Root>
<Avatar.Fallback>LT</Avatar.Fallback>
<Avatar.Image keep-mounted alt="" width="48" height="48" loading="lazy">
<template #element="{ props, elementRef }">
<img
v-bind="props"
:ref="elementRef"
:srcset="`${photo}?w=48&h=48 1x, ${photo}?w=96&h=96 2x`"
:src="`${photo}?w=48&h=48`"
/>
</template>
</Avatar.Image>
</Avatar.Root>
</template>Stacking
With keepMounted, the image and the fallback are both present until the image loads. Until then, the image is hidden from assistive technology, so the fallback gives the accessible name.
Put the two in the same box. Put <Avatar.Image> after <Avatar.Fallback>. Both are positioned, so the later one in the DOM paints on top. The fallback then shows until the image covers it.
While an image loads, it paints nothing, so the fallback shows through it. An image that failed to load paints a broken-image icon on top of the fallback. Hide the image in these two states 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;
}Do not use display: none or the hidden attribute here. In Chromium, Firefox and WebKit, a lazy image with no box requests no picture. visibility and opacity do not stop lazy loading.
Server rendering
By default, the server HTML has no image. With keepMounted, the image is in the server HTML and starts to load before the hydration. The fallback is also in the server HTML, and it stays visible until the hydration reads the loading status.
Examples
Animate the image
Put <Avatar.Image> in a <Transition> to animate its enter and its leave. Vue keeps an image that leaves in the DOM beside the fallback until its leave ends. Take that image out of the flow with CSS:
<script setup lang="ts">
import { Avatar } from '@vase-ui/vue/avatar';
</script>
<template>
<Avatar.Root class="avatar">
<Transition>
<Avatar.Image src="/avatar.png" width="48" height="48" />
</Transition>
<Avatar.Fallback>LT</Avatar.Fallback>
</Avatar.Root>
</template>
<style scoped>
.avatar {
position: relative;
display: inline-flex;
}
.v-enter-active,
.v-leave-active {
transition: opacity 150ms;
}
.v-enter-from,
.v-leave-to {
opacity: 0;
}
/* Vue keeps the leaving image in the DOM until its leave ends. */
.v-leave-active {
position: absolute;
}
</style>A <Transition> around <Avatar.Fallback> needs the same CSS. <Transition> does not animate an image in the element slot, or an image that keepMounted keeps in the DOM. For such an image, use keepMounted and a CSS transition on [data-loading].
API reference
Avatar.Root
Displays a user's profile picture, initials, or fallback icon.
Renders a <span> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: AvatarRootState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: AvatarRootState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLSpanElement | null | The element of the part, or null while it is not mounted. |
Avatar.Image
The image to be displayed in the avatar. It has alt="" when you give no alt. Give an alt
when no text beside the avatar names the person.
Renders an <img> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
crossorigin | "anonymous" | "use-credentials" | "" | undefined | The CORS setting of the image request, as in the crossorigin attribute of <img>. | |
keepMounted | boolean | undefined | false | Whether the image element stays in the DOM and loads the picture in place. Supports
loading="lazy" and an image component that renders its own <img>. Until the picture loads,
the image has aria-hidden="true", so the fallback names the avatar. |
referrerpolicy | ReferrerPolicy | undefined | The referrer policy of the image request, as in the referrerpolicy attribute of <img>. | |
sizes | string | undefined | The display sizes of the image, as in the sizes attribute of <img>. | |
src | string | undefined | The URL of the image. | |
srcset | string | undefined | The candidate sources of the image, as in the srcset attribute of <img>. |
Slots
| Slot | Slot props | Description |
|---|---|---|
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: AvatarRootState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Events
| Event | Arguments | Description |
|---|---|---|
loadingStatusChange | [status: ImageLoadingStatus] | Callback fired when the loading status changes. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLImageElement | null | The element of the part, or null while it is not mounted. |
Selectors
| Selector | Description |
|---|---|
[data-loading] | The picture loads, and keepMounted keeps the image in the DOM. |
[data-error] | The picture failed to load, and keepMounted keeps the image in the DOM. |
Avatar.Fallback
Rendered when the image fails to load or when no image is provided.
Renders a <span> element.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
delay | number | undefined | 0 | How long to wait before showing the fallback. Specified in milliseconds. |
as | keyof HTMLElementTagNameMap | Component | GenericComponent | undefined | A tag or a component that the part renders in place of its own element, with the same
attributes and content. The element slot wins over it. |
Slots
| Slot | Slot props | Description |
|---|---|---|
default | state: AvatarRootState | The content of the part. |
element | props: Record<string, unknown>elementRef: (target: unknown) => voidstate: AvatarRootState | Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref. |
Exposed
| Member | Type | Description |
|---|---|---|
element | HTMLSpanElement | null | The element of the part, or null while it is not mounted. |
ImageLoadingStatus
The loading status of the image.
'idle' | 'loading' | 'loaded' | 'error'
Differences from Base UI
Vase UI ports Avatar from Base UI 1.9.0.
API
| Base UI | Vase UI |
|---|---|
Avatar.Image | <Transition> around Avatar.Image gives the enter and the leave with its classes and hooks, so the state has no transitionStatus, and the image has no data-starting-style and no data-ending-style. With keepMounted, a CSS transition on [data-loading] animates the image. |
The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.