Skip to content

Commit 9238bab

Browse files
delayCalculatePosition prop (#465)
* delayCalculatePosition prop * linter * declaration file Co-authored-by: James Liew <[email protected]>
1 parent 4020212 commit 9238bab

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

src/components/Tooltip/Tooltip.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface TooltipProps {
4040
onShow(): void
4141

4242
isVisible?: boolean
43+
44+
delayCalculatePosition?: boolean
4345
}
4446

4547
declare const Tooltip: React.ComponentClass<TooltipProps>

src/components/Tooltip/Tooltip.js

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Tooltip extends PureComponent {
3535
onDismiss: PropTypes.func,
3636
onShow: PropTypes.func,
3737
isVisible: PropTypes.bool,
38+
delayCalculatePosition: PropTypes.bool,
3839
}
3940

4041
static defaultProps = {
@@ -99,6 +100,7 @@ class Tooltip extends PureComponent {
99100
snacksStyle,
100101
isVisible,
101102
overlayStyle,
103+
delayCalculatePosition,
102104
} = this.props
103105

104106
return (
@@ -110,6 +112,7 @@ class Tooltip extends PureComponent {
110112
show={isVisible || this.state.show}
111113
onRootClose={this.handleHideToolTip}
112114
style={overlayStyle}
115+
delayCalculatePosition={delayCalculatePosition}
113116
>
114117
<InnerToolTip
115118
size={size}

src/components/Tooltip/TooltipOverlay.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,36 @@ class TooltipOverlay extends PureComponent {
1313
onRootClose: PropTypes.func,
1414
rootCloseEnabled: PropTypes.bool,
1515
style: PropTypes.shape({}),
16+
delayCalculatePosition: PropTypes.bool,
1617
}
1718

1819
static defaultProps = {
1920
rootCloseEnabled: true,
2021
}
2122

2223
render() {
23-
const { show, children, target, placement, onRootClose, rootCloseEnabled, style } = this.props
24+
const {
25+
show,
26+
children,
27+
target,
28+
placement,
29+
onRootClose,
30+
rootCloseEnabled,
31+
style,
32+
delayCalculatePosition,
33+
} = this.props
2434

2535
if (!show) {
2636
return false
2737
}
2838

2939
let child = (
30-
<TooltipPosition style={style} target={target} placement={placement}>
40+
<TooltipPosition
41+
style={style}
42+
target={target}
43+
placement={placement}
44+
delayCalculatePosition={delayCalculatePosition}
45+
>
3146
{children}
3247
</TooltipPosition>
3348
)

src/components/Tooltip/TooltipPosition.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class TooltipPosition extends PureComponent {
1919
target: PropTypes.oneOfType([PropTypes.node, PropTypes.func]).isRequired,
2020
placement: PropTypes.oneOf(['top', 'left', 'right', 'bottom']).isRequired,
2121
style: PropTypes.shape({}),
22+
delayCalculatePosition: PropTypes.bool,
2223
}
2324

2425
state = {
@@ -27,7 +28,18 @@ class TooltipPosition extends PureComponent {
2728
}
2829

2930
componentDidMount() {
30-
this.calculatePosition()
31+
const { delayCalculatePosition } = this.props
32+
33+
if (delayCalculatePosition) {
34+
// modified July 28, 2021. Need to delay calculatePosition function because documentWidth, overlay and targetRect may not be ready with the right sizes
35+
// if we calculatePosition with these wrong sizes, we can't recalculate to the correct one in the next few frames, due to the side effects of calculatePosition
36+
// (overlayRect values already set)
37+
setTimeout(() => {
38+
this.calculatePosition()
39+
}, 300)
40+
} else {
41+
this.calculatePosition()
42+
}
3143
}
3244

3345
getTarget = () => {

0 commit comments

Comments
 (0)