import * as React from "react"; import { cn } from "../../utils/cn"; export interface AvatarProps extends React.HTMLAttributes { src?: string; /** Person's name — used to derive fallback initials and, if no separate alt is given, the image's alt text. */ name: string; size?: "sm" | "md" | "lg"; } const sizeMap = { sm: "ui-h-8 ui-w-8 ui-text-[var(--ui-text-xs)]", md: "ui-h-10 ui-w-10 ui-text-[var(--ui-text-sm)]", lg: "ui-h-14 ui-w-14 ui-text-[var(--ui-text-lg)]" }; function initials(name: string) { const parts = name.trim().split(/\s+/); return ((parts[0]?.[0] ?? "") + (parts.length > 1 ? parts[parts.length - 1]?.[0] ?? "" : "")).toUpperCase(); } /** Avatar with automatic initials fallback when no image is provided or the image fails to load. */ export const Avatar = React.forwardRef( ({ className, src, name, size = "md", ...props }, ref) => { const [errored, setErrored] = React.useState(false); const showImage = src && !errored; return ( {showImage ? ( {name} setErrored(true)} /> ) : ( )} {!showImage && {name}} ); } ); Avatar.displayName = "Avatar";