|
| 1 | +import * as React from "react"; |
| 2 | +import AppBar from "@mui/material/AppBar"; |
| 3 | +import Box from "@mui/material/Box"; |
| 4 | +import Toolbar from "@mui/material/Toolbar"; |
| 5 | +import IconButton from "@mui/material/IconButton"; |
| 6 | +import Typography from "@mui/material/Typography"; |
| 7 | +import Menu from "@mui/material/Menu"; |
| 8 | +import MenuIcon from "@mui/icons-material/Menu"; |
| 9 | +import Container from "@mui/material/Container"; |
| 10 | +import Button from "@mui/material/Button"; |
| 11 | +import MenuItem from "@mui/material/MenuItem"; |
| 12 | +import AdbIcon from "@mui/icons-material/Adb"; |
| 13 | + |
| 14 | +const pages = ["Products", "Pricing", "Blog"]; |
| 15 | +const authPages = ["Login", "Sign Up"]; |
| 16 | +const settings = ["Profile", "Account", "Dashboard", "Logout"]; |
| 17 | + |
| 18 | +function Navbar() { |
| 19 | + const [anchorElNav, setAnchorElNav] = React.useState<null | HTMLElement>( |
| 20 | + null |
| 21 | + ); |
| 22 | + const [anchorElUser, setAnchorElUser] = React.useState<null | HTMLElement>( |
| 23 | + null |
| 24 | + ); |
| 25 | + |
| 26 | + const handleOpenNavMenu = (event: React.MouseEvent<HTMLElement>) => { |
| 27 | + setAnchorElNav(event.currentTarget); |
| 28 | + }; |
| 29 | + const handleOpenUserMenu = (event: React.MouseEvent<HTMLElement>) => { |
| 30 | + setAnchorElUser(event.currentTarget); |
| 31 | + }; |
| 32 | + |
| 33 | + const handleCloseNavMenu = () => { |
| 34 | + setAnchorElNav(null); |
| 35 | + }; |
| 36 | + |
| 37 | + const handleCloseUserMenu = () => { |
| 38 | + setAnchorElUser(null); |
| 39 | + }; |
| 40 | + |
| 41 | + return ( |
| 42 | + <AppBar position="static"> |
| 43 | + <Container maxWidth="xl"> |
| 44 | + <Toolbar disableGutters> |
| 45 | + <AdbIcon sx={{ display: { xs: "none", md: "flex" }, mr: 1 }} /> |
| 46 | + <Typography |
| 47 | + variant="h6" |
| 48 | + noWrap |
| 49 | + component="a" |
| 50 | + href="/" |
| 51 | + sx={{ |
| 52 | + mr: 2, |
| 53 | + display: { xs: "none", md: "flex" }, |
| 54 | + fontFamily: "monospace", |
| 55 | + fontWeight: 700, |
| 56 | + letterSpacing: ".3rem", |
| 57 | + color: "inherit", |
| 58 | + textDecoration: "none", |
| 59 | + }} |
| 60 | + > |
| 61 | + LOGO |
| 62 | + </Typography> |
| 63 | + |
| 64 | + <Box sx={{ flexGrow: 1, display: { xs: "flex", md: "none" } }}> |
| 65 | + <IconButton |
| 66 | + size="large" |
| 67 | + aria-label="account of current user" |
| 68 | + aria-controls="menu-appbar" |
| 69 | + aria-haspopup="true" |
| 70 | + onClick={handleOpenNavMenu} |
| 71 | + color="inherit" |
| 72 | + > |
| 73 | + <MenuIcon /> |
| 74 | + </IconButton> |
| 75 | + <Menu |
| 76 | + id="menu-appbar" |
| 77 | + anchorEl={anchorElNav} |
| 78 | + anchorOrigin={{ |
| 79 | + vertical: "bottom", |
| 80 | + horizontal: "left", |
| 81 | + }} |
| 82 | + keepMounted |
| 83 | + transformOrigin={{ |
| 84 | + vertical: "top", |
| 85 | + horizontal: "left", |
| 86 | + }} |
| 87 | + open={Boolean(anchorElNav)} |
| 88 | + onClose={handleCloseNavMenu} |
| 89 | + sx={{ |
| 90 | + display: { xs: "block", md: "none" }, |
| 91 | + }} |
| 92 | + > |
| 93 | + {pages.map((page) => ( |
| 94 | + <MenuItem key={page} onClick={handleCloseNavMenu}> |
| 95 | + <Typography textAlign="center">{page}</Typography> |
| 96 | + </MenuItem> |
| 97 | + ))} |
| 98 | + </Menu> |
| 99 | + </Box> |
| 100 | + <AdbIcon sx={{ display: { xs: "flex", md: "none" }, mr: 1 }} /> |
| 101 | + <Typography |
| 102 | + variant="h5" |
| 103 | + noWrap |
| 104 | + component="a" |
| 105 | + href="/" |
| 106 | + sx={{ |
| 107 | + mr: 2, |
| 108 | + display: { xs: "flex", md: "none" }, |
| 109 | + flexGrow: 1, |
| 110 | + fontFamily: "monospace", |
| 111 | + fontWeight: 700, |
| 112 | + letterSpacing: ".3rem", |
| 113 | + color: "inherit", |
| 114 | + textDecoration: "none", |
| 115 | + }} |
| 116 | + > |
| 117 | + LOGO |
| 118 | + </Typography> |
| 119 | + <Box sx={{ flexGrow: 1, display: { xs: "none", md: "flex" } }}> |
| 120 | + {pages.map((page) => ( |
| 121 | + <Button |
| 122 | + key={page} |
| 123 | + onClick={handleCloseNavMenu} |
| 124 | + sx={{ my: 2, color: "white", display: "block" }} |
| 125 | + > |
| 126 | + {page} |
| 127 | + </Button> |
| 128 | + ))} |
| 129 | + </Box> |
| 130 | + |
| 131 | + <Box sx={{ flexGrow: 0 }}> |
| 132 | + <Box sx={{ flexGrow: 1, display: { xs: "none", md: "flex" } }}> |
| 133 | + {authPages.map((page) => ( |
| 134 | + <Button |
| 135 | + key={page} |
| 136 | + onClick={handleCloseNavMenu} |
| 137 | + sx={{ my: 2, color: "white", display: "block" }} |
| 138 | + > |
| 139 | + {page} |
| 140 | + </Button> |
| 141 | + ))} |
| 142 | + </Box> |
| 143 | + <Menu |
| 144 | + sx={{ mt: "45px" }} |
| 145 | + id="menu-appbar" |
| 146 | + anchorEl={anchorElUser} |
| 147 | + anchorOrigin={{ |
| 148 | + vertical: "top", |
| 149 | + horizontal: "right", |
| 150 | + }} |
| 151 | + keepMounted |
| 152 | + transformOrigin={{ |
| 153 | + vertical: "top", |
| 154 | + horizontal: "right", |
| 155 | + }} |
| 156 | + open={Boolean(anchorElUser)} |
| 157 | + onClose={handleCloseUserMenu} |
| 158 | + > |
| 159 | + {settings.map((setting) => ( |
| 160 | + <MenuItem key={setting} onClick={handleCloseUserMenu}> |
| 161 | + <Typography textAlign="center">{setting}</Typography> |
| 162 | + </MenuItem> |
| 163 | + ))} |
| 164 | + </Menu> |
| 165 | + </Box> |
| 166 | + </Toolbar> |
| 167 | + </Container> |
| 168 | + </AppBar> |
| 169 | + ); |
| 170 | +} |
| 171 | +export default Navbar; |
0 commit comments