Skip to content

Commit 23f3e0c

Browse files
committed
Merge pull request react-bootstrap#1053 from benekastah/svg-overlay-target
Overlays don't position themselves properly when target is an SVG
2 parents 418b1f6 + c8d9a33 commit 23f3e0c

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

src/utils/domUtils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,25 @@ function getPosition(elem, offsetParent) {
137137
};
138138
}
139139

140+
/**
141+
* Get an element's size
142+
*
143+
* @param {HTMLElement} elem
144+
* @returns {{width: number, height: number}}
145+
*/
146+
function getSize(elem) {
147+
let rect = {
148+
width: elem.offsetWidth || 0,
149+
height: elem.offsetHeight || 0
150+
};
151+
if (typeof elem.getBoundingClientRect !== 'undefined') {
152+
let {width, height} = elem.getBoundingClientRect();
153+
rect.width = width || rect.width;
154+
rect.height = height || rect.height;
155+
}
156+
return rect;
157+
}
158+
140159
/**
141160
* Get parent element
142161
*
@@ -187,6 +206,7 @@ export default {
187206
getComputedStyles,
188207
getOffset,
189208
getPosition,
209+
getSize,
190210
activeElement: getActiveElement,
191211
offsetParent: offsetParentFunc
192212
};

src/utils/overlayPositionUtils.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,35 @@ import domUtils from './domUtils';
33
const utils = {
44

55
getContainerDimensions(containerNode) {
6-
let width, height, scroll;
6+
let size, scroll;
77

88
if (containerNode.tagName === 'BODY') {
9-
width = window.innerWidth;
10-
height = window.innerHeight;
9+
size = {
10+
width: window.innerWidth,
11+
height: window.innerHeight
12+
};
1113
scroll =
1214
domUtils.ownerDocument(containerNode).documentElement.scrollTop ||
1315
containerNode.scrollTop;
1416
} else {
15-
width = containerNode.offsetWidth;
16-
height = containerNode.offsetHeight;
17+
size = domUtils.getSize(containerNode);
1718
scroll = containerNode.scrollTop;
1819
}
1920

20-
return {width, height, scroll};
21+
return {...size, scroll};
2122
},
2223

2324
getPosition(target, container) {
2425
const offset = container.tagName === 'BODY' ?
2526
domUtils.getOffset(target) : domUtils.getPosition(target, container);
26-
27-
return {
28-
...offset,
29-
30-
// In Firefox, the target does not have a offsetHeight or offsetWidth
31-
// property. For now, default them to 0 to keep code from breaking.
32-
height: target.offsetHeight || 0,
33-
width: target.offsetWidth || 0
34-
};
27+
const size = domUtils.getSize(target);
28+
return {...offset, ...size};
3529
},
3630

3731
calcOverlayPosition(placement, overlayNode, target, container, padding) {
3832
const childOffset = utils.getPosition(target, container);
3933

40-
const overlayHeight = overlayNode.offsetHeight;
41-
const overlayWidth = overlayNode.offsetWidth;
34+
const {height: overlayHeight, width: overlayWidth} = domUtils.getSize(overlayNode);
4235

4336
let positionLeft, positionTop, arrowOffsetLeft, arrowOffsetTop;
4437

0 commit comments

Comments
 (0)