-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathSessionTooltip.tsx
More file actions
177 lines (160 loc) · 4.33 KB
/
SessionTooltip.tsx
File metadata and controls
177 lines (160 loc) · 4.33 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
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
import {
type ReactNode,
type RefObject,
type SessionDataTestId,
useRef,
useState,
useEffect,
type Dispatch,
} from 'react';
import styled, { type CSSProperties } from 'styled-components';
import useDebounce from 'react-use/lib/useDebounce';
import {
type HorizontalAlignment,
SessionPopoverContent,
type VerticalPosition,
} from './SessionPopover';
const StyledTooltipTrigger = styled.div`
position: relative;
width: max-content;
height: max-content;
`;
export type TooltipProps = {
children: ReactNode;
content: ReactNode;
open?: boolean;
onMouseEnter?: () => void;
onMouseLeave?: () => void;
maxContentWidth?: string;
loading?: boolean;
style?: CSSProperties;
dataTestId?: SessionDataTestId;
verticalPosition?: VerticalPosition;
horizontalPosition?: HorizontalAlignment;
debounceTimeout?: number;
containerMarginTop?: number;
containerMarginBottom?: number;
containerMarginLeft?: number;
containerMarginRight?: number;
};
export type PopoverTriggerPosition = {
x: number;
y: number;
width: number;
height: number;
offsetX?: number;
};
export type WithPopoverPosition = { triggerPosition: PopoverTriggerPosition | null };
export type WithSetPopoverPosition = {
setTriggerPosition: Dispatch<PopoverTriggerPosition | null>;
};
export const defaultTriggerPos: PopoverTriggerPosition = { x: 0, y: 0, width: 0, height: 0 };
export function getTriggerPositionFromBoundingClientRect(rect: DOMRect): PopoverTriggerPosition {
return {
x: rect.left,
y: rect.top,
width: rect.width,
height: rect.height,
};
}
export const getTriggerPositionFromId = (id: string): PopoverTriggerPosition => {
const el = document.getElementById(id);
if (!el) {
return defaultTriggerPos;
}
return getTriggerPositionFromBoundingClientRect(el.getBoundingClientRect());
};
// Returns null if the ref is null
export const getTriggerPosition = (ref: RefObject<HTMLElement | null>) => {
if (!ref.current) {
return null;
}
return getTriggerPositionFromBoundingClientRect(ref.current.getBoundingClientRect());
};
// Returns null if the ref is null
export const useTriggerPosition = (
ref: RefObject<HTMLElement | null>
): PopoverTriggerPosition | null => {
return getTriggerPosition(ref);
};
export const SessionTooltip = ({
children,
content,
onMouseEnter,
onMouseLeave,
maxContentWidth,
open,
loading = false,
debounceTimeout = 150,
style,
dataTestId,
verticalPosition,
horizontalPosition,
containerMarginBottom,
containerMarginLeft,
containerMarginRight,
containerMarginTop,
}: TooltipProps) => {
const [hovered, setHovered] = useState(false);
const [debouncedHover, setDebouncedHover] = useState(false);
const ref = useRef<HTMLDivElement>(null);
const getTriggerPos = () => {
if (!ref.current) {
return defaultTriggerPos;
}
return getTriggerPositionFromBoundingClientRect(ref.current.getBoundingClientRect());
};
useEffect(() => {
if (!debouncedHover) {
return;
}
const handleScroll = () => {
setHovered(false);
setDebouncedHover(false);
};
window.addEventListener('scroll', handleScroll, true);
// eslint-disable-next-line consistent-return
return () => window.removeEventListener('scroll', handleScroll, true);
}, [debouncedHover]);
useDebounce(
() => {
setDebouncedHover(hovered);
},
debounceTimeout,
[hovered]
);
const isVisible = open || debouncedHover;
return (
<StyledTooltipTrigger
ref={ref}
onMouseEnter={() => {
onMouseEnter?.();
setHovered(true);
setDebouncedHover(true);
}}
onMouseLeave={() => {
onMouseLeave?.();
setHovered(false);
}}
style={style}
data-testid={dataTestId}
>
{children}
<SessionPopoverContent
triggerPosition={isVisible ? getTriggerPos() : defaultTriggerPos}
open={isVisible}
loading={loading}
maxWidth={maxContentWidth}
isTooltip={true}
verticalPosition={verticalPosition}
horizontalPosition={horizontalPosition}
containerMarginBottom={containerMarginBottom}
containerMarginTop={containerMarginTop}
containerMarginLeft={containerMarginLeft}
containerMarginRight={containerMarginRight}
>
{content}
</SessionPopoverContent>
</StyledTooltipTrigger>
);
};