-
Notifications
You must be signed in to change notification settings - Fork 715
/
TooltipWithBounds.tsx
61 lines (52 loc) · 1.49 KB
/
TooltipWithBounds.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
import React from 'react';
import { withBoundingRects, WithBoundingRectsProps } from '@vx/bounds';
import Tooltip, { TooltipProps, defaultStyles } from './Tooltip';
export type TooltipWithBoundsProps = {
offsetLeft?: number;
offsetTop?: number;
} & TooltipProps &
React.HTMLProps<HTMLDivElement> &
WithBoundingRectsProps;
function TooltipWithBounds({
left: initialLeft = 0,
top: initialTop = 0,
offsetLeft = 10,
offsetTop = 10,
children,
rect: ownBounds,
parentRect: parentBounds,
getRects,
style = defaultStyles,
unstyled = false,
...otherProps
}: TooltipWithBoundsProps) {
let left = initialLeft;
let top = initialTop;
if (ownBounds && parentBounds) {
const placeTooltipLeft =
offsetLeft + ownBounds.right > parentBounds.right ||
offsetLeft + ownBounds.right > window.innerWidth;
const placeTooltipUp =
offsetTop + ownBounds.bottom > parentBounds.bottom ||
offsetTop + ownBounds.bottom > window.innerHeight;
left = placeTooltipLeft ? left - ownBounds.width - offsetLeft : left + offsetLeft;
top = placeTooltipUp ? top - ownBounds.height - offsetTop : top + offsetTop;
}
left = Math.round(left);
top = Math.round(top);
return (
<Tooltip
style={{
top: 0,
left: 0,
transform: `translate(${left}px, ${top}px)`,
...(!unstyled && style),
}}
unstyled={unstyled}
{...otherProps}
>
{children}
</Tooltip>
);
}
export default withBoundingRects(TooltipWithBounds);