Skip to content
Primitives

Avatar

Radix avatar with image + fallback, plus a stacked AvatarGroup with sized +N overflow and a names tooltip.

MotionAvatarGroup +N chip uses the shared Tooltip

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import {
  Avatar,
  AvatarImage,
  AvatarFallback,
  AvatarGroup,
} from "kineto/avatar";

Preview

MB
AR
SK
DC
+2
AR
SK
DC
LP
+2
tsx
"use client";

import * as React from "react";
import * as AvatarPrimitive from "@radix-ui/react-avatar";

import { cn } from "@/lib/cn";
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";

const Avatar = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Root>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Root>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Root
    ref={ref}
    className={cn(
      "relative flex size-10 shrink-0 overflow-hidden rounded-full",
      className
    )}
    {...props}
  />
));
Avatar.displayName = "Avatar";

const AvatarImage = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Image>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Image>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Image
    ref={ref}
    className={cn("aspect-square size-full object-cover", className)}
    {...props}
  />
));
AvatarImage.displayName = "AvatarImage";

const AvatarFallback = React.forwardRef<
  React.ElementRef<typeof AvatarPrimitive.Fallback>,
  React.ComponentPropsWithoutRef<typeof AvatarPrimitive.Fallback>
>(({ className, ...props }, ref) => (
  <AvatarPrimitive.Fallback
    ref={ref}
    className={cn(
      "flex size-full items-center justify-center rounded-full bg-muted text-sm font-medium text-muted-foreground",
      className
    )}
    {...props}
  />
));
AvatarFallback.displayName = "AvatarFallback";

type AvatarGroupSize = "sm" | "md" | "lg" | "xl";

/* One size token drives the avatars, the +N chip, and the overlap together so the cluster always
 * stays visually even — the size is applied to each child and the chip, never hand-set per avatar. */
const groupSizeClasses: Record<
  AvatarGroupSize,
  { avatar: string; chip: string; space: string }
> = {
  sm: { avatar: "size-8", chip: "size-8 text-[10px]", space: "-space-x-2.5" },
  md: { avatar: "size-10", chip: "size-10 text-xs", space: "-space-x-3" },
  lg: { avatar: "size-12", chip: "size-12 text-sm", space: "-space-x-3.5" },
  xl: { avatar: "size-14", chip: "size-14 text-sm", space: "-space-x-4" },
};

/** Best-effort display name for an avatar child: `title`, then `aria-label`, then a nested image `alt`. */
function avatarLabel(node: React.ReactNode): string | null {
  if (!React.isValidElement(node)) return null;
  const props = node.props as Record<string, unknown> & { children?: React.ReactNode };
  if (typeof props.title === "string" && props.title) return props.title;
  if (typeof props["aria-label"] === "string" && props["aria-label"]) {
    return props["aria-label"] as string;
  }
  let alt: string | null = null;
  React.Children.forEach(props.children, (child) => {
    if (alt || !React.isValidElement(child)) return;
    const childAlt = (child.props as { alt?: unknown }).alt;
    if (typeof childAlt === "string" && childAlt) alt = childAlt;
  });
  return alt;
}

export interface AvatarGroupProps extends React.HTMLAttributes<HTMLDivElement> {
  /** Cap the number of avatars shown; the remainder collapses into a +N chip. */
  max?: number;
  /** Sizes every avatar and the +N chip together. Per-avatar `className` still wins. */
  size?: AvatarGroupSize;
  /** Show a tooltip on the +N chip listing the hidden people (default true). */
  tooltip?: boolean;
}

const AvatarGroup = React.forwardRef<HTMLDivElement, AvatarGroupProps>(
  ({ className, children, max, size = "md", tooltip = true, ...props }, ref) => {
    const sizes = groupSizeClasses[size];
    const items = React.Children.toArray(children);
    const shown = typeof max === "number" ? items.slice(0, max) : items;
    const hidden = typeof max === "number" ? items.slice(max) : [];
    const extra = hidden.length;

    // Overflow names for the tooltip — list the ones we can label, count the rest.
    const names = hidden.map(avatarLabel).filter((n): n is string => Boolean(n));
    const unnamed = extra - names.length;

    const chip = (
      <div
        className={cn(
          "flex items-center justify-center rounded-full bg-muted font-medium text-muted-foreground ring-2 ring-card",
          sizes.chip
        )}
      >
        +{extra}
      </div>
    );

    return (
      <div ref={ref} className={cn("flex items-center", sizes.space, className)} {...props}>
        {shown.map((child, index) => (
          <div key={index} className="rounded-full ring-2 ring-card">
            {React.isValidElement(child)
              ? React.cloneElement(child as React.ReactElement<{ className?: string }>, {
                  // Group size first so per-avatar className still overrides it.
                  className: cn(sizes.avatar, (child.props as { className?: string }).className),
                })
              : child}
          </div>
        ))}
        {extra > 0 ? (
          tooltip ? (
            <Tooltip>
              <TooltipTrigger asChild>{chip}</TooltipTrigger>
              <TooltipContent>
                {names.length > 0 ? (
                  <div className="flex flex-col gap-0.5">
                    {names.map((name, i) => (
                      <span key={i}>{name}</span>
                    ))}
                    {unnamed > 0 ? (
                      <span className="text-background/70">and {unnamed} more</span>
                    ) : null}
                  </div>
                ) : (
                  `${extra} more`
                )}
              </TooltipContent>
            </Tooltip>
          ) : (
            chip
          )
        ) : null}
      </div>
    );
  }
);
AvatarGroup.displayName = "AvatarGroup";

export { Avatar, AvatarImage, AvatarFallback, AvatarGroup };

src/components/ui/avatar.tsx

Props

PropTypeDefaultDescription
AvatarGroup maxnumber—Cap shown avatars; the rest collapse to a +N chip.
AvatarGroup size"sm" | "md" | "lg" | "xl""md"Sizes every avatar and the +N chip together; per-avatar className still wins.
AvatarGroup tooltipbooleantrueShow a tooltip on the +N chip listing the hidden people (by title / aria-label / image alt).