{"version":3,"file":"Avatar.cjs","names":[],"sources":["../../../src/components/Avatar/Avatar.tsx"],"sourcesContent":["/*\nCopyright 2023 New Vector Ltd.\n\nSPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial\nPlease see LICENSE files in the repository root for full details.\n*/\n\nimport classnames from \"classnames\";\nimport React, { type ComponentProps, forwardRef } from \"react\";\nimport { getInitialLetter } from \"../../utils/string\";\nimport styles from \"./Avatar.module.css\";\nimport { useIdColorHash } from \"./useIdColorHash\";\n\ntype AvatarProps = (ComponentProps<\"button\"> | ComponentProps<\"span\">) & {\n  /**\n   * The avatar image URL, if any.\n   */\n  src?: React.ComponentProps<\"img\">[\"src\"];\n  /**\n   * The Matrix ID, Room ID, or Alias to generate the color when no image source\n   * is provided. Also used as a fallback when name is empty.\n   */\n  id: string;\n  /**\n   * The name used for the initial letter displayed when no image source is provided.\n   */\n  name: string;\n  /**\n   * Defines the avatar type, typically round, square is usually for spaces.\n   * @default \"round\"\n   */\n  type?: \"square\" | \"round\";\n  /**\n   * The avatar size in CSS units, e.g. `\"24px\"`.\n   */\n  size?: CSSStyleDeclaration[\"height\"];\n  /**\n   * On click handler, will turn the avatar into a button element.\n   */\n  onClick?: (e: React.MouseEvent) => void;\n  /**\n   * Key down handler, will turn the avatar into a button element.\n   */\n  onKeyDown?: (e: React.KeyboardEvent) => void;\n  /**\n   * Key up handler, will turn the avatar into a button element.\n   */\n  onKeyUp?: (e: React.KeyboardEvent) => void;\n  /**\n   * Callback when the image has failed to load.\n   */\n  onError?: React.ComponentProps<\"img\">[\"onError\"];\n};\n\n/**\n * Some props warrant that the avatar become a button for accessibility purposes\n * @param props Avatar props\n * @returns whether the avatar should be a button or not\n */\nfunction shouldBeAButton(props: Partial<AvatarProps>): boolean {\n  return !!(props.onClick || props.onKeyDown || props.onKeyUp);\n}\n\n/**\n * Avatar component that will fallback to an initial letter over a coloured\n * background if no source is provided or if the source has failed to load.\n */\nexport const Avatar = forwardRef<\n  HTMLSpanElement | HTMLButtonElement,\n  AvatarProps\n>(function Avatar(\n  {\n    src,\n    id,\n    name = \"\",\n    type = \"round\",\n    className = \"\",\n    size,\n    style = {},\n    onError,\n    ...props\n  },\n  ref,\n) {\n  return React.createElement(\n    shouldBeAButton(props) ? \"button\" : \"span\",\n    {\n      ref,\n      role: \"img\",\n      // Default the aria-label to id\n      \"aria-label\": id,\n      ...props,\n      \"data-type\": type,\n      \"data-color\": useIdColorHash(id),\n      className: classnames(styles.avatar, className, {\n        [styles[\"avatar-imageless\"]]: !src,\n      }),\n      style: {\n        ...style,\n        \"--cpd-avatar-size\": size,\n      } as React.CSSProperties,\n    },\n    <React.Fragment>\n      {!src ? (\n        getInitialLetter(name)\n      ) : (\n        <img\n          loading=\"lazy\"\n          alt=\"\"\n          src={src}\n          referrerPolicy=\"no-referrer\"\n          className={classnames(styles.image)}\n          data-type={type}\n          style={style}\n          width={size}\n          height={size}\n          onError={onError}\n        />\n      )}\n    </React.Fragment>,\n  );\n});\n"],"mappings":";;;;;;;;;;;;;;;AA2DA,SAAS,gBAAgB,OAAsC;AAC7D,QAAO,CAAC,EAAE,MAAM,WAAW,MAAM,aAAa,MAAM;;;;;;AAOtD,IAAa,UAAA,GAAA,MAAA,YAGX,SAAS,OACT,EACE,KACA,IACA,OAAO,IACP,OAAO,SACP,YAAY,IACZ,MACA,QAAQ,EAAE,EACV,SACA,GAAG,SAEL,KACA;AACA,QAAO,MAAA,QAAM,cACX,gBAAgB,MAAM,GAAG,WAAW,QACpC;EACE;EACA,MAAM;EAEN,cAAc;EACd,GAAG;EACH,aAAa;EACb,cAAc,uBAAA,eAAe,GAAG;EAChC,YAAA,GAAA,WAAA,SAAsB,sBAAA,QAAO,QAAQ,WAAW,GAC7C,sBAAA,QAAO,sBAAsB,CAAC,KAChC,CAAC;EACF,OAAO;GACL,GAAG;GACH,qBAAqB;GACtB;EACF,EACD,iBAAA,GAAA,kBAAA,KAAC,MAAA,QAAM,UAAP,EAAA,UACG,CAAC,MACA,eAAA,iBAAiB,KAAK,GAEtB,iBAAA,GAAA,kBAAA,KAAC,OAAD;EACE,SAAQ;EACR,KAAI;EACC;EACL,gBAAe;EACf,YAAA,GAAA,WAAA,SAAsB,sBAAA,QAAO,MAAM;EACnC,aAAW;EACJ;EACP,OAAO;EACP,QAAQ;EACC;EACT,CAAA,EAEW,CAAA,CAClB;EACD"}