Skip to content

Commit

Permalink
Bug fix for multiple class exclusions and added excludeScrollbar prop…
Browse files Browse the repository at this point in the history
…erty.
  • Loading branch information
Greg Taylor committed Oct 18, 2018
1 parent e5a2313 commit f508bb2
Showing 1 changed file with 36 additions and 15 deletions.
51 changes: 36 additions & 15 deletions src/OutsideClickHandler.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const propTypes = forbidExtraProps({
children: PropTypes.node.isRequired,
onOutsideClick: PropTypes.func.isRequired,
classWhiteList: PropTypes.string,
excludeScrollbar: PropTypes.bool,
disabled: PropTypes.bool,
useCapture: PropTypes.bool,
display: PropTypes.oneOf(objectValues(DISPLAY)),
Expand Down Expand Up @@ -63,10 +64,23 @@ export default class OutsideClickHandler extends React.Component {
// descendant tree, even when dragged. This should also get triggered on
// touch devices.
onMouseDown(e) {
const { useCapture } = this.props;
const { useCapture, excludeScrollbar, classWhiteList } = this.props;

var w = window,
d = document,
de = d.documentElement,
g = document.body,
innerY = w.clientHeight || de.clientHeight || g.clientHeight,
innerX = w.clientWidth || de.clientWidth || g.clientWidth;

if ((e.clientX >= innerX) || (e.clientY >= innerY) && excludeScrollbar) {
return;
}

const isDescendantOfRoot = this.childNode && this.childNode.contains(e.target);
if (!isDescendantOfRoot) {
let isChildOfWhitelistNode = this.isChildOfWhiteList(e, classWhiteList);

if (!isDescendantOfRoot && !isChildOfWhitelistNode) {
this.removeMouseUp = addEventListener(
document,
'mouseup',
Expand All @@ -76,28 +90,33 @@ export default class OutsideClickHandler extends React.Component {
}
}

// Use mousedown/mouseup to enforce that clicks remain outside the root's
// descendant tree, even when dragged. This should also get triggered on
// touch devices.
onMouseUp(e) {
const { onOutsideClick, classWhiteList } = this.props;

const isDescendantOfRoot = this.childNode && this.childNode.contains(e.target);
isChildOfWhiteList = (e, classWhiteList) => {
let isChildOfWhitelistNode = false;

if (classWhiteList) {
let classWhiteListArr = classWhiteList.split(" ");

classWhiteListArr.forEach(c => {

isChildOfWhitelistNode = this.hasParentClass(e.target, c);
for (let i = 0; i < classWhiteListArr.length; i++) {
isChildOfWhitelistNode = this.hasParentClass(e.target, classWhiteListArr[i]);

if (isChildOfWhitelistNode) {
return;
return true;
}
});
}
}

return isChildOfWhitelistNode;
}

// Use mousedown/mouseup to enforce that clicks remain outside the root's
// descendant tree, even when dragged. This should also get triggered on
// touch devices.
onMouseUp(e) {
const { onOutsideClick, classWhiteList } = this.props;

const isDescendantOfRoot = this.childNode && this.childNode.contains(e.target);
let isChildOfWhitelistNode = this.isChildOfWhiteList(e, classWhiteList);

if (this.removeMouseUp) this.removeMouseUp();
this.removeMouseUp = null;

Expand All @@ -107,7 +126,9 @@ export default class OutsideClickHandler extends React.Component {
}

hasParentClass = (element, classname) => {
if (element.className && element.className.split(' ').indexOf(classname) >= 0) {
if (element.className
&& (typeof element.className === 'string' || element.className instanceof String)
&& element.className.split(' ').indexOf(classname) >= 0) {
return true;
}

Expand Down

0 comments on commit f508bb2

Please sign in to comment.