-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
avatar.tsx
225 lines (206 loc) · 6.25 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import React, { useRef } from "react";
import { ImageProps } from "../image/image";
import Image from "../image/image";
import { ImageSourcePropType, ViewStyle } from "react-native";
import Box from "../../atoms/box/box";
import Text from "../../atoms/text/text";
import { useAccessibleColor } from "../../../hooks/useAccessibleColor";
import {
MoleculeComponentProps,
PaletteColors,
} from "../../../theme/src/types";
import { pearl } from "../../../pearl";
import { useAvatarGroup } from "./useAvatarGroup";
import { AvatarAtoms } from "./avatar.config";
/**
* A function that generates initials from a name
* @param name The name to generate initials from
* @returns The initials generated from the name
*/
const defaultGetInitials = (name: string) =>
name
.split(" ")
.map((el) => el[0])
.join("")
.toUpperCase();
const DEFAULT_AVATAR_BACKGROUND = [
"#c8e6d0ff",
"#e0ecbaff",
"#fbb991ff",
"#f27e81ff",
"#8bc7d3ff",
"#bcdfd8ff",
"#fae3d9ff",
"#f8b4b9ff",
"#c2b0d7ff",
"#eeef99ff",
"#a5d7c4ff",
"#fef5aeff",
"#f7bdd6ff",
"#d6a5cbff",
"#c38788ff",
"#FA8072",
"#FFD700",
"#FFA500",
];
export type BaseAvatarProps = Omit<
ImageProps,
"source" | "size" | "variant"
> & {
/** The name of the person in the avatar. If 'src' is not loaded or is missing, the 'name' will be used to create the initials */
name?: string;
/** The source of the Avatar image. This can be a url, or a local image */
src?: string | number;
/** A method to specify how initials are generated from a user's name */
getInitials?(name: string): string;
/** A custom list of background colors to choose the avatar background from.
*
* @default
* [
"#c8e6d0ff",
"#e0ecbaff",
"#fbb991ff",
"#f27e81ff",
"#8bc7d3ff",
"#bcdfd8ff",
"#fae3d9ff",
"#f8b4b9ff",
"#c2b0d7ff",
"#eeef99ff",
"#a5d7c4ff",
"#fef5aeff",
"#f7bdd6ff",
"#d6a5cbff",
"#c38788ff",
"#FA8072",
"#FFD700",
"#FFA500",
]
*/
customBackgroundColors?: string[];
};
/**
* CustomAvatar is a component that renders an avatar image, initials or a fallback component.
* It uses the name, src, getInitials, atoms, and fallbackComponent props from the parent Avatar component.
* If an image source is provided, it will render the image.
* If no image source is provided but a name is provided, it will render the initials of the name.
* If neither image source nor name is provided, it will render the fallbackComponent.
* If no fallbackComponent is provided, it will render nothing.
*/
const CustomAvatar = React.memo(
React.forwardRef(
(
{ atoms }: MoleculeComponentProps<"Avatar", BaseAvatarProps, AvatarAtoms>,
ref: any
) => {
const { name, src, getInitials, fallbackComponent, ...otherBoxProps } =
atoms.box;
// Function to pick a random color from the namedColors object
const pickRandomColor = () =>
DEFAULT_AVATAR_BACKGROUND[
Math.floor(Math.random() * DEFAULT_AVATAR_BACKGROUND.length)
];
// Store the random color in a ref to prevent it from changing on re-renders
const randomColor = useRef(pickRandomColor()).current;
// Determine the text color based on the background color to ensure accessibility
const accessibleTextColor = useAccessibleColor(
typeof otherBoxProps.backgroundColor === "string"
? (otherBoxProps.backgroundColor as PaletteColors)
: typeof otherBoxProps.bgColor === "string"
? (otherBoxProps.bgColor as PaletteColors)
: typeof (otherBoxProps.style as ViewStyle)?.backgroundColor ===
"string"
? ((otherBoxProps.style as ViewStyle)
?.backgroundColor as PaletteColors)
: randomColor,
{
light: "neutral.50",
dark: "neutral.900",
}
);
// Function to render the fallback component (initials or fallbackComponent prop)
const renderFallBack = () => {
if (name) {
const initialComputeFunction = getInitials ?? defaultGetInitials;
const nameInitials = initialComputeFunction(name);
return (
<Text color={accessibleTextColor} {...atoms.text}>
{nameInitials}
</Text>
);
}
if (fallbackComponent) return React.cloneElement(fallbackComponent);
return null;
};
// Determine the final source of the image
const finalSource =
src && typeof src === "string"
? ({ uri: src } as ImageSourcePropType)
: (src as ImageSourcePropType);
// If a source is provided, render the image
if (finalSource) {
return (
<Image ref={ref} source={finalSource} {...(otherBoxProps as any)} />
);
}
// If no source is provided, render the fallback
return (
<Box
{...otherBoxProps}
backgroundColor={
otherBoxProps.backgroundColor ??
otherBoxProps.bgColor ??
randomColor
}
justifyContent="center"
alignItems="center"
>
{renderFallBack()}
</Box>
);
}
)
);
const StyledAvatar = pearl<BaseAvatarProps, "molecule", AvatarAtoms>(
CustomAvatar,
{
componentName: "Avatar",
type: "molecule",
animatable: true,
},
undefined,
{
partForOverridenStyleProps: "box",
partForOverridenNativeProps: "box",
partForOverridenAnimationProps: "box",
}
);
/** The Avatar component is used to represent a user, and displays the profile picture, initials or fallback icon. */
const Avatar = React.forwardRef(
(
{
children,
...rest
}: Omit<
MoleculeComponentProps<"Avatar", BaseAvatarProps, AvatarAtoms>,
"atoms"
> & {
atoms?: Partial<AvatarAtoms>;
},
ref: any
) => {
let { size, variant } = useAvatarGroup();
// Overwrite props from avatar group
rest.size = size ?? rest.size;
rest.variant = variant ?? rest.variant;
// Render a fallback
return (
<StyledAvatar {...(rest as any)} ref={ref}>
{children}
</StyledAvatar>
);
}
);
export type AvatarProps = React.ComponentProps<typeof Avatar>;
Avatar.displayName = "Avatar";
export default Avatar;