Skip to content

Commit

Permalink
Merge pull request react-bootstrap#1053 from benekastah/svg-overlay-t…
Browse files Browse the repository at this point in the history
…arget

Overlays don't position themselves properly when target is an SVG
  • Loading branch information
jquense committed Jul 28, 2015
2 parents 418b1f6 + c8d9a33 commit 23f3e0c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
20 changes: 20 additions & 0 deletions src/utils/domUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,25 @@ function getPosition(elem, offsetParent) {
};
}

/**
* Get an element's size
*
* @param {HTMLElement} elem
* @returns {{width: number, height: number}}
*/
function getSize(elem) {
let rect = {
width: elem.offsetWidth || 0,
height: elem.offsetHeight || 0
};
if (typeof elem.getBoundingClientRect !== 'undefined') {
let {width, height} = elem.getBoundingClientRect();
rect.width = width || rect.width;
rect.height = height || rect.height;
}
return rect;
}

/**
* Get parent element
*
Expand Down Expand Up @@ -187,6 +206,7 @@ export default {
getComputedStyles,
getOffset,
getPosition,
getSize,
activeElement: getActiveElement,
offsetParent: offsetParentFunc
};
27 changes: 10 additions & 17 deletions src/utils/overlayPositionUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,35 @@ import domUtils from './domUtils';
const utils = {

getContainerDimensions(containerNode) {
let width, height, scroll;
let size, scroll;

if (containerNode.tagName === 'BODY') {
width = window.innerWidth;
height = window.innerHeight;
size = {
width: window.innerWidth,
height: window.innerHeight
};
scroll =
domUtils.ownerDocument(containerNode).documentElement.scrollTop ||
containerNode.scrollTop;
} else {
width = containerNode.offsetWidth;
height = containerNode.offsetHeight;
size = domUtils.getSize(containerNode);
scroll = containerNode.scrollTop;
}

return {width, height, scroll};
return {...size, scroll};
},

getPosition(target, container) {
const offset = container.tagName === 'BODY' ?
domUtils.getOffset(target) : domUtils.getPosition(target, container);

return {
...offset,

// In Firefox, the target does not have a offsetHeight or offsetWidth
// property. For now, default them to 0 to keep code from breaking.
height: target.offsetHeight || 0,
width: target.offsetWidth || 0
};
const size = domUtils.getSize(target);
return {...offset, ...size};
},

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

const overlayHeight = overlayNode.offsetHeight;
const overlayWidth = overlayNode.offsetWidth;
const {height: overlayHeight, width: overlayWidth} = domUtils.getSize(overlayNode);

let positionLeft, positionTop, arrowOffsetLeft, arrowOffsetTop;

Expand Down

0 comments on commit 23f3e0c

Please sign in to comment.