Files
clash-proxy/src/components/layout/layout-item.tsx

70 lines
2.0 KiB
TypeScript
Raw Normal View History

import {
alpha,
ListItem,
ListItemButton,
ListItemText,
ListItemIcon,
} from "@mui/material";
2021-12-08 23:36:34 +08:00
import { useMatch, useResolvedPath, useNavigate } from "react-router-dom";
2024-03-10 19:54:47 +08:00
import { useVerge } from "@/hooks/use-verge";
interface Props {
to: string;
children: string;
2024-03-10 19:54:47 +08:00
icon: React.ReactNode[];
}
export const LayoutItem = (props: Props) => {
2024-03-09 23:38:03 +08:00
const { to, children, icon } = props;
2024-03-10 19:54:47 +08:00
const { verge } = useVerge();
const { menu_icon } = verge ?? {};
2021-12-08 23:36:34 +08:00
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" }}>
2021-12-08 23:36:34 +08:00
<ListItemButton
2021-12-12 15:34:17 +08:00
selected={!!match}
2021-12-09 23:28:57 +08:00
sx={[
{
borderRadius: 2,
2024-03-10 07:00:24 +08:00
marginLeft: 1.25,
2024-03-10 19:54:47 +08:00
paddingLeft: 1,
paddingRight: 1,
2024-03-10 07:00:24 +08:00
marginRight: 1.25,
"& .MuiListItemText-primary": {
color: "text.primary",
fontWeight: "700",
},
2021-12-09 23:28:57 +08:00
},
2021-12-12 15:34:17 +08:00
({ palette: { mode, primary } }) => {
2023-12-02 23:20:04 +08:00
const bgcolor =
2021-12-12 15:34:17 +08:00
mode === "light"
? alpha(primary.main, 0.15)
2023-12-02 23:20:04 +08:00
: alpha(primary.main, 0.35);
const color = mode === "light" ? "#1f1f1f" : "#ffffff";
2021-12-09 23:28:57 +08:00
return {
2021-12-12 15:34:17 +08:00
"&.Mui-selected": { bgcolor },
"&.Mui-selected:hover": { bgcolor },
"&.Mui-selected .MuiListItemText-primary": { color },
2021-12-09 23:28:57 +08:00
};
},
]}
2021-12-08 23:36:34 +08:00
onClick={() => navigate(to)}
>
2024-03-10 20:48:13 +08:00
{menu_icon === "monochrome" && (
<ListItemIcon sx={{ color: "text.primary" }}>{icon[0]}</ListItemIcon>
)}
2024-03-10 19:54:47 +08:00
{menu_icon === "colorful" && <ListItemIcon>{icon[1]}</ListItemIcon>}
2024-03-10 00:34:22 +08:00
<ListItemText
2024-03-10 19:54:47 +08:00
sx={{
textAlign: "center",
marginLeft: menu_icon === "disable" ? "" : "-35px",
}}
2024-03-10 00:34:22 +08:00
primary={children}
/>
2021-12-08 23:36:34 +08:00
</ListItemButton>
</ListItem>
);
};