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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | 518x 518x 518x 41515x 41515x 41515x 41515x 7286x 34229x 34229x 41515x 41515x 41515x 10012x 10012x 41515x 41515x 41515x 279x 19x 19x 41515x 41515x 10012x 10012x 132x 10012x 10012x 132x 41515x 10160x 41515x 10x 10x 10x 41515x 244x 356x 269x 356x 10x 518x 41515x 532357x 41515x | import { Box, Tooltip as MUITooltip, Stack, TooltipProps } from "@mui/material";
import { Text_color_types } from "@shared/Text/giveText.types";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import { styled, useAppTheme } from "@theme/v2/Provider";
import React, { useMemo, useState, useEffect } from "react";
import GiveText from "../Text/GiveText";
import { GiveTooltipProps } from "./GiveTooltip.types";
const TOOLTIP_BOTTOM_MARGIN = 20;
const TOOLTIP_MAX_WIDTH = 320;
const GiveTooltip = ({
title,
children,
color = "default",
iconLeft,
iconRight,
heading,
isArrow = false,
arrowPlacement,
leaveTouchDelay = 3000,
disableHoverListener = false,
width = "compact",
fluidWidth = false,
alignment = "center",
customTooltipStyles,
onClick,
scrollRef,
customBottomMargin,
...rest
}: GiveTooltipProps) => {
const [open, setOpen] = useState(false);
const [tooltipDisabled, setTooltipDisabled] = useState(false);
const tooltipWidth = (() => {
if (fluidWidth) {
return "fit-content";
} else if (width === "compact") {
return "100%";
} else E{
return TOOLTIP_MAX_WIDTH;
}
})();
const { isWideView } = useCustomThemeV2();
const { palette } = useAppTheme();
const textColorData = useMemo(() => {
const colorMap: Record<string, { color?: string; type: Text_color_types }> =
{
default: { color: palette.text.invert, type: "default" },
warning: {
color: palette.primitive?.warning?.["100"],
type: "warning",
},
success: {
color: palette.primitive?.success?.["100"],
type: "success1",
},
light: {
color: palette.text.invert,
type: "light",
},
};
return colorMap?.[color] || colorMap.default;
}, [color]);
const textColor = textColorData.color;
const textColorType = textColorData.type;
const handleTooltipOpen = (
e?: React.MouseEvent<HTMLDivElement, MouseEvent>,
) => {
if (disableHoverListener) return;
e?.stopPropagation();
setOpen(true);
};
const handleTooltipClose = () => {
setOpen(false);
};
//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;
if (element) {
element.addEventListener("scroll", handleTooltipClose);
}
return () => {
if (element) {
element.removeEventListener("scroll", handleTooltipClose);
}
};
}, []);
// When disableHoverListener is set to true while the tooltip is being hovered, we hide the tooltip by setting open to false
useEffect(() => {
Iif (disableHoverListener && open) {
setOpen(false);
}
}, [disableHoverListener]);
const hoverHandler = () => {
Iif (open) return;
Iif (tooltipDisabled) return;
handleTooltipOpen();
};
return (
<StyledTooltip
color={color}
open={open}
leaveTouchDelay={leaveTouchDelay}
onClose={handleTooltipClose}
version="two"
width={tooltipWidth}
PopperProps={
// On mobile / tablet view tooltip should be at the bottom of the screen
!isWideView
? {
anchorEl: {
getBoundingClientRect: () =>
({
top:
window.innerHeight -
(customBottomMargin || TOOLTIP_BOTTOM_MARGIN),
left: window.innerWidth / 2,
width: 0,
height: 0,
} as any),
},
}
: {
sx: {
width: "fit-content",
},
}
}
title={
<Stack
direction="row"
gap="12px"
justifyContent="center"
alignItems="center"
>
{iconLeft && (
<Box minWidth="18px" minHeight="18px">
{React.cloneElement(iconLeft as React.ReactElement, {
color: textColor,
width: 18,
height: 18,
})}
</Box>
)}
<Stack gap="4px" flexGrow={1}>
{heading && (
<GiveText
variant="bodyS"
color={textColorType}
sx={{
fontWeight: "500",
}}
>
{heading}
</GiveText>
)}
{typeof title === "string" ? (
<GiveText
data-testid="custom-tooltip-text"
variant="bodyS"
color={textColorType}
>
{title}
</GiveText>
) : (
title
)}
</Stack>
{iconRight && (
<Box minWidth="18px" minHeight="18px">
{React.cloneElement(iconRight as React.ReactElement, {
color: textColor,
width: 18,
height: 18,
})}
</Box>
)}
</Stack>
}
arrow={isArrow}
placement={arrowPlacement}
sx={customTooltipStyles}
{...rest}
>
<Stack
onClick={(e) => {
if (!isWideView) {
handleTooltipOpen(e);
}
onClick?.(e);
}}
onMouseOver={
!isWideView || disableHoverListener ? undefined : () => hoverHandler()
}
onMouseLeave={
!isWideView || disableHoverListener
? undefined
: () => handleTooltipClose()
}
alignItems={alignment}
>
{React.isValidElement(children) && !!children.props?.onOpening
? React.cloneElement(
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);
}
}
},
},
)
: children}
</Stack>
</StyledTooltip>
);
};
type StyledTooltipProps = {
width?: string | number;
} & TooltipProps;
const StyledTooltip = styled(
({ ...props }: StyledTooltipProps) => (
<MUITooltip {...props} classes={{ popper: props.className }} />
),
{
shouldForwardProp: (prop) => prop !== "iconPlacement",
},
)(({ width }) => ({
// [`& .${tooltipClasses.tooltip}`]: {
// width,
// maxWidth: TOOLTIP_MAX_WIDTH,
// },
}));
export default GiveTooltip;
|