forked from flexport/__latitude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTooltip.jsx
152 lines (142 loc) · 3.26 KB
/
Tooltip.jsx
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
/**
* TEAM: frontend_infra
* @flow strict
*/
import * as React from "react";
import {StyleSheet, css} from "aphrodite";
import TooltipTrigger from "react-popper-tooltip";
import colors from "./colors";
import {zIndices} from "./tools/zIndices";
const verticalOffset = 8;
type Props = {|
+placement?: "top" | "bottom" | "left" | "right",
+overlay: React.Node,
+mouseEnterDelay?: number, // seconds
+mouseExitDelay?: number, // seconds
+maxWidth?: number,
+children: React.Node,
+triggerClassName?: string,
|};
export default function Tooltip({
placement = "right",
overlay,
children,
mouseEnterDelay = 0,
mouseExitDelay = 0.15,
maxWidth = null,
triggerClassName = "",
}: Props) {
return (
<TooltipTrigger
placement={placement}
trigger="hover"
tooltip={makeContainer(overlay, maxWidth)}
delayShow={mouseEnterDelay * 1000}
delayHide={mouseExitDelay * 1000}
modifiers={{
offset: {
offset: `0,${verticalOffset}px`,
},
}}
>
{makeTrigger(children, triggerClassName)}
</TooltipTrigger>
);
}
function makeContainer(children: React.Node, maxWidth: ?number) {
return ({
arrowRef,
tooltipRef,
getArrowProps,
getTooltipProps,
placement,
}) => (
<div
{...getTooltipProps({
ref: tooltipRef,
className: css(styles.tooltipContainer),
style: maxWidth != null ? {maxWidth} : {},
})}
>
<div
{...getArrowProps({
ref: arrowRef,
className: css(styles.tooltipArrow, getPlacementStyles(placement)),
})}
/>
<div className={css(styles.tooltipContent)}>{children}</div>
</div>
);
}
function makeTrigger(children: React.Node, className: string) {
return ({getTriggerProps, triggerRef}) => (
<div
{...getTriggerProps({
ref: triggerRef,
className: className || css(styles.trigger),
})}
>
{children}
</div>
);
}
function getPlacementStyles(placement: string) {
switch (placement) {
case "top":
return styles.downArrow;
case "bottom":
return styles.upArrow;
case "left":
return styles.rightArrow;
case "right":
return styles.leftArrow;
default:
return null;
}
}
const arrowVerticalOffset = "-14px";
const styles = StyleSheet.create({
trigger: {
display: "inline-block",
},
tooltipContainer: {
display: "flex",
flexDirection: "column",
margin: "0.4rem",
transition: "opacity 0.3s",
zIndex: zIndices.zIndex1500AboveModal.value,
},
tooltipContent: {
backgroundColor: colors.grey60,
boxShadow: "0 0 4px rgba(0, 0, 0, 0.17)",
color: colors.white,
padding: "12px",
textAlign: "left",
textDecoration: "none",
wordBreak: "break-word",
},
tooltipArrow: {
position: "absolute",
width: 0,
height: 0,
borderColor: "transparent",
borderStyle: "solid",
borderWidth: "8px",
},
upArrow: {
top: arrowVerticalOffset,
borderBottomColor: colors.grey60,
},
downArrow: {
bottom: arrowVerticalOffset,
borderTopColor: colors.grey60,
},
leftArrow: {
left: arrowVerticalOffset,
borderRightColor: colors.grey60,
},
rightArrow: {
right: arrowVerticalOffset,
borderLeftColor: colors.grey60,
},
});