DashLayout.jsx
user_3194955
plain_text
2 months ago
8.3 kB
9
Indexable
import React, { useState } from "react";
import { Outlet, Link, useLocation, useNavigate } from "react-router-dom";
import { styled, useTheme, alpha } from "@mui/material/styles";
import Box from "@mui/material/Box";
import MuiDrawer from "@mui/material/Drawer";
import MuiAppBar from "@mui/material/AppBar";
import Toolbar from "@mui/material/Toolbar";
import List from "@mui/material/List";
import CssBaseline from "@mui/material/CssBaseline";
import Typography from "@mui/material/Typography";
import Divider from "@mui/material/Divider";
import IconButton from "@mui/material/IconButton";
import MenuIcon from "@mui/icons-material/Menu";
import SearchIcon from "@mui/icons-material/Search";
import ChevronLeftIcon from "@mui/icons-material/ChevronLeft";
import ChevronRightIcon from "@mui/icons-material/ChevronRight";
import ListItem from "@mui/material/ListItem";
import ListItemButton from "@mui/material/ListItemButton";
import ListItemIcon from "@mui/material/ListItemIcon";
import ListItemText from "@mui/material/ListItemText";
import DashboardIcon from "@mui/icons-material/Dashboard";
import PeopleIcon from "@mui/icons-material/People";
import AssessmentIcon from "@mui/icons-material/Assessment";
import Button from "@mui/material/Button";
import MenuOpenIcon from "@mui/icons-material/MenuOpen";
const drawerWidth = 240;
const dashboardNavItems = [
{
label: "Dashboard",
title: "Dashboard",
to: "/dashboard",
icon: DashboardIcon,
},
{
label: "Reports",
title: "Reports",
to: "/dashboard/reports",
icon: AssessmentIcon,
},
{
label: "Users",
title: "Users",
to: "/dashboard/users",
icon: PeopleIcon,
},
];
const openedMixin = (theme) => ({
width: drawerWidth,
transition: theme.transitions.create("width", {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
overflowX: "hidden",
});
const closedMixin = (theme) => ({
transition: theme.transitions.create("width", {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
overflowX: "hidden",
width: `calc(${theme.spacing(7)} + 1px)`,
[theme.breakpoints.up("sm")]: {
width: `calc(${theme.spacing(8)} + 1px)`,
},
});
const DrawerHeader = styled("div")(({ theme }) => ({
display: "flex",
alignItems: "center",
justifyContent: "flex-end",
padding: theme.spacing(0, 1),
...theme.mixins.toolbar,
}));
const AppBar = styled(MuiAppBar, {
shouldForwardProp: (prop) => prop !== "open",
})(({ theme, open }) => ({
zIndex: theme.zIndex.drawer + 1,
backgroundColor: "#18181b",
borderBottom: "3px solid #facc15",
transition: theme.transitions.create(["width", "margin"], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.leavingScreen,
}),
...(open && {
marginLeft: drawerWidth,
width: `calc(100% - ${drawerWidth}px)`,
transition: theme.transitions.create(["width", "margin"], {
easing: theme.transitions.easing.sharp,
duration: theme.transitions.duration.enteringScreen,
}),
}),
}));
const Drawer = styled(MuiDrawer, {
shouldForwardProp: (prop) => prop !== "open",
})(({ theme, open }) => ({
width: drawerWidth,
flexShrink: 0,
whiteSpace: "nowrap",
boxSizing: "border-box",
...(open && {
...openedMixin(theme),
"& .MuiDrawer-paper": openedMixin(theme),
}),
...(!open && {
...closedMixin(theme),
"& .MuiDrawer-paper": closedMixin(theme),
}),
}));
const SearchIconWrapper = styled("div")(({ theme }) => ({
padding: theme.spacing(0, 2),
height: "100%",
position: "absolute",
pointerEvents: "none",
display: "flex",
alignItems: "center",
justifyContent: "center",
}));
const Search = styled("div")(({ theme }) => ({
position: "relative",
borderRadius: theme.shape.borderRadius,
backgroundColor: alpha(theme.palette.common.white, 0.15),
"&:hover": {
backgroundColor: alpha(theme.palette.common.white, 0.25),
},
marginRight: theme.spacing(2),
marginLeft: 0,
width: "100%",
[theme.breakpoints.up("sm")]: {
marginLeft: theme.spacing(3),
width: "auto",
},
}));
const StyledInputBase = styled("input")(({ theme }) => ({
color: "white",
padding: "8px 8px 8px 48px",
width: "100%",
background: "none",
border: "none",
outline: "none",
fontSize: "14px",
[theme.breakpoints.up("md")]: {
width: "20ch",
},
}));
const getPageTitle = (pathname) =>
dashboardNavItems.find((i) => i.to === pathname)?.title ?? "Welcome";
const DashLayout = () => {
const theme = useTheme();
const [open, setOpen] = useState(false);
const location = useLocation();
const pageTitle = getPageTitle(location.pathname);
const navigate = useNavigate();
const handleDrawerOpen = () => setOpen(true);
const handleDrawerClose = () => setOpen(false);
const handleLogout = () => navigate("/");
return (
<>
<Box sx={{ display: "flex" }}>
<CssBaseline />
<AppBar position="fixed" open={open}>
<Toolbar>
<IconButton
color="inherit"
aria-label="open drawer"
onClick={open ? handleDrawerClose : handleDrawerOpen}
edge="start"
sx={{ marginRight: 5, ...open }}
>
{open ? <MenuOpenIcon /> : <MenuIcon />}
</IconButton>
<Typography variant="h6" noWrap component="div" sx={{ flexGrow: 1, color: "white", fontWeight: 700 }}>
{pageTitle}
</Typography>
<Search>
<SearchIconWrapper>
<SearchIcon />
</SearchIconWrapper>
<StyledInputBase placeholder="Search..." />
</Search>
<Button
color="inherit"
variant="outlined"
onClick={handleLogout}
sx={{ borderColor: '#facc15', color: '#facc15', fontWeight: 700, '&:hover': { backgroundColor: '#facc15', color: '#18181b' } }}
>
Logout
</Button>
</Toolbar>
</AppBar>
<Drawer variant="permanent" open={open}>
<DrawerHeader sx={{ backgroundColor: '#18181b', borderBottom: '1px solid #facc15' }}>
<IconButton onClick={handleDrawerClose} sx={{ color: '#facc15' }}>
{theme.direction === "rtl" ? <ChevronRightIcon /> : <ChevronLeftIcon />}
</IconButton>
</DrawerHeader>
<Divider />
<List sx={{ backgroundColor: '#18181b', height: '100%' }}>
{dashboardNavItems.map(({ label, to, icon: Icon }) => (
<ListItem key={to} disablePadding sx={{ display: "block" }}>
<ListItemButton
component={Link}
to={to}
selected={location.pathname === to}
sx={{
minHeight: 48,
px: 2.5,
justifyContent: open ? "initial" : "center",
'&.Mui-selected': { backgroundColor: '#facc1520' },
'&:hover': { backgroundColor: '#facc1510' },
}}
>
<ListItemIcon
sx={{
minWidth: 0,
mr: open ? 3 : "auto",
justifyContent: "center",
color: location.pathname === to ? '#facc15' : '#a1a1aa',
}}
>
<Icon />
</ListItemIcon>
<ListItemText
primary={label}
sx={{ opacity: open ? 1 : 0, color: location.pathname === to ? '#facc15' : '#a1a1aa' }}
/>
</ListItemButton>
</ListItem>
))}
</List>
</Drawer>
<Box component="main" sx={{ flexGrow: 1, p: 3 }}>
<DrawerHeader />
<Outlet />
</Box>
</Box>
</>
);
};
export default DashLayout;Editor is loading...
Leave a Comment