-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSkeletonAvatar.tsx
52 lines (49 loc) · 1.13 KB
/
SkeletonAvatar.tsx
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
import {
Skeleton,
Image,
Avatar,
ImageProps,
AvatarProps,
} from "@chakra-ui/react";
import { useState } from "react";
interface SkeletonAvatarProps extends ImageProps {
alt: string;
isLoaded?: boolean;
}
// renders a skeleton while the image `src` is loading to avoid layout shifts
// useful for profile pictures and other circular images
// example: <SkeletonAvatar src={user.profilePicture} alt={user.name} />
export default function SkeletonAvatar({
w,
h,
isLoaded,
alt,
...props
}: SkeletonAvatarProps) {
const [loaded, setLoaded] = useState(!props.src);
const sharedProps = { w, h };
return (
<Skeleton
isLoaded={(isLoaded === undefined || isLoaded) && loaded}
borderRadius="full"
{...sharedProps}
>
{props.src ? (
<Image
{...props}
{...sharedProps}
alt={alt}
borderRadius="full"
onLoad={() => setLoaded(true)}
objectFit="cover"
/>
) : (
<Avatar
{...(props.src && { bg: "white" })}
{...(props as AvatarProps)}
{...sharedProps}
/>
)}
</Skeleton>
);
}