Avatar group
A row of avatars that overlap, for the people on a project, in a thread or on call. Past a maximum it ends in a +N item that names the people it hides. For one person use the avatar.
Install
npx shadcn@latest add https://thrumui.dev/r/avatar-group.jsonAdds components/ui/avatar-group.tsx. Nothing else in your project changes.
import { AvatarGroup } from "@/components/ui/avatar-group";Data shape
// The group reads the name of each avatar it hides.
<Avatar name="Mira Kaur">…</Avatar>const team = [
{ id: "u1", name: "Mira Kaur", picture: "/people/mira.jpg" },
{ id: "u2", name: "Tomas Lindqvist", picture: "/people/tomas.jpg" },
];Props
| Prop | Type | Default | Description |
|---|---|---|---|
max | number | – | How many avatars to draw. The rest are counted in a +N item at the end. Without it every avatar is drawn. |
size | "xs" | "sm" | "md" | "lg" | "xl" | Default "md" | The size of every avatar and of the +N item: 20, 24, 32, 40 or 56 pixels. An avatar that sets its own size keeps it. |
reversed | boolean | Default false | Stack the other way: the first avatar lies on top, in place of the last. |
overflowLabel | string | Default "3 more: Ana, Ben, and Chi" | The name of the +N item, in place of the built-in English one. Set it to translate or to shorten it. |
children | ReactNode | – | Avatar elements. Give each a name: the +N item lists the names it hides. |
className | string | – | Extra classes, merged last. On a card add *:ring-card, so the ring between avatars matches the surface. |
...props | ComponentProps<"div"> | – | Every other div attribute is passed through. Name the group with aria-label. |
Motion and sound
Motion
- The group does not move.
Sound
Silent. This component makes no sound.
Accessibility
- The row has the group role. Name it with aria-label: “Design team”, “On call this week”.
- Each avatar keeps its own name, from its picture's alt text or its fallback.
- The +N item is an image named after what it hides: “2 more: Idris Bello and Sofia Marin”. The same text is its tooltip.
- A 2 pixel ring of the page colour separates each avatar from the next, so two pictures of a similar colour do not merge.
- Nothing in the group takes focus. A group with no avatars is an empty row.
Source
"use client";
import { Children, cloneElement, isValidElement, type ComponentProps, type CSSProperties, type ReactElement } from "react";
import { cn } from "@/lib/utils";
import { AVATAR_SIZE, AvatarSizeContext, type AvatarSize } from "@/components/ui/avatar";
export type AvatarGroupProps = ComponentProps<"div"> & {
/** How many avatars to draw. The rest are counted in a "+N" item at the end. Without it every avatar is drawn. */
max?: number;
/** The size of every avatar in the group, unless an avatar sets its own. */
size?: AvatarSize;
/** Stack the other way: the first avatar lies on top, in place of the last. */
reversed?: boolean;
/** The name of the "+N" item, in place of the built-in English one ("3 more: Ana, Ben, and Chi"). */
overflowLabel?: string;
};
// Each avatar covers about a fifth of the one before it, and less at 20 pixels, so two initials stay readable beside the ring.
const OVERLAP: Record<AvatarSize, string> = { xs: "*:not-first:-ms-0.5", sm: "*:not-first:-ms-1", md: "*:not-first:-ms-1.5", lg: "*:not-first:-ms-2", xl: "*:not-first:-ms-3" };
type Child = ReactElement<{ name?: string; square?: boolean; style?: CSSProperties }>;
/**
* Avatars in a row, each overlapping the one before, with a ring of the page colour between them.
* Its children are `Avatar` elements. Name the group with an `aria-label`: it has the group role.
*/
export function AvatarGroup({ max, size = "md", reversed = false, overflowLabel, className, children, ...props }: AvatarGroupProps) {
const all = Children.toArray(children).filter((child): child is Child => isValidElement(child));
const limit = max === undefined ? all.length : Math.max(0, Math.floor(max));
const shown = all.slice(0, limit);
const hidden = all.slice(limit);
const names = hidden.map((child) => child.props.name).filter((name): name is string => Boolean(name));
const label =
overflowLabel ?? `${hidden.length} more${names.length ? `: ${new Intl.ListFormat("en", { style: "long", type: "conjunction" }).format(names)}` : ""}`;
return (
<div
data-slot="avatar-group"
role="group"
// On a card or a popover, change the ring to that surface: className="*:ring-card".
className={cn(
"isolate inline-flex items-center *:ring-2 *:ring-background *:transition-transform *:duration-150 *:ease-out *:hover:-translate-y-0.5 motion-reduce:*:transition-none",
OVERLAP[size],
className,
)}
{...props}
>
<AvatarSizeContext value={size}>
{/* Later avatars lie on top by default. Reversed, each one is lifted above the next. */}
{shown.map((child, i) => (reversed ? cloneElement(child, { style: { ...child.props.style, zIndex: all.length - i } }) : child))}
</AvatarSizeContext>
{hidden.length > 0 && (
<span
data-slot="avatar-group-overflow"
role="img"
aria-label={label}
title={label}
className={cn(
"relative inline-grid shrink-0 place-items-center border border-foreground/22 bg-foreground/12 leading-none font-medium text-foreground tabular-nums select-none",
// The same shape as the avatars it stands for.
all[0]?.props.square ? "rounded-md" : "rounded-[calc(var(--radius)*9999)]",
AVATAR_SIZE[size],
)}
>
+{hidden.length}
</span>
)}
</div>
);
}Files and dependencies
- Adds
components/ui/avatar-group.tsx- Packages
- None
- Brings along
- Avatar
Changelog
- 1.0.0 · 2026-12-03First release.