import * as AvatarPrimitive from "@radix-ui/react-avatar"; import React, { ComponentProps } from "react"; import { cn } from "../hooks"; /** * Avatar root component that displays a user's profile image with fallback support. * Based on Radix UI Avatar primitive with default styling for a circular avatar. * * @param className - Additional CSS classes to apply * @param props - All standard Radix Avatar.Root props are supported */ export const Avatar = ({ className, ...props }: ComponentProps) => ( ); /** * Avatar image component for displaying the actual user image. * If the image fails to load, it will automatically fall back to AvatarFallback. * * @param src - Image source URL * @param className - Additional CSS classes to apply * @param props - All standard img element props are supported */ export const AvatarImage = ({ className, ...props }: ComponentProps) => ( ); /** * Avatar fallback component that displays when the image fails to load. * Typically contains user initials or a default icon. * * @param className - Additional CSS classes to apply * @param children - Content to display (usually initials) * @param props - All standard Radix Avatar.Fallback props are supported */ export const AvatarFallback = ({ className, ...props }: ComponentProps) => ( ); /** * Props for the AvatarGroup component. */ export interface AvatarGroupProps { /** Avatar components to display in the group */ children: React.ReactNode; /** Additional CSS classes to apply */ className?: string; /** Spacing between avatars in the group */ spacing?: "tight" | "normal" | "loose"; /** Maximum number of avatars to show before adding a "+N" indicator */ max?: number; } /** * Avatar group component for displaying multiple avatars in a stacked layout. * Supports configurable spacing and a maximum count with overflow indicator. * * @param children - Avatar components to display * @param className - Additional CSS classes to apply * @param spacing - Spacing between avatars ("tight" | "normal" | "loose"), defaults to "normal" * @param max - Maximum number of avatars to show, remaining count will be shown as "+N" * * @example * ```tsx * * * * U1 * * * * U2 * * * ``` */ export function AvatarGroup({ children, className, spacing = "normal", max }: AvatarGroupProps) { const spacingClasses = { tight: "-space-x-1.5", normal: "-space-x-1", loose: "-space-x-0.45", }; const childrenArray = React.Children.toArray(children); const displayChildren = max ? childrenArray.slice(0, max) : childrenArray; const remainingCount = max && childrenArray.length > max ? childrenArray.length - max : 0; return (
{remainingCount > 0 && ( +{remainingCount} )} {displayChildren.reverse().map((child) => { if (React.isValidElement(child)) { return React.cloneElement( child as React.ReactElement<{ className?: string; style?: React.CSSProperties; }>, { className: cn( `ring-background relative ring-2 transition-all duration-200 ease-in-out hover:z-[100] hover:scale-120`, (child.props as { className?: string })?.className, ), }, ); } return child; })}
); }