All files / src/components/common/Tooltip Tooltip.tsx

66.66% Statements 26/39
63.26% Branches 31/49
83.33% Functions 10/12
74.28% Lines 26/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220                                                                      516x           516x                             3960x 3960x 3960x   3960x 847x     3960x   554x     3960x 2x 2x     3960x 2x 2x 2x           3960x 847x   847x       847x 847x           3960x     554x 554x                                                                                                                                                                                   42858x           3958x                                            
import { Text, TruncateText } from "@common/Text";
import { TruncateTextProps } from "@common/Text/TruncateText";
import {
  Box,
  ClickAwayListener,
  Tooltip as MuiTooltip,
  TooltipProps as MuiTooltipProps,
  SxProps,
  tooltipClasses,
} from "@mui/material";
import { styled } from "@mui/material/styles";
import { palette } from "@palette";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import React, { Dispatch, SetStateAction, useEffect } from "react";
 
export type TooltipProps = MuiTooltipProps & {
  renderIcon?: () => React.ReactNode;
  title: string | React.ReactNode;
  description?: string | React.ReactNode;
  titleSx?: SxProps;
  fullWidth?: boolean;
  bgColor?: string;
  lineClamp?: TruncateTextProps["lineClamp"];
  parentStyle?: SxProps;
  maxWidth?: string;
  descriptionSx?: SxProps;
  titleContainerSx?: SxProps;
  toolTipSx?: SxProps;
  scrollRef?: any;
  children: React.ReactNode;
  disableOpenFromChildren?: boolean;
  openFromExtSource?: boolean;
  setOpenFromExtSource?: Dispatch<SetStateAction<boolean>>;
};
 
const TooltipIcon = styled("span")({
  "& *": {
    fill: palette.common.white,
  },
});
 
export const Tooltip = styled(
  ({
    renderIcon,
    title,
    description,
    disableHoverListener,
    titleSx,
    fullWidth,
    lineClamp,
    parentStyle,
    descriptionSx,
    titleContainerSx,
    scrollRef,
    ...props
  }: TooltipProps) => {
    const [open, setOpen] = React.useState(false);
    const [tooltipDisabled, setTooltipDisabled] = React.useState(false);
    const { isMobileView } = useCustomTheme();
 
    useEffect(() => {
      Iif (props.openFromExtSource) setOpen(props.openFromExtSource);
    }, [props.openFromExtSource]);
 
    const handleTooltipClose = () => {
      // if (isMobileView && e.type === "mouseleave") return;
      setOpen(false);
    };
 
    const handleTooltipOpen = () => {
      Iif (props.disableOpenFromChildren) return;
      setOpen(true);
    };
 
    const hoverHandler = (e: any) => {
      Iif (open) return;
      Iif (tooltipDisabled) return;
      handleTooltipOpen();
    };
 
    //in the app, we are using tooltips in many places, even in dropdown menus
    //this causes glitchy behavior if we scroll with tooltip open
    //this prevents this glitch
    useEffect(() => {
      const element = scrollRef?.current;
 
      Iif (element) {
        element.addEventListener("scroll", handleTooltipClose);
      }
 
      return () => {
        Iif (element) {
          element.removeEventListener("scroll", handleTooltipClose);
        }
      };
    }, []);
 
    return (
      <ClickAwayListener
        onClickAway={() => {
          handleTooltipClose();
          props.setOpenFromExtSource?.(false);
        }}
      >
        <div style={{ width: fullWidth ? "100%" : "initial" }}>
          <MuiTooltip
            classes={{ popper: props.className }}
            enterDelay={0}
            leaveTouchDelay={3500}
            onClose={() => {
              handleTooltipClose();
              props.setOpenFromExtSource?.(false);
            }}
            open={!disableHoverListener ? open : false}
            title={
              <>
                <Box
                  sx={{
                    display: "flex",
                    gap: 1,
                    ...(titleContainerSx && titleContainerSx),
                  }}
                >
                  {renderIcon && <TooltipIcon>{renderIcon()}</TooltipIcon>}
                  {title && (
                    <TruncateText
                      lineClamp={lineClamp || 5}
                      sx={{
                        textAlign: "center",
                        fontSize: "14px",
                        ...(titleSx && titleSx),
                      }}
                      variant="caption"
                    >
                      {title}
                    </TruncateText>
                  )}
                </Box>
                {description && (
                  <Text
                    variant="captionSmall"
                    color="#E1E1DE"
                    sx={{
                      textAlign: "center",
                      fontSize: "14px",
                      ...(descriptionSx && descriptionSx),
                    }}
                  >
                    {description}
                  </Text>
                )}
              </>
            }
            {...props}
          >
            <Box
              sx={{
                display: "flex",
                alignItems: "center",
                justifyContent: "center",
                ...parentStyle,
              }}
              onClick={disableHoverListener ? undefined : handleTooltipOpen}
              onMouseOver={disableHoverListener ? undefined : hoverHandler}
            >
              {React.isValidElement(props.children) &&
              !!props.children.props?.onOpening
                ? React.cloneElement(
                    props.children as React.ReactElement<{ onOpening?: any }>,
                    {
                      onOpening: (event: "toggle_tooltip", data?: any) => {
                        if (event === "toggle_tooltip") {
                          if (data) {
                            setOpen(false);
                            setTooltipDisabled(true);
                          } else {
                            setTooltipDisabled(false);
                          }
                        }
                      },
                    },
                  )
                : props.children}
            </Box>
          </MuiTooltip>
        </div>
      </ClickAwayListener>
    );
  },
  {
    shouldForwardProp: (prop) =>
      prop !== "bgColor" && prop !== "maxWidth" && prop !== "toolTipSx",
  },
)<{
  bgColor?: string;
  maxWidth?: string;
  toolTipSx?: SxProps;
}>(({ theme, bgColor, maxWidth, toolTipSx }) => ({
  [`& .${tooltipClasses.tooltip}`]: {
    display: "flex",
    flexDirection: "column",
    width: "100%",
    alignItems: "center",
    backgroundColor: bgColor || "#403D3D",
    color: theme.palette.common.white,
    maxWidth: maxWidth || 220,
    fontSize: "12px",
    boxShadow: "0px 4px 16px rgba(0, 0, 0, 0.25)",
    borderRadius: "6px",
    paddingLeft: 16,
    paddingRight: 16,
    paddingTop: 8,
    paddingBottom: 8,
    position: "relative",
    textAlign: "center",
    top: "-5px",
    ...toolTipSx,
  },
}));