79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
|
|
import { cn } from '../../tools/cn';
|
||
|
|
import { Element } from '../../primitives/element';
|
||
|
|
import { GridBox } from '../grid-box';
|
||
|
|
import { Icon } from '../icon';
|
||
|
|
import { Typography } from '../typography';
|
||
|
|
import { AVATAR_INITIAL_VARIANT, AVATAR_MARK_PX, AVATAR_PX } from './constants';
|
||
|
|
import type { AvatarProps } from './types';
|
||
|
|
import { avatarVariants } from './variants';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* A circular creator or user picture. The placeholder sits under the image, so it shows while
|
||
|
|
* the image loads and when it fails, without client code.
|
||
|
|
*/
|
||
|
|
export function Avatar({
|
||
|
|
name,
|
||
|
|
src,
|
||
|
|
size = 'md',
|
||
|
|
verified = false,
|
||
|
|
verifiedLabel,
|
||
|
|
label,
|
||
|
|
className,
|
||
|
|
...props
|
||
|
|
}: AvatarProps) {
|
||
|
|
const px = AVATAR_PX[size];
|
||
|
|
|
||
|
|
return (
|
||
|
|
<Element
|
||
|
|
tag="span"
|
||
|
|
className={cn('relative inline-block flex-none', className)}
|
||
|
|
{...props}
|
||
|
|
>
|
||
|
|
<GridBox
|
||
|
|
tag="span"
|
||
|
|
role={label ? 'img' : undefined}
|
||
|
|
aria-label={label}
|
||
|
|
aria-hidden={label ? undefined : true}
|
||
|
|
justifyItems="justify-items-center"
|
||
|
|
alignItems="items-center"
|
||
|
|
className={avatarVariants({ size })}
|
||
|
|
>
|
||
|
|
<Typography
|
||
|
|
tag="span"
|
||
|
|
variant={AVATAR_INITIAL_VARIANT[size]}
|
||
|
|
color="inherit"
|
||
|
|
className="font-bold uppercase"
|
||
|
|
>
|
||
|
|
{Array.from(name)[0] ?? '?'}
|
||
|
|
</Typography>
|
||
|
|
{src && (
|
||
|
|
// A broken image with an empty alt draws nothing, so the letter shows through.
|
||
|
|
<Element
|
||
|
|
tag="img"
|
||
|
|
src={src}
|
||
|
|
alt=""
|
||
|
|
width={px}
|
||
|
|
height={px}
|
||
|
|
loading="lazy"
|
||
|
|
decoding="async"
|
||
|
|
className="absolute inset-0 size-full object-cover"
|
||
|
|
/>
|
||
|
|
)}
|
||
|
|
</GridBox>
|
||
|
|
{verified && (
|
||
|
|
<GridBox
|
||
|
|
tag="span"
|
||
|
|
justifyItems="justify-items-center"
|
||
|
|
alignItems="items-center"
|
||
|
|
role={verifiedLabel ? 'img' : undefined}
|
||
|
|
aria-label={verifiedLabel}
|
||
|
|
aria-hidden={verifiedLabel ? undefined : true}
|
||
|
|
className="absolute -end-0.5 -bottom-0.5 rounded-full bg-action-primary p-0.5 text-text-on-action ring-2 ring-surface"
|
||
|
|
>
|
||
|
|
<Icon name="check" size={AVATAR_MARK_PX[size]} strokeWidth={3} />
|
||
|
|
</GridBox>
|
||
|
|
)}
|
||
|
|
</Element>
|
||
|
|
);
|
||
|
|
}
|