| 1 |
import styles from '@patternfly/react-styles/css/components/Avatar/avatar'; |
| 2 |
import { css } from '@patternfly/react-styles'; |
| 3 |
|
| 4 |
export interface AvatarProps extends React.DetailedHTMLProps< |
| 5 |
React.ImgHTMLAttributes<HTMLImageElement>, |
| 6 |
HTMLImageElement |
| 7 |
> { |
| 8 |
/** Content rendered inside the avatar, such as custom svgs or icons. */ |
| 9 |
children?: React.ReactNode; |
| 10 |
/** Additional classes added to the avatar. */ |
| 11 |
className?: string; |
| 12 |
/** Specifies the URL of the image for the avatar. */ |
| 13 |
src?: string; |
| 14 |
/** Specifies the alternate text of the image for the avatar. Will instead set the aria-label when using children or initials; to hide the avatar |
| 15 |
* from assistive technologies when passing children or initials, pass an empty string to the alt prop. |
| 16 |
*/ |
| 17 |
alt: string; |
| 18 |
/** Flag to indicate the avatar should have a border. */ |
| 19 |
isBordered?: boolean; |
| 20 |
/** Size variant of avatar. */ |
| 21 |
size?: 'sm' | 'md' | 'lg' | 'xl'; |
| 22 |
/** Color of the avatar. */ |
| 23 |
color?: 'red' | 'orangered' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'purple' | 'gray'; |
| 24 |
/** Initials of the avatar. */ |
| 25 |
initials?: string; |
| 26 |
} |
| 27 |
|
| 28 |
export const Avatar: React.FunctionComponent<AvatarProps> = ({ |
| 29 |
children, |
| 30 |
className, |
| 31 |
src = '', |
| 32 |
alt, |
| 33 |
isBordered, |
| 34 |
size, |
| 35 |
color, |
| 36 |
initials, |
| 37 |
...props |
| 38 |
}: AvatarProps) => { |
| 39 |
const avatarClasses = css(styles.avatar, styles.modifiers[size], isBordered && styles.modifiers.bordered, className); |
| 40 |
const isAltEmptyString = alt === ''; |
| 41 |
|
| 42 |
if (initials || children) { |
| 43 |
return ( |
| 44 |
<div |
| 45 |
className={css(avatarClasses, color && styles.modifiers.colorful, color && styles.modifiers[color])} |
| 46 |
role="img" |
| 47 |
aria-hidden={isAltEmptyString || undefined} |
| 48 |
{...(!isAltEmptyString && { 'aria-label': alt })} |
| 49 |
{...props} |
| 50 |
> |
| 51 |
{initials && ( |
| 52 |
<span className="pf-v6-c-avatar__initials" aria-hidden="true"> |
| 53 |
{initials} |
| 54 |
</span> |
| 55 |
)} |
| 56 |
{children && children} |
| 57 |
</div> |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
return <img src={src} alt={alt} className={avatarClasses} {...props} />; |
| 62 |
}; |
| 63 |
Avatar.displayName = 'Avatar'; |