-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathTextWithTooltip.tsx
More file actions
95 lines (89 loc) · 2.59 KB
/
TextWithTooltip.tsx
File metadata and controls
95 lines (89 loc) · 2.59 KB
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
import { InformationCircleIcon } from '@heroicons/react/outline';
import { Box, BoxProps, IconButton, SvgIcon, Typography } from '@mui/material';
import { TypographyProps } from '@mui/material/Typography';
import { JSXElementConstructor, ReactElement, ReactNode, useState } from 'react';
import { TrackEventProps } from 'src/store/analyticsSlice';
import { useRootStore } from 'src/store/root';
import { ContentWithTooltip } from './ContentWithTooltip';
export interface TextWithTooltipProps extends TypographyProps {
text?: ReactNode;
icon?: ReactNode;
iconSize?: number;
iconColor?: string;
iconMargin?: number;
textColor?: string;
// eslint-disable-next-line
children?: ReactElement<any, string | JSXElementConstructor<any>>;
wrapperProps?: BoxProps;
event?: TrackEventProps;
open?: boolean;
setOpen?: (open: boolean) => void;
placement?: 'bottom' | 'left' | 'right';
}
export const TextWithTooltip = ({
text,
icon,
iconSize = 14,
iconColor,
iconMargin,
children,
textColor,
wrapperProps: { sx: boxSx, ...boxRest } = {},
event,
open: openProp = false,
setOpen: setOpenProp,
placement = 'bottom',
...rest
}: TextWithTooltipProps) => {
const [open, setOpen] = useState(openProp);
const trackEvent = useRootStore((store) => store.trackEvent);
const toggleOpen = () => {
if (setOpenProp) setOpenProp(!open);
setOpen(!open);
};
return (
<Box sx={{ display: 'flex', alignItems: 'center', ...boxSx }} {...boxRest}>
{text && (
<Typography {...rest} color={textColor}>
{text}
</Typography>
)}
<ContentWithTooltip
tooltipContent={<>{children}</>}
open={open}
setOpen={toggleOpen}
placement={placement}
>
<IconButton
sx={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
width: iconSize,
height: iconSize,
borderRadius: '50%',
p: 0,
minWidth: 0,
ml: iconMargin || 0.5,
}}
onClick={() => {
if (event) {
trackEvent(event.eventName, { ...event.eventParams });
}
}}
>
<SvgIcon
sx={{
fontSize: iconSize,
color: iconColor ? iconColor : open ? 'info.main' : 'text.muted',
borderRadius: '50%',
'&:hover': { color: iconColor || 'info.main' },
}}
>
{icon || <InformationCircleIcon />}
</SvgIcon>
</IconButton>
</ContentWithTooltip>
</Box>
);
};