Avatar
An image of a person, with a fallback while it loads or if it can't.
Reka UI Avatar
<script setup lang="ts">
import * as Avatar from '@/components/ui/avatar';
</script>
<template>
<div class="row">
<Avatar.Root>
<Avatar.Image src="/avatar.svg" alt="Profile picture" />
<Avatar.Fallback>JB</Avatar.Fallback>
</Avatar.Root>
<Avatar.Root>
<Avatar.Image src="/missing.png" alt="Missing picture" />
<Avatar.Fallback>CN</Avatar.Fallback>
</Avatar.Root>
<Avatar.Root size="sm"><Avatar.Fallback>SM</Avatar.Fallback></Avatar.Root>
<Avatar.Root size="lg"><Avatar.Fallback>LG</Avatar.Fallback></Avatar.Root>
</div>
</template>
<style scoped>
.row {
display: flex;
align-items: center;
gap: var(--dui-space-3);
}
</style>
Group
<script setup lang="ts">
import * as Avatar from '@/components/ui/avatar';
</script>
<template>
<Avatar.Group>
<Avatar.Root>
<Avatar.Image src="/avatar.svg" alt="Ada" />
<Avatar.Fallback>AD</Avatar.Fallback>
</Avatar.Root>
<Avatar.Root><Avatar.Fallback>BO</Avatar.Fallback></Avatar.Root>
<Avatar.Root><Avatar.Fallback>CY</Avatar.Fallback></Avatar.Root>
</Avatar.Group>
</template>
Installation
npx distill-ui add avatarIt installs reka-ui if you don't have it yet.
- Set up the tokens once, if you haven't: see manual installation.
- Install the packages it needs:
npm install reka-ui@2.11.0 - Copy these files into your project:
<script setup lang="ts">
import { AvatarRoot } from 'reka-ui';
const { size = 'md' } = defineProps<{ size?: 'sm' | 'md' | 'lg' }>();
</script>
<template>
<AvatarRoot as-child>
<span class="avatar" :data-size="size"><slot /></span>
</AvatarRoot>
</template>
<!--
Options you can set from a parent or on the avatar itself:
--dui-avatar-size, --dui-avatar-radius
-->
<style scoped>
.avatar {
position: relative;
display: flex;
flex-shrink: 0;
width: var(--dui-avatar-size, 2rem);
height: var(--dui-avatar-size, 2rem);
/* Set by Avatar.Group so stacked avatars overlap with a gap ring. */
margin-inline-start: var(--avatar-overlap, 0);
border-radius: var(--dui-avatar-radius, var(--dui-radius-full));
box-shadow: 0 0 0 var(--avatar-ring-width, 0) var(--dui-color-background);
font-size: var(--dui-text-sm);
user-select: none;
&:first-child {
margin-inline-start: 0;
}
/* A faint edge so light images don't blend into the page. */
&::after {
content: '';
position: absolute;
inset: 0;
border: 1px solid color-mix(in oklch, var(--dui-color-foreground) 10%, transparent);
border-radius: inherit;
pointer-events: none;
}
}
.avatar[data-size='sm'] {
width: var(--dui-avatar-size, 1.5rem);
height: var(--dui-avatar-size, 1.5rem);
font-size: var(--dui-text-xs);
}
.avatar[data-size='lg'] {
width: var(--dui-avatar-size, 2.5rem);
height: var(--dui-avatar-size, 2.5rem);
}
</style>
<script setup lang="ts">
import { AvatarFallback } from 'reka-ui';
// Shown until the image loads, and for good if it fails.
const { delayMs } = defineProps<{ delayMs?: number }>();
</script>
<template>
<AvatarFallback :delay-ms="delayMs" as-child>
<span class="fallback"><slot /></span>
</AvatarFallback>
</template>
<!--
Options you can set from a parent or on the avatar fallback itself:
--dui-avatar-fallback-bg
-->
<style scoped>
.fallback {
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
border-radius: inherit;
background: var(--dui-avatar-fallback-bg, var(--dui-color-muted));
/* Pulled toward the foreground so initials pass AA contrast on the muted fill. */
color: var(
--dui-avatar-fallback-fg,
color-mix(in oklch, var(--dui-color-muted-foreground) 70%, var(--dui-color-foreground))
);
}
</style>
<template>
<div class="group"><slot /></div>
</template>
<style scoped>
.group {
/* Read by each Avatar inside: overlap them and ring each one in the page color. */
--avatar-overlap: calc(var(--dui-space-2) * -1);
--avatar-ring-width: 2px;
display: flex;
}
</style>
<script setup lang="ts">
import { AvatarImage } from 'reka-ui';
// Reka only shows the image once it has loaded.
const { src, alt } = defineProps<{ src: string; alt: string }>();
</script>
<template>
<AvatarImage :src="src" as-child>
<img class="image" :src="src" :alt="alt" />
</AvatarImage>
</template>
<style scoped>
.image {
width: 100%;
height: 100%;
aspect-ratio: 1;
border-radius: inherit;
object-fit: cover;
}
</style>
export { default as Root } from './Avatar.vue';
export { default as Image } from './AvatarImage.vue';
export { default as Fallback } from './AvatarFallback.vue';
export { default as Group } from './AvatarGroup.vue';
Customizing
Set any of these on the component or on a parent element. See Customizing for how they work.
| Custom property | Default |
|---|---|
--dui-avatar-fallback-bg | var(--dui-color-muted) |
--dui-avatar-size | 2rem |
--dui-avatar-radius | var(--dui-radius-full) |