Files
clash-verge-rev/src/components/layout/layout-item.tsx
2024-03-10 11:12:54 +08:00

63 lines
1.7 KiB
TypeScript

import {
alpha,
ListItem,
ListItemButton,
ListItemText,
ListItemIcon,
} from "@mui/material";
import { useMatch, useResolvedPath, useNavigate } from "react-router-dom";
interface Props {
to: string;
children: string;
icon: React.ReactNode;
}
export const LayoutItem = (props: Props) => {
const { to, children, icon } = props;
const resolved = useResolvedPath(to);
const match = useMatch({ path: resolved.pathname, end: true });
const navigate = useNavigate();
return (
<ListItem sx={{ py: 0.5, maxWidth: 250, mx: "auto", padding: "4px 0px" }}>
<ListItemButton
selected={!!match}
sx={[
{
borderRadius: 2,
marginLeft: 1.25,
paddingLeft: 1.5,
paddingRight: 1,
marginRight: 1.25,
"& .MuiListItemText-primary": {
color: "text.primary",
fontWeight: "700",
},
},
({ palette: { mode, primary } }) => {
const bgcolor =
mode === "light"
? alpha(primary.main, 0.15)
: alpha(primary.main, 0.35);
const color = mode === "light" ? "#1f1f1f" : "#ffffff";
return {
"&.Mui-selected": { bgcolor },
"&.Mui-selected:hover": { bgcolor },
"&.Mui-selected .MuiListItemText-primary": { color },
};
},
]}
onClick={() => navigate(to)}
>
<ListItemIcon>{icon}</ListItemIcon>
<ListItemText
sx={{ textAlign: "center", marginLeft: "-35px" }}
primary={children}
/>
</ListItemButton>
</ListItem>
);
};