Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import PropTypes from 'prop-types'

export default class ClickOutside extends Component {
static propTypes = {
onClickOutside: PropTypes.func.isRequired
onClickOutside: PropTypes.func.isRequired,
ignored: PropTypes.array
}

static defaultProps = {
ignored: []
}

constructor(props) {
Expand Down Expand Up @@ -36,6 +41,18 @@ export default class ClickOutside extends Component {
if (e.type === 'click' && this.isTouch) return
const { onClickOutside } = this.props
const el = this.container
if (el && !el.contains(e.target)) onClickOutside(e)
if (el && !el.contains(e.target) && !this.isClickedIgnoredElements(e.target)) onClickOutside(e)
}

isClickedIgnoredElements = clicked => {
const { ignore } = this.props;

if (!ignore || (ignore && !ignore.length)) {
return;
}

return ignore
.map(el => clicked.isEqualNode(document.querySelector(el)))
.some(boolean => boolean === true);
};
}