layout-item.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import {
  2. alpha,
  3. ListItem,
  4. ListItemButton,
  5. ListItemText,
  6. ListItemIcon,
  7. } from "@mui/material";
  8. import { useMatch, useResolvedPath, useNavigate } from "react-router-dom";
  9. import { useVerge } from "@/hooks/use-verge";
  10. interface Props {
  11. to: string;
  12. children: string;
  13. icon: React.ReactNode[];
  14. }
  15. export const LayoutItem = (props: Props) => {
  16. const { to, children, icon } = props;
  17. const { verge } = useVerge();
  18. const { menu_icon = "monochrome" } = verge ?? {};
  19. const resolved = useResolvedPath(to);
  20. const match = useMatch({ path: resolved.pathname, end: true });
  21. const navigate = useNavigate();
  22. return (
  23. <ListItem sx={{ py: 0.5, maxWidth: 250, mx: "auto", padding: "4px 0px" }}>
  24. <ListItemButton
  25. selected={!!match}
  26. sx={[
  27. {
  28. borderRadius: 2,
  29. marginLeft: 1.25,
  30. paddingLeft: 1,
  31. paddingRight: 1,
  32. marginRight: 1.25,
  33. "& .MuiListItemText-primary": {
  34. color: "text.primary",
  35. fontWeight: "700",
  36. },
  37. },
  38. ({ palette: { mode, primary } }) => {
  39. const bgcolor =
  40. mode === "light"
  41. ? alpha(primary.main, 0.15)
  42. : alpha(primary.main, 0.35);
  43. const color = mode === "light" ? "#1f1f1f" : "#ffffff";
  44. return {
  45. "&.Mui-selected": { bgcolor },
  46. "&.Mui-selected:hover": { bgcolor },
  47. "&.Mui-selected .MuiListItemText-primary": { color },
  48. };
  49. },
  50. ]}
  51. onClick={() => navigate(to)}
  52. >
  53. {menu_icon === "monochrome" && (
  54. <ListItemIcon sx={{ color: "text.primary" }}>{icon[0]}</ListItemIcon>
  55. )}
  56. {menu_icon === "colorful" && <ListItemIcon>{icon[1]}</ListItemIcon>}
  57. <ListItemText
  58. sx={{
  59. textAlign: "center",
  60. marginLeft: menu_icon === "disable" ? "" : "-35px",
  61. }}
  62. primary={children}
  63. />
  64. </ListItemButton>
  65. </ListItem>
  66. );
  67. };