Animated Avatar Group
Preview
<script setup lang="ts">
import { AvatarGroup } from 'beui-vue'
const members = [
['maya','Maya Chen','1494790108377-be9c29b29330'],['leo','Leo Hart','1507003211169-0a1dd7228f2d'],
['ines','Ines Duarte','1438761681033-6461ffad8d80'],['sam','Sam Okafor','1500648767791-00dcc994a43e'],
['noah','Noah Berg','1472099645785-5658abf4ff4e'],['aria','Aria Patel',''],
['zoe','Zoe Laurent','1534528741775-53994a69daeb'],['kai','Kai Moreno',''],
].map(([id, name, photo]) => ({ id: id!, name: name!, src: photo ? `https://images.unsplash.com/photo-${photo}?auto=format&fit=crop&w=96&h=96&q=80` : undefined }))
</script>
<template><AvatarGroup :members="members" :max="4" size="lg" label="Project members" /></template><script lang="ts">
export interface AvatarGroupMember {
id: string
name: string
/** Image URL. Without one, or if it fails to load, initials show. */
src?: string
}
export type AvatarSize = 'sm' | 'md' | 'lg'
/** English defaults for `AvatarGroup`. */
export const avatarGroupMessages = {
label: 'Members',
showFewer: 'Show fewer members',
showMore: 'Show {count} more members',
}
export type AvatarGroupMessages = typeof avatarGroupMessages
</script>
<script setup lang="ts">
import { AnimatePresence, motion } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { computed, shallowRef } from 'vue'
import { useMountedMedia } from '../core/mounted-media'
import { easeOut, layoutSpring, pressSpring } from '../core/motion'
import Avatar from './Avatar.vue'
import Tooltip from './tooltip/Tooltip.vue'
import TooltipProvider from './tooltip/TooltipProvider.vue'
const SIZES = {
sm: { text: '10px', px: 28 },
md: { text: '.75rem', px: 36 },
lg: { text: '.875rem', px: 44 },
} as const
const props = withDefaults(defineProps<{
members: readonly AvatarGroupMember[]
/** Avatars shown before the rest collapse into a +N count. */
max?: number
size?: AvatarSize
/** Whether every member is shown. Pair with `@update:expanded`. */
expanded?: boolean
defaultExpanded?: boolean
/** Accessible name for the list, such as "Project members". */
label?: string
/** Override any built-in string. `{count}` fills `showMore`. */
labels?: Partial<AvatarGroupMessages>
className?: string
}>(), { max: 4, size: 'md', defaultExpanded: false, expanded: undefined })
const emit = defineEmits<{ 'update:expanded': [value: boolean]; expandedChange: [value: boolean] }>()
const reduce = useReducedMotion()
// `useHoverCapable`: touch devices fire a phantom sticky `:hover` on tap, so the
// fan-apart only runs where a real pointer can leave.
const canHover = useMountedMedia('(hover: hover) and (pointer: fine)')
const localExpanded = shallowRef(props.defaultExpanded)
const isExpanded = computed(() => props.expanded ?? localExpanded.value)
// Index of the avatar under a mouse pointer; the +N button counts as the last slot.
const hovered = shallowRef<number | null>(null)
const text = computed(() => ({ ...avatarGroupMessages, ...props.labels }))
const limit = computed(() => Math.max(1, props.max))
const overflow = computed(() => Math.max(0, props.members.length - limit.value))
const visible = computed(() => (isExpanded.value ? props.members : props.members.slice(0, limit.value)))
const px = computed(() => SIZES[props.size].px)
// Avatars overlap by a third of their width. Hovering one parts the row around
// it: everything before slides back, everything after slides on, so the hovered
// avatar shows in full while the group stays in place.
const geometry = computed(() => {
const overlap = Math.round(px.value / 3)
return { overlap, rest: -overlap, push: Math.round(overlap * .75) }
})
const parting = computed(() => !reduce.value && canHover.value && hovered.value !== null)
function offset(index: number) {
if (!parting.value || index === hovered.value) return 0
return index < (hovered.value ?? 0) ? -geometry.value.push : geometry.value.push
}
function toggle() {
const next = !isExpanded.value
if (props.expanded === undefined) localExpanded.value = next
emit('update:expanded', next)
emit('expandedChange', next)
}
const showMore = computed(() => text.value.showMore.replace('{count}', String(overflow.value)))
function enter(index: number, event: PointerEvent) {
if (event.pointerType === 'mouse') hovered.value = index
}
</script>
<template>
<TooltipProvider :skip-delay="400">
<div class="be-avatars" :class="className" :data-size="size" @pointerleave="hovered = null">
<ul :aria-label="label ?? text.label" class="be-avatars-list">
<AnimatePresence :initial="false" mode="popLayout">
<motion.li
v-for="(member, index) in visible"
:key="member.id"
class="be-avatar-slot"
:layout="reduce ? false : 'position'"
:initial="{ opacity: 0, scale: reduce ? 1 : .6 }"
:animate="{ opacity: 1, scale: 1 }"
:exit="{ opacity: 0, scale: reduce ? 1 : .6, transition: { duration: .14, ease: easeOut } }"
:transition="{ layout: layoutSpring, scale: layoutSpring, opacity: { duration: .18, ease: easeOut } }"
:style="{ marginInlineStart: index === 0 ? 0 : geometry.rest, zIndex: parting && hovered === index ? visible.length + 1 : index, position: 'relative' }"
>
<Tooltip :content="member.name" side="top">
<motion.span
class="be-avatar-part"
:animate="{ x: offset(index), y: parting && hovered === index ? -3 : 0, scale: parting && hovered === index ? 1.06 : 1 }"
:transition="layoutSpring"
@pointerenter="enter(index, $event)"
>
<Avatar :name="member.name" :src="member.src" :size="size" />
</motion.span>
</Tooltip>
</motion.li>
<motion.li
v-if="overflow > 0"
key="overflow"
class="be-avatar-slot"
:layout="reduce ? false : 'position'"
:transition="{ layout: layoutSpring }"
:style="{ marginInlineStart: geometry.rest, zIndex: visible.length, position: 'relative' }"
>
<motion.span class="be-avatar-part" :animate="{ x: offset(visible.length) }" :transition="layoutSpring" @pointerenter="enter(visible.length, $event)">
<motion.button
type="button"
class="be-avatar be-avatar-count" :data-size="size"
:aria-expanded="isExpanded"
:aria-label="isExpanded ? text.showFewer : showMore"
:while-press="reduce ? undefined : { scale: .92 }"
:transition="pressSpring"
@click="toggle"
>
<AnimatePresence :initial="false" mode="popLayout">
<motion.span
:key="isExpanded ? 'less' : 'more'"
aria-hidden="true"
:initial="{ opacity: 0, y: reduce ? 0 : isExpanded ? 8 : -8 }"
:animate="{ opacity: 1, y: 0 }"
:exit="{ opacity: 0, y: reduce ? 0 : isExpanded ? -8 : 8 }"
:transition="{ duration: .16, ease: easeOut }"
>{{ isExpanded ? '−' : `+${overflow}` }}</motion.span>
</AnimatePresence>
</motion.button>
</motion.span>
</motion.li>
</AnimatePresence>
</ul>
</div>
</TooltipProvider>
</template>
<style scoped>
.be-avatars { display: inline-flex; align-items: center; }
.be-avatars-list { position: relative; display: flex; align-items: center; margin: 0; padding: 0; list-style: none; }
.be-avatar-slot { margin: 0; }
.be-avatar-part { display: block; border-radius: 9999px; }
.be-avatar-count {
border: 0; padding: 0; background: var(--beui-foreground); color: var(--beui-background);
font-family: inherit; font-weight: 500; font-variant-numeric: tabular-nums; cursor: pointer;
}
.be-avatar-count { outline: none; }
.be-avatar-count:focus-visible { box-shadow: 0 0 0 2px var(--beui-ring); }
</style>Install
Add it with your package manager, or copy the source manually.
Run it in a project set up with shadcn-vue. The source is copied to components/beui and its dependencies are added to your package.json. Read the shadcn-vue guide
$ bunx --bun shadcn-vue@latest add @beui-vue/avatar-group$ bun add beui-vueDownload the release archive to your project root, then install the local file.
$ bun add ./beui-vue-0.1.0.tgzImport the shared styles once in your application.
import AvatarGroup from '@/components/beui/controls/AvatarGroup.vue'
import '@/components/beui/styles.css'API Reference
Avatar
namestring—src?stringImage URL. Without one, or if it fails to load, initials show.
—size?"sm" | "md" | "lg"mdclass?string—AvatarGroup
membersreadonly AvatarGroupMember[]—max?numberAvatars shown before the rest collapse into a +N count.
4size?AvatarSizemdv-model:expanded?booleanWhether every member is shown. Pair with `@update:expanded`.
undefineddefaultExpanded?booleanfalselabel?stringAccessible name for the list, such as "Project members".
—labels?Partial<AvatarGroupMessages>Override any built-in string. `{count}` fills `showMore`.
—className?string—@expanded-change?(value: boolean) => void—Related components
Expandable Control
Click-to-expand button and chip controls that reveal a label or trailing action through spring layout continuity.
Preview Rail
Codex app-inspired navigation rail with compact ticks that form a hover pyramid and reveal a floating destination preview.
Tooltip
Composable hover, focus and tap tooltip with a subtle scale and fade, collision-aware side and alignment, an optional arrow, a pointer corridor onto hoverable content, one label at a time, keyboard-only focus reveal and optional cursor tracking.
Updated