Skip to content

Commit

Permalink
Extract TextAvatar from BaseAvatar
Browse files Browse the repository at this point in the history
Extracted the fallback/textual avatar into its own component.

Signed-off-by: Clark Fischer <clark.fischer@gmail.com>
  • Loading branch information
clarkf committed Jan 26, 2023
1 parent 878daf5 commit 26dd36f
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions src/components/views/avatars/BaseAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,35 +139,7 @@ const BaseAvatar: React.FC<IProps> = (props) => {
const [imageUrl, onError] = useImageUrl({ url, urls });

if (!imageUrl && defaultToInitialLetter && name) {
const initialLetter = AvatarLogic.getInitialLetter(name);
const textNode = (
<span
className="mx_BaseAvatar_initial"
aria-hidden="true"
style={{
fontSize: toPx(width * 0.65),
width: toPx(width),
lineHeight: toPx(height),
}}
>
{initialLetter}
</span>
);
const imgNode = (
<img
className="mx_BaseAvatar_image"
src={AvatarLogic.defaultAvatarUrlForString(idName || name)}
alt=""
title={title}
onError={onError}
style={{
width: toPx(width),
height: toPx(height),
}}
aria-hidden="true"
data-testid="avatar-img"
/>
);
const avatar = <TextAvatar name={name} idName={idName} width={width} height={height} title={title} />;

if (onClick) {
return (
Expand All @@ -180,8 +152,7 @@ const BaseAvatar: React.FC<IProps> = (props) => {
onClick={onClick}
inputRef={inputRef}
>
{textNode}
{imgNode}
{avatar}
</AccessibleButton>
);
} else {
Expand All @@ -192,8 +163,7 @@ const BaseAvatar: React.FC<IProps> = (props) => {
{...otherProps}
role="presentation"
>
{textNode}
{imgNode}
{avatar}
</span>
);
}
Expand Down Expand Up @@ -240,3 +210,47 @@ const BaseAvatar: React.FC<IProps> = (props) => {

export default BaseAvatar;
export type BaseAvatarType = React.FC<IProps>;

const TextAvatar: React.FC<{
name: string;
idName?: string;
width: number;
height: number;
title?: string;
}> = ({ name, idName, width, height, title }) => {
const initialLetter = AvatarLogic.getInitialLetter(name);
const textNode = (
<span
className="mx_BaseAvatar_initial"
aria-hidden="true"
style={{
fontSize: toPx(width * 0.65),
width: toPx(width),
lineHeight: toPx(height),
}}
>
{initialLetter}
</span>
);
const imgNode = (
<img
className="mx_BaseAvatar_image"
src={AvatarLogic.defaultAvatarUrlForString(idName || name)}
alt=""
title={title}
style={{
width: toPx(width),
height: toPx(height),
}}
aria-hidden="true"
data-testid="avatar-img"
/>
);

return (
<>
{textNode}
{imgNode}
</>
);
};

0 comments on commit 26dd36f

Please sign in to comment.