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

64 lines
1.7 KiB
TypeScript
Raw Normal View History

import {
alpha,
ListItem,
ListItemButton,
ListItemText,
ListItemAvatar,
Avatar,
} from "@mui/material";
2021-12-08 23:36:34 +08:00
import { useMatch, useResolvedPath, useNavigate } from "react-router-dom";
interface Props {
to: string;
children: string;
img: string;
}
export const LayoutItem = (props: Props) => {
const { to, children, img } = props;
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,
marginLeft: 1.5,
paddingLeft: 1,
paddingRight: 1,
marginRight: 1.5,
textAlign: "left",
"& .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)}
>
<ListItemAvatar sx={{ marginRight: -0.5 }}>
<Avatar src={img}></Avatar>
</ListItemAvatar>
2021-12-12 15:34:17 +08:00
<ListItemText primary={children} />
2021-12-08 23:36:34 +08:00
</ListItemButton>
</ListItem>
);
};