-
Notifications
You must be signed in to change notification settings - Fork 3
/
UserMenu.tsx
96 lines (93 loc) · 2.36 KB
/
UserMenu.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
import {
Divider,
IconButton,
ListItemIcon,
ListItemText,
Menu,
MenuItem,
Typography
} from "@mui/material";
import { ExitToApp, VpnKey } from "@mui/icons-material";
import React, { Fragment } from "react";
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 = {
button: {
height: 34,
width: 34
},
divider: {
marginBottom: (theme: Theme) => theme.spacing(1),
marginTop: (theme: Theme) => theme.spacing(1)
},
loggedInAs: {
"&:focus": {
outline: "none"
},
padding: (theme: Theme) => theme.spacing(1, 2)
},
menu: {
marginTop: (theme: Theme) => theme.spacing(1)
}
};
/**
* User menu avatar and dropdown menu
*/
export default function UserMenu({
username,
onChangePassword,
onLogout
}: UserMenuProps) {
const popupState = usePopupState({ popupId: "userMenu", variant: "popover" });
const handleClick =
(cb: (event: React.MouseEvent<HTMLElement, MouseEvent>) => void) =>
(event: React.MouseEvent<HTMLElement, MouseEvent>) => {
popupState.close();
cb && cb(event);
};
return (
<Fragment>
<IconButton {...bindTrigger(popupState)} size="large" sx={sx.button}>
<UserAvatar
name={username}
color="#bdbdbd"
sx={{
color: "#fff",
height: 34,
width: 34
}}
/>
</IconButton>
<Menu
{...bindMenu(popupState)}
anchorOrigin={{ horizontal: "right", vertical: "bottom" }}
transformOrigin={{ horizontal: "right", vertical: "top" }}
sx={sx.menu}
>
<Typography sx={sx.loggedInAs} variant="body2">
Logged in as <strong>{!username ? "Unknown" : username}</strong>
</Typography>
<Divider sx={sx.divider} />
<MenuItem onClick={handleClick(onChangePassword)}>
<ListItemIcon>
<VpnKey />
</ListItemIcon>
<ListItemText>Change password</ListItemText>
</MenuItem>
<MenuItem onClick={handleClick(onLogout)}>
<ListItemIcon>
<ExitToApp />
</ListItemIcon>
<ListItemText>Logout</ListItemText>
</MenuItem>
</Menu>
</Fragment>
);
}