-
Notifications
You must be signed in to change notification settings - Fork 52
/
Avatar.tsx
60 lines (54 loc) · 1.46 KB
/
Avatar.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
53
54
55
56
57
58
59
60
import { forwardRef, useState } from 'react';
import { Image, Text, View } from 'react-native';
import { cn } from '../lib/utils';
const Avatar = forwardRef<
React.ElementRef<typeof View>,
React.ComponentPropsWithoutRef<typeof View>
>(({ className, ...props }, ref) => (
<View
ref={ref}
className={cn(
'relative flex h-10 w-10 shrink-0 overflow-hidden rounded-full',
className
)}
{...props}
/>
));
Avatar.displayName = 'Avatar';
const AvatarImage = forwardRef<
React.ElementRef<typeof Image>,
React.ComponentPropsWithoutRef<typeof Image>
>(({ className, ...props }, ref) => {
const [hasError, setHasError] = useState(false);
if (hasError) {
return null;
}
return (
<Image
ref={ref}
onError={() => setHasError(true)}
className={cn('aspect-square h-full w-full', className)}
{...props}
/>
);
});
AvatarImage.displayName = 'AvatarImage';
const AvatarFallback = forwardRef<
React.ElementRef<typeof View>,
React.ComponentPropsWithoutRef<typeof View> & { textClassname?: string }
>(({ children, className, textClassname, ...props }, ref) => (
<View
ref={ref}
className={cn(
'flex h-full w-full items-center justify-center rounded-full bg-muted',
className
)}
{...props}
>
<Text className={cn('text-lg text-primary', textClassname)}>
{children}
</Text>
</View>
));
AvatarFallback.displayName = 'AvatarFallback';
export { Avatar, AvatarImage, AvatarFallback };