Skip to content

Commit 07be8c5

Browse files
committed
Make getPosition() calculate offsets correctly for svg elements
1 parent 10b8e43 commit 07be8c5

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/utils/getPosition.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
* - `position` {OBject} {left: {Number}, top: {Number}}
1616
*/
1717
export default function (e, target, node, place, effect, offset) {
18-
const tipWidth = node.clientWidth
19-
const tipHeight = node.clientHeight
18+
const {
19+
width: tipWidth,
20+
height: tipHeight
21+
} = getDimensions(node)
22+
23+
const {
24+
width: targetWidth,
25+
height: targetHeight
26+
} = getDimensions(target)
27+
2028
const {mouseX, mouseY} = getCurrentOffset(e, target, effect)
21-
const defaultOffset = getDefaultPosition(effect, target.clientWidth, target.clientHeight, tipWidth, tipHeight)
29+
const defaultOffset = getDefaultPosition(effect, targetWidth, targetHeight, tipWidth, tipHeight)
2230
const {extraOffset_X, extraOffset_Y} = calculateOffset(offset)
2331

2432
const windowWidth = window.innerWidth
@@ -161,13 +169,23 @@ export default function (e, target, node, place, effect, offset) {
161169
}
162170
}
163171

172+
const getDimensions = (node) => {
173+
const { height, width } = node.getBoundingClientRect()
174+
return {
175+
height: parseInt(height, 10),
176+
width: parseInt(width, 10)
177+
}
178+
}
179+
164180
// Get current mouse offset
165181
const getCurrentOffset = (e, currentTarget, effect) => {
166182
const boundingClientRect = currentTarget.getBoundingClientRect()
167183
const targetTop = boundingClientRect.top
168184
const targetLeft = boundingClientRect.left
169-
const targetWidth = currentTarget.clientWidth
170-
const targetHeight = currentTarget.clientHeight
185+
const {
186+
width: targetWidth,
187+
height: targetHeight
188+
} = getDimensions(currentTarget)
171189

172190
if (effect === 'float') {
173191
return {

0 commit comments

Comments
 (0)