Skip to content

Commit d865519

Browse files
committed
Add support when parent is under transform
1 parent cfaca9a commit d865519

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

example/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const Test = React.createClass({
9191
<p className="sub-title">Use everything as tooltip</p>
9292

9393
<div className="example-jsx">
94-
<div className="side">
94+
<div className="side" style={{ transform: 'translate3d(5px, 5px, 5px)' }}>
9595
<a data-tip data-for='happyFace'> d(`・∀・)b </a>
9696
<ReactTooltip id='happyFace' type="error"><span>Show happy face</span></ReactTooltip>
9797
</div>

src/utils/getPosition.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export default function (e, target, node, place, effect, offset) {
2424
const windowWidth = window.innerWidth
2525
const windowHeight = window.innerHeight
2626

27+
const {parentTop, parentLeft} = getParent(target)
28+
2729
// Get the edge offset of the tooltip
2830
const getTipOffsetLeft = (place) => {
2931
const offset_X = defaultOffset[place].l
@@ -153,8 +155,8 @@ export default function (e, target, node, place, effect, offset) {
153155
return {
154156
isNewState: false,
155157
position: {
156-
left: getTipOffsetLeft(place),
157-
top: getTipOffsetTop(place)
158+
left: getTipOffsetLeft(place) - parentLeft,
159+
top: getTipOffsetTop(place) - parentTop
158160
}
159161
}
160162
}
@@ -186,8 +188,9 @@ const getDefaultPosition = (effect, targetWidth, targetHeight, tipWidth, tipHeig
186188
let right
187189
let bottom
188190
let left
189-
const disToMouse = 8
191+
const disToMouse = 3
190192
const triangleHeight = 2
193+
const cursorHeight = 12 // Optimize for float bottom only, cause the cursor will hide the tooltip
191194

192195
if (effect === 'float') {
193196
top = {
@@ -199,8 +202,8 @@ const getDefaultPosition = (effect, targetWidth, targetHeight, tipWidth, tipHeig
199202
bottom = {
200203
l: -(tipWidth / 2),
201204
r: tipWidth / 2,
202-
t: disToMouse,
203-
b: tipHeight + disToMouse + triangleHeight
205+
t: disToMouse + cursorHeight,
206+
b: tipHeight + disToMouse + triangleHeight + cursorHeight
204207
}
205208
left = {
206209
l: -(tipWidth + disToMouse + triangleHeight),
@@ -266,3 +269,17 @@ const calculateOffset = (offset) => {
266269

267270
return {extraOffset_X, extraOffset_Y}
268271
}
272+
273+
// Get the offset of the parent elements
274+
const getParent = (currentTarget) => {
275+
let currentParent = currentTarget.parentElement
276+
while (currentParent) {
277+
if (currentParent.style.transform.length > 0) break
278+
currentParent = currentParent.parentElement
279+
}
280+
281+
const parentTop = currentParent && currentParent.getBoundingClientRect().top || 0
282+
const parentLeft = currentParent && currentParent.getBoundingClientRect().left || 0
283+
284+
return {parentTop, parentLeft}
285+
}

0 commit comments

Comments
 (0)