Skip to content

Commit

Permalink
Consistent UserAvatar in UserMenu and elsewhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Corner committed Nov 17, 2023
1 parent a96caab commit e217582
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 46 deletions.
24 changes: 16 additions & 8 deletions src/UserAvatar/UserAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import { Avatar } from "@mui/material";
import React from "react";
import { UserAvatarProps } from "./UserAvatar.types";

// returns the first char of first name and first char of last name
const getFirstAndLastChars = (str: string) => {
return str.charAt(0) + str.charAt(str.length - 1);
};

/**
* UserAvatar component
*/
export default function UserAvatar({
img,
name = "",
color = "rgb(0,0,0)"
color = "rgb(0,0,0)",
sx
}: UserAvatarProps) {
// returns the first char of first name and first char of last name
const getFirstAndLastChars = (str: string) => {
return str.charAt(0) + str.charAt(str.length - 1);
};

// set icon name
let initials = (name === "" ? "?" : name)
.split(" ")
Expand All @@ -27,9 +28,16 @@ export default function UserAvatar({
return (
<>
{img ? (
<Avatar src={img} />
<Avatar src={img} sx={sx} />
) : (
<Avatar sx={{ backgroundColor: color }}>{initials}</Avatar>
<Avatar
sx={[
{ backgroundColor: color, fontSize: "14px" },
...(Array.isArray(sx) ? sx : [sx])
]}
>
{initials}
</Avatar>
)}
</>
);
Expand Down
6 changes: 6 additions & 0 deletions src/UserAvatar/UserAvatar.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SxProps, Theme } from "@mui/material";

export type UserAvatarProps = {
/**
* Icon background color
Expand All @@ -11,4 +13,8 @@ export type UserAvatarProps = {
* Display Name
*/
name?: string;
/**
* Styling
*/
sx?: SxProps<Theme>;
};
49 changes: 11 additions & 38 deletions src/UserMenu/UserMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
Avatar,
Divider,
IconButton,
ListItemIcon,
Expand All @@ -10,14 +9,15 @@ import {
} from "@mui/material";
import { ExitToApp, VpnKey } from "@mui/icons-material";
import React, { Fragment } from "react";
import { UserAvatarProps, UserMenuProps } from "./UserMenu.types";
import {
bindMenu,
bindTrigger,
usePopupState
} from "material-ui-popup-state/hooks";

import { Theme } from "@mui/material/styles";
import { UserAvatar } from "../UserAvatar";
import { UserMenuProps } from "./UserMenu.types";

// styling
const sx = {
Expand Down Expand Up @@ -58,7 +58,15 @@ export default function UserMenu({
return (
<Fragment>
<IconButton {...bindTrigger(popupState)} size="large" sx={sx.button}>
<UserAvatar username={username} />
<UserAvatar
name={username}
color="#bdbdbd"
sx={{
// color: "#fff",
height: 34,
width: 34
}}
/>
</IconButton>
<Menu
{...bindMenu(popupState)}
Expand Down Expand Up @@ -86,38 +94,3 @@ export default function UserMenu({
</Fragment>
);
}

/**
* User avatar
*
* Provides custom styling and converts username to max 2 initials
*/
const UserAvatar = ({ username, ...rest }: UserAvatarProps) => {
const allInitials = (!username ? "?" : username)
.split(" ")
.map(s => s[0])
.join("")
.toUpperCase();
const initials =
allInitials.length <= 2 ? allInitials : getFirstAndLastChars(allInitials);
return (
<Avatar
{...rest}
sx={{
backgroundColor: "#bdbdbd",
border: "2px solid #bdbdbd",
color: "white",
fontSize: "13px",
height: 34,
width: 34
}}
>
{initials}
</Avatar>
);
};

// returns the first and last chars of a string concatenated
function getFirstAndLastChars(str: string) {
return [str.charAt(0), str.charAt(str.length - 1)].join("");
}

0 comments on commit e217582

Please sign in to comment.