Avatars

Stacked group

Overlapping faces with an overflow count. Each avatar loads on its own staggered delay, which is what a real roster request looks like.

  • stack
  • overflow
  • presence

Preview

ALAda LovelaceGHGrace HopperATAlan TuringRPRadia Perlman+3
hover the stack

Usage

<AvatarGroup people={members} max={4} size={40} />
"use client";

import { Avatar, type AvatarProps } from "./Avatar";
import { cn } from "@/lib/cn";

export interface AvatarGroupProps {
  people: Array<Pick<AvatarProps, "src" | "name">>;
  max?: number;
  size?: number;
  /** Fraction of the avatar width each face overlaps the previous one. */
  overlap?: number;
  loadingStyle?: AvatarProps["loadingStyle"];
  /** Fan the stack apart on hover. */
  spreadOnHover?: boolean;
  className?: string;
}

export function AvatarGroup({
  people,
  max = 4,
  size = 36,
  overlap = 0.34,
  loadingStyle = "blur",
  spreadOnHover = true,
  className,
}: AvatarGroupProps) {
  const shown = people.slice(0, max);
  const extra = people.length - shown.length;
  const shift = -size * overlap;

  return (
    <div
      className={cn("group/stack flex items-center", className)}
      style={{ paddingLeft: Math.abs(shift) }}
    >
      {shown.map((p, i) => (
        <span
          key={`${p.name}-${i}`}
          className={cn(
            "relative transition-[margin] duration-300 ease-[cubic-bezier(0.22,1,0.36,1)]",
            spreadOnHover && "group-hover/stack:ml-1!",
          )}
          style={{ marginLeft: shift, zIndex: shown.length - i }}
        >
          <span
            className="block rounded-full"
            style={{ boxShadow: `0 0 0 2px var(--color-canvas)`, borderRadius: 9999 }}
          >
            <Avatar {...p} size={size} loadingStyle={loadingStyle} />
          </span>
        </span>
      ))}

      {extra > 0 && (
        <span
          className={cn(
            "relative flex items-center justify-center rounded-full bg-surface font-medium text-ink-2",
            "transition-[margin] duration-300 ease-[cubic-bezier(0.22,1,0.36,1)]",
            spreadOnHover && "group-hover/stack:ml-1!",
          )}
          style={{
            marginLeft: shift,
            width: size,
            height: size,
            fontSize: size * 0.34,
            boxShadow: `0 0 0 2px var(--color-canvas), inset 0 0 0 1px var(--color-line)`,
          }}
        >
          +{extra}
        </span>
      )}
    </div>
  );
}

export default AvatarGroup;