EnteMenuItem.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import {
  2. SpaceBetweenFlex,
  3. VerticallyCenteredFlex,
  4. } from "@ente/shared/components/Container";
  5. import {
  6. Box,
  7. ButtonProps,
  8. MenuItem,
  9. Typography,
  10. type TypographyProps,
  11. } from "@mui/material";
  12. import React from "react";
  13. import { CaptionedText } from "../CaptionedText";
  14. import PublicShareSwitch from "../Collections/CollectionShare/publicShare/switch";
  15. interface Iprops {
  16. onClick: () => void;
  17. color?: ButtonProps["color"];
  18. variant?: "primary" | "captioned" | "toggle" | "secondary" | "mini";
  19. fontWeight?: TypographyProps["fontWeight"];
  20. startIcon?: React.ReactNode;
  21. endIcon?: React.ReactNode;
  22. label?: string;
  23. subText?: string;
  24. subIcon?: React.ReactNode;
  25. checked?: boolean;
  26. labelComponent?: React.ReactNode;
  27. disabled?: boolean;
  28. }
  29. export function EnteMenuItem({
  30. onClick,
  31. color = "primary",
  32. startIcon,
  33. endIcon,
  34. label,
  35. subText,
  36. subIcon,
  37. checked,
  38. variant = "primary",
  39. fontWeight = "bold",
  40. labelComponent,
  41. disabled = false,
  42. }: Iprops) {
  43. const handleButtonClick = () => {
  44. if (variant === "toggle") {
  45. return;
  46. }
  47. onClick();
  48. };
  49. const handleIconClick = () => {
  50. if (variant !== "toggle") {
  51. return;
  52. }
  53. onClick();
  54. };
  55. return (
  56. <MenuItem
  57. disabled={disabled}
  58. onClick={handleButtonClick}
  59. sx={{
  60. width: "100%",
  61. color: (theme) =>
  62. variant !== "captioned" && theme.palette[color].main,
  63. ...(variant !== "secondary" &&
  64. variant !== "mini" && {
  65. backgroundColor: (theme) => theme.colors.fill.faint,
  66. }),
  67. "&:hover": {
  68. backgroundColor: (theme) => theme.colors.fill.faintPressed,
  69. },
  70. "& .MuiSvgIcon-root": {
  71. fontSize: "20px",
  72. },
  73. p: 0,
  74. borderRadius: "4px",
  75. }}
  76. >
  77. <SpaceBetweenFlex sx={{ pl: "16px", pr: "12px" }}>
  78. <VerticallyCenteredFlex sx={{ py: "14px" }} gap={"10px"}>
  79. {startIcon && startIcon}
  80. <Box px={"2px"}>
  81. {labelComponent ? (
  82. labelComponent
  83. ) : variant === "captioned" ? (
  84. <CaptionedText
  85. color={color}
  86. mainText={label}
  87. subText={subText}
  88. subIcon={subIcon}
  89. />
  90. ) : variant === "mini" ? (
  91. <Typography variant="mini" color="text.muted">
  92. {label}
  93. </Typography>
  94. ) : (
  95. <Typography fontWeight={fontWeight}>
  96. {label}
  97. </Typography>
  98. )}
  99. </Box>
  100. </VerticallyCenteredFlex>
  101. <VerticallyCenteredFlex gap={"4px"}>
  102. {endIcon && endIcon}
  103. {variant === "toggle" && (
  104. <PublicShareSwitch
  105. checked={checked}
  106. onClick={handleIconClick}
  107. />
  108. )}
  109. </VerticallyCenteredFlex>
  110. </SpaceBetweenFlex>
  111. </MenuItem>
  112. );
  113. }