Created
January 24, 2018 01:10
-
-
Save tyscorp/2bc90d8c3c90cbd701a63aaffb87c45f to your computer and use it in GitHub Desktop.
Overlay patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Utils, Overlay } from '@blueprintjs/core'; | |
import PropTypes from 'prop-types'; | |
import React from 'react'; | |
function handleDocumentClick(e) { | |
const { isOpen, onClose } = this.props; | |
const eventTarget = e.target; | |
const isClickInDescendantOverlay = Array.from(document.querySelectorAll(`[data-overlay-id="${this.overlayId}"]`)) | |
.filter((elem) => elem && elem.contains) | |
.filter((elem) => this.overlayDepth < Number(elem.dataset.overlayDepth)) | |
.some((elem) => elem.contains(eventTarget)); | |
const isClickInOverlay = | |
(this.containerElement != null && this.containerElement.contains(eventTarget)) || isClickInDescendantOverlay; | |
if (isOpen && this.props.canOutsideClickClose && !isClickInOverlay) { | |
Utils.safeInvoke(onClose, e); | |
} | |
} | |
Overlay.contextTypes = { | |
overlayDepth: PropTypes.number, | |
overlayId: PropTypes.number | |
}; | |
Overlay.childContextTypes = { | |
overlayDepth: PropTypes.number, | |
overlayId: PropTypes.number | |
}; | |
let overlayId = 0; | |
Overlay.prototype.componentWillMount = function componentWillMount() { | |
this.overlayDepth = (this.context.overlayDepth && this.context.overlayDepth + 1) || 1; | |
this.overlayId = this.context.overlayId || overlayId++; | |
this.handleDocumentClick = handleDocumentClick.bind(this); | |
}; | |
Overlay.prototype.getChildContext = function getChildContext() { | |
return { | |
overlayDepth: this.overlayDepth, | |
overlayId: this.overlayId | |
}; | |
}; | |
const componentDidUpdate = Overlay.prototype.componentDidUpdate; | |
Overlay.prototype.componentDidUpdate = function(prevProps) { | |
componentDidUpdate.call(this, prevProps); | |
if (this.containerElement) { | |
this.containerElement.dataset.overlayDepth = this.overlayDepth; | |
this.containerElement.dataset.overlayId = this.overlayId; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment