forked from chanzuckerberg/sci-components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.tsx
129 lines (109 loc) · 2.67 KB
/
index.tsx
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
import {
Tooltip as RawTooltip,
TooltipProps as RawTooltipProps,
TooltipClassKey,
} from "@mui/material";
import { useTheme } from "@mui/material/styles";
import { forwardRef } from "react";
import {
SDSWarningTypes,
showWarningIfFirstOccurence,
} from "src/common/warnings";
import {
StyledPopper,
Subtitle,
TooltipExtraProps,
arrowCss,
tooltipCss,
} from "./style";
type TooltipProps = TooltipExtraProps & RawTooltipProps;
export type { TooltipProps };
/**
* @see https://mui.com/material-ui/react-tooltip/
*
* @warning If the tooltip wraps a disabled component, please make sure to
* wrap the children in a `<span>` tag.
* https://mui.com/components/tooltips/#disabled-elements
*/
const Tooltip = forwardRef(function Tooltip(
props: TooltipProps,
ref
): JSX.Element | null {
const {
arrowOffset,
classes,
inverted,
sdsStyle = "light",
subtitle,
title,
width = "default",
PopperComponent = StyledPopper,
...rest
} = props;
const { children } = rest;
if (inverted) {
showWarningIfFirstOccurence(SDSWarningTypes.TooltipInverted);
}
if (width === "wide" && sdsStyle === "dark") {
showWarningIfFirstOccurence(SDSWarningTypes.TooltipWidth);
}
if (subtitle && sdsStyle === "light") {
showWarningIfFirstOccurence(SDSWarningTypes.TooltipSubtitle);
}
const theme = useTheme();
const extraProps = {
/* stylelint-disable property-no-unknown -- false positive */
arrowOffset,
classes,
inverted,
sdsStyle,
theme,
width
/* stylelint-enable property-no-unknown -- false positive */
};
const tooltip = mergeClass({
className: tooltipCss(extraProps),
key: "tooltip",
props,
});
const arrow = mergeClass({
className: arrowCss(extraProps),
key: "arrow",
props,
});
// (mlila) if no content is passed into the tooltip, don't render
// a tooltip. this matches with the native MUI behavior.
if (!title && !subtitle) return <>{children}</>;
const content = (
<>
{title}
{sdsStyle === "dark" && subtitle && <Subtitle>{subtitle}</Subtitle>}
</>
);
const leaveDelay = inverted || sdsStyle === "dark" ? 0 : 500;
return (
<RawTooltip
classes={{ arrow, tooltip }}
leaveDelay={leaveDelay}
title={content}
PopperComponent={PopperComponent}
ref={ref}
{...rest}
/>
);
});
function mergeClass({
props,
className,
key,
}: {
props: TooltipProps;
className: string;
key: TooltipClassKey;
}) {
const { classes } = props;
if (!classes) return className;
const propClassName = classes[key];
return propClassName ? `${propClassName} ${className}` : className;
}
export default Tooltip;