-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
TooltipRenderedOnPageBody.js
154 lines (133 loc) · 5.45 KB
/
TooltipRenderedOnPageBody.js
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
import React, {useLayoutEffect, useEffect, useState, useRef, useMemo} from 'react';
import PropTypes from 'prop-types';
import {Animated, View} from 'react-native';
import ReactDOM from 'react-dom';
import getTooltipStyles from '../../styles/getTooltipStyles';
import Text from '../Text';
import Log from '../../libs/Log';
const propTypes = {
/** Window width */
windowWidth: PropTypes.number.isRequired,
/** Tooltip Animation value */
// eslint-disable-next-line react/forbid-prop-types
animation: PropTypes.object.isRequired,
/** The distance between the left side of the wrapper view and the left side of the window */
xOffset: PropTypes.number.isRequired,
/** The distance between the top of the wrapper view and the top of the window */
yOffset: PropTypes.number.isRequired,
/** The width of the tooltip's target */
targetWidth: PropTypes.number.isRequired,
/** The height of the tooltip's target */
targetHeight: PropTypes.number.isRequired,
/** Any additional amount to manually adjust the horizontal position of the tooltip.
A positive value shifts the tooltip to the right, and a negative value shifts it to the left. */
shiftHorizontal: PropTypes.number,
/** Any additional amount to manually adjust the vertical position of the tooltip.
A positive value shifts the tooltip down, and a negative value shifts it up. */
shiftVertical: PropTypes.number,
/** Text to be shown in the tooltip */
text: PropTypes.string.isRequired,
/** Maximum number of lines to show in tooltip */
numberOfLines: PropTypes.number.isRequired,
/** Number of pixels to set max-width on tooltip */
maxWidth: PropTypes.number,
/** Render custom content inside the tooltip. Note: This cannot be used together with the text props. */
renderTooltipContent: PropTypes.func,
};
const defaultProps = {
shiftHorizontal: 0,
shiftVertical: 0,
renderTooltipContent: undefined,
maxWidth: 0,
};
// Props will change frequently.
// On every tooltip hover, we update the position in state which will result in re-rendering.
// We also update the state on layout changes which will be triggered often.
// There will be n number of tooltip components in the page.
// It's good to memoize this one.
const TooltipRenderedOnPageBody = (props) => {
// The width of tooltip's inner content. Has to be undefined in the beginning
// as a width of 0 will cause the content to be rendered of a width of 0,
// which prevents us from measuring it correctly.
const [contentMeasuredWidth, setContentMeasuredWidth] = useState(undefined);
// The height of tooltip's wrapper.
const [wrapperMeasuredHeight, setWrapperMeasuredHeight] = useState(undefined);
const contentRef = useRef();
const rootWrapper = useRef();
useEffect(() => {
if (!props.renderTooltipContent || !props.text) {
return;
}
Log.warn('Developer error: Cannot use both text and renderTooltipContent props at the same time in <TooltipRenderedOnPageBody />!');
}, [props.text, props.renderTooltipContent]);
useLayoutEffect(() => {
// Calculate the tooltip width and height before the browser repaints the screen to prevent flicker
// because of the late update of the width and the height from onLayout.
setContentMeasuredWidth(contentRef.current.getBoundingClientRect().width);
setWrapperMeasuredHeight(rootWrapper.current.getBoundingClientRect().height);
}, []);
const {animationStyle, rootWrapperStyle, textStyle, pointerWrapperStyle, pointerStyle} = useMemo(
() =>
getTooltipStyles(
props.animation,
props.windowWidth,
props.xOffset,
props.yOffset,
props.targetWidth,
props.targetHeight,
props.maxWidth,
contentMeasuredWidth,
wrapperMeasuredHeight,
props.shiftHorizontal,
props.shiftVertical,
rootWrapper.current,
),
[
props.animation,
props.windowWidth,
props.xOffset,
props.yOffset,
props.targetWidth,
props.targetHeight,
props.maxWidth,
contentMeasuredWidth,
wrapperMeasuredHeight,
props.shiftHorizontal,
props.shiftVertical,
],
);
let content;
if (props.renderTooltipContent) {
content = <View ref={contentRef}>{props.renderTooltipContent()}</View>;
} else {
content = (
<Text
numberOfLines={props.numberOfLines}
style={textStyle}
>
<Text
style={textStyle}
ref={contentRef}
>
{props.text}
</Text>
</Text>
);
}
return ReactDOM.createPortal(
<Animated.View
ref={rootWrapper}
style={[rootWrapperStyle, animationStyle]}
>
{content}
<View style={pointerWrapperStyle}>
<View style={pointerStyle} />
</View>
</Animated.View>,
document.querySelector('body'),
);
};
TooltipRenderedOnPageBody.propTypes = propTypes;
TooltipRenderedOnPageBody.defaultProps = defaultProps;
TooltipRenderedOnPageBody.displayName = 'TooltipRenderedOnPageBody';
export default React.memo(TooltipRenderedOnPageBody);