/*! Strand UI | MIT License | dillingerstaffing.com */ import type { JSX } from "preact"; import { forwardRef } from "preact/compat"; import { useState } from "preact/hooks"; import { cx } from "../../internal/index.js"; export interface AvatarProps extends Omit, "size"> { /** Image URL; falls back to initials if it fails to load. */ src?: string; alt?: string; /** One or two characters shown without an image. */ initials?: string; size?: "sm" | "md" | "lg" | "xl"; } /** * Circular person mark: an image, or initials. * * @example * */ export const Avatar = forwardRef(({ src, alt = "", initials = "", size = "md", className = "", ...rest }, ref) => { const [imgError, setImgError] = useState(false); const displayInitials = initials.slice(0, 2).toUpperCase(); return (
{src && !imgError ? ( {alt} setImgError(true)} /> ) : ( )}
); }); Avatar.displayName = "Avatar";