-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathAvatar.tsx
More file actions
183 lines (168 loc) · 4.23 KB
/
Copy pathAvatar.tsx
File metadata and controls
183 lines (168 loc) · 4.23 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
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
import { useComponentCssInjection } from "@salt-ds/styles";
import { useWindow } from "@salt-ds/window";
import { clsx } from "clsx";
import {
type ComponentPropsWithoutRef,
forwardRef,
type HTMLAttributes,
type ReactNode,
} from "react";
import { useIcon } from "../semantic-icon-provider";
import { makePrefixer, type RenderPropsType, renderProps } from "../utils";
import avatarCss from "./Avatar.css";
import { useAvatarImage } from "./useAvatarImage";
export type NameToInitials = (name?: string) => string;
export interface AvatarProps extends HTMLAttributes<HTMLDivElement> {
/**
* The name that Avatar represents.
*/
name?: string;
/**
* Defines the function that gets initials. Default is capital first letter of each separate word in name.
* If a function is not passed or returns undefined, Avatar will default to Icon.
*/
nameToInitials?: NameToInitials;
/**
* Image src of Avatar.
*/
src?: string;
/**
* Multiplier for the base avatar.
*/
size?: number;
/**
* Icon to be used as a default item. Defaults to the semantic `UserIcon` when
* `kind` is `"person"`, and the semantic `EntityIcon` when `kind`
* is `"entity"`.
*/
fallbackIcon?: ReactNode;
/**
* What the Avatar depicts, which determines its default shape and fallback icon:
* - `"person"` renders a circular Avatar (default).
* - `"entity"` renders a square Avatar.
*
* @default "person"
*/
kind?: "person" | "entity";
/**
* Changes Avatar's color.
*/
color?:
| "accent"
| "category-1"
| "category-2"
| "category-3"
| "category-4"
| "category-5"
| "category-6"
| "category-7"
| "category-8"
| "category-9"
| "category-10"
| "category-11"
| "category-12"
| "category-13"
| "category-14"
| "category-15"
| "category-16"
| "category-17"
| "category-18"
| "category-19"
| "category-20";
/**
* Render prop to enable customization of the avatar root element.
*/
render?: RenderPropsType["render"];
}
const withBaseName = makePrefixer("saltAvatar");
const DEFAULT_AVATAR_SIZE = 2; // medium
interface AvatarActionProps extends ComponentPropsWithoutRef<"div"> {
render?: RenderPropsType["render"];
}
const AvatarAction = forwardRef<HTMLDivElement, AvatarActionProps>(
function AvatarAction(props, ref) {
return renderProps("div", { ...props, ref });
},
);
const defaultNameToInitials = (name?: string) =>
name
?.split(" ")
.slice(0, 2)
.map((n) => n[0])
.join("")
.toUpperCase();
export const Avatar = forwardRef<HTMLDivElement, AvatarProps>(function Avatar(
{
className,
children: childrenProp,
color = "accent",
name,
nameToInitials = defaultNameToInitials,
kind = "person",
src,
size = DEFAULT_AVATAR_SIZE,
style: styleProp,
fallbackIcon: fallbackIconProp,
render,
...rest
},
ref,
) {
const targetWindow = useWindow();
const { UserIcon, EntityIcon } = useIcon();
const DefaultFallbackIcon = kind === "entity" ? EntityIcon : UserIcon;
const fallbackIcon =
fallbackIconProp === undefined ? (
<DefaultFallbackIcon aria-hidden />
) : (
fallbackIconProp
);
useComponentCssInjection({
testId: "salt-avatar",
css: avatarCss,
window: targetWindow,
});
let children: ReactNode;
const style = {
...styleProp,
"--saltAvatar-size-multiplier": `${size}`,
};
const status = useAvatarImage({ src });
const hasImgNotFailing = status === "loaded";
if (hasImgNotFailing) {
children = <img alt="" src={src} />;
} else if (childrenProp != null) {
children = childrenProp;
}
const avatarInitials = nameToInitials(name);
const ariaProps = name
? render
? {
"aria-label": name,
}
: {
role: "img",
"aria-label": name,
}
: {};
return (
<AvatarAction
ref={ref}
render={render}
style={style}
className={clsx(
withBaseName(),
withBaseName(color),
withBaseName(kind),
{
[withBaseName("withImage")]: hasImgNotFailing,
},
className,
)}
{...ariaProps}
{...rest}
>
{children || avatarInitials || fallbackIcon}
</AvatarAction>
);
});