Skip to content

Commit 1ce0a12

Browse files
authored
Merge pull request #366 from P0lip/master
Make getPosition() calculate offsets correctly for svg elements
2 parents 3b48785 + a351395 commit 1ce0a12

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/utils/getPosition.js

+23-5
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, desiredPlace, 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
@@ -186,13 +194,23 @@ export default function (e, target, node, place, desiredPlace, effect, offset) {
186194
}
187195
}
188196

197+
const getDimensions = (node) => {
198+
const { height, width } = node.getBoundingClientRect()
199+
return {
200+
height: parseInt(height, 10),
201+
width: parseInt(width, 10)
202+
}
203+
}
204+
189205
// Get current mouse offset
190206
const getCurrentOffset = (e, currentTarget, effect) => {
191207
const boundingClientRect = currentTarget.getBoundingClientRect()
192208
const targetTop = boundingClientRect.top
193209
const targetLeft = boundingClientRect.left
194-
const targetWidth = currentTarget.clientWidth
195-
const targetHeight = currentTarget.clientHeight
210+
const {
211+
width: targetWidth,
212+
height: targetHeight
213+
} = getDimensions(currentTarget)
196214

197215
if (effect === 'float') {
198216
return {

0 commit comments

Comments
 (0)