forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
avatar.tsx
69 lines (62 loc) · 1.6 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
61
62
63
64
65
66
67
68
69
import React from 'react';
import {
Image,
ImageErrorEventData,
NativeSyntheticEvent,
Text,
View,
} from 'react-native';
import { cn } from '~/lib/utils';
const Avatar = React.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 = React.forwardRef<
React.ElementRef<typeof Image>,
React.ComponentPropsWithoutRef<typeof Image>
>(({ className, ...props }, ref) => {
const [hasError, setHasError] = React.useState(false);
function onError(error: NativeSyntheticEvent<ImageErrorEventData>) {
setHasError(!!error);
}
if (hasError) {
return null;
}
return (
<Image
ref={ref}
className={cn('aspect-square h-full w-full', className)}
onError={onError}
{...props}
/>
);
});
AvatarImage.displayName = 'AvatarImage';
const AvatarFallback = React.forwardRef<
React.ElementRef<typeof Text>,
React.ComponentPropsWithoutRef<typeof Text> & { textClass?: string }
>(({ children, textClass, className, ...props }, ref) => (
<View
ref={ref}
className={cn(
'flex h-full w-full items-center justify-center rounded-full bg-muted',
className
)}
role='img'
{...props}
>
<Text className={cn('text-lg text-foreground', textClass)}>{children}</Text>
</View>
));
AvatarFallback.displayName = 'AvatarFallback';
export { Avatar, AvatarImage, AvatarFallback };