-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvatar.tsx
More file actions
96 lines (89 loc) · 2.4 KB
/
Avatar.tsx
File metadata and controls
96 lines (89 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {useEffect, useState} from "react";
import type {AvatarProps, AvatarSize} from "./AvatarProps";
const sizeMap: Record<AvatarSize, number> = {
xs: 24,
sm: 32,
md: 40,
lg: 56,
xl: 72,
};
/**
* Renders an Avatar component.
*
* Displays a user profile image when `src` is provided.
* Falls back to initials or a placeholder when the image is missing or fails to load.
*
* @param {Readonly<AvatarProps>} props - Avatar properties
* @returns {React.ReactNode} Rendered avatar
*
* @example
* <Avatar src="https://example.com/avatar.jpg" alt="User Avatar" />
*
* @example
* <Avatar size="lg" fallback="JD" />
*
* @example
* <Avatar src="https://example.com/avatar.jpg" className="my-avatar" />
*
* @example
* <Avatar size="sm" />
*/
export function Avatar(props: Readonly<AvatarProps>) {
const {
src,
alt = "Avatar",
size = "md",
fallback,
className,
} = props;
const [hasError, setHasError] = useState(false);
// Reset error state when image source changes
useEffect(() => {
setHasError(false);
}, [src]);
const pixelSize = sizeMap[size];
const showImage = Boolean(src) && !hasError;
// IMAGE RENDER
if (showImage) {
return (
<img
src={src}
alt={alt}
width={pixelSize}
height={pixelSize}
className={className}
onError={(e) => {
// prevent infinite error loop
e.currentTarget.onerror = null;
setHasError(true);
}}
style={{
borderRadius: "50%",
objectFit: "cover",
}}
/>
);
}
// FALLBACK RENDER
return (
<div
aria-label={alt}
className={className}
style={{
width: pixelSize,
height: pixelSize,
borderRadius: "50%",
display: "flex",
alignItems: "center",
justifyContent: "center",
backgroundColor: "#e5e7eb",
color: "#374151",
fontWeight: 600,
fontSize: Math.floor(pixelSize / 2.5),
userSelect: "none",
}}
>
{fallback?.slice(0, 2).toUpperCase() ?? "?"}
</div>
);
}