@blueprintjs/core
Version:
Core styles & components
226 lines (225 loc) • 10.4 kB
JavaScript
/*
* Copyright 2015 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the terms of the LICENSE file distributed with this project.
*/
import * as tslib_1 from "tslib";
import * as classNames from "classnames";
import * as PureRender from "pure-render-decorator";
import * as React from "react";
import * as CSSTransitionGroup from "react-addons-css-transition-group";
import * as Classes from "../../common/classes";
import * as Keys from "../../common/keys";
import { safeInvoke } from "../../common/utils";
import { Portal } from "../portal/portal";
var Overlay = (function (_super) {
tslib_1.__extends(Overlay, _super);
function Overlay(props, context) {
var _this = _super.call(this, props, context) || this;
_this.refHandlers = {
container: function (ref) { return (_this.containerElement = ref); },
};
_this.handleBackdropMouseDown = function (e) {
var _a = _this.props, backdropProps = _a.backdropProps, canOutsideClickClose = _a.canOutsideClickClose, enforceFocus = _a.enforceFocus, onClose = _a.onClose;
if (canOutsideClickClose) {
safeInvoke(onClose, e);
}
if (enforceFocus) {
// make sure document.activeElement is updated before bringing the focus back
_this.bringFocusInsideOverlay();
}
safeInvoke(backdropProps.onMouseDown, e);
};
_this.handleDocumentClick = function (e) {
var _a = _this.props, isOpen = _a.isOpen, onClose = _a.onClose;
var eventTarget = e.target;
var isClickInOverlay = _this.containerElement != null && _this.containerElement.contains(eventTarget);
if (isOpen && _this.props.canOutsideClickClose && !isClickInOverlay) {
// casting to any because this is a native event
safeInvoke(onClose, e);
}
};
_this.handleContentMount = function () {
if (_this.props.isOpen) {
safeInvoke(_this.props.didOpen);
}
if (_this.props.autoFocus) {
_this.bringFocusInsideOverlay();
}
};
_this.handleDocumentFocus = function (e) {
if (_this.props.enforceFocus &&
_this.containerElement != null &&
!_this.containerElement.contains(e.target)) {
// prevent default focus behavior (sometimes auto-scrolls the page)
e.preventDefault();
e.stopImmediatePropagation();
_this.bringFocusInsideOverlay();
}
};
_this.handleKeyDown = function (e) {
var _a = _this.props, canEscapeKeyClose = _a.canEscapeKeyClose, onClose = _a.onClose;
if (e.which === Keys.ESCAPE && canEscapeKeyClose) {
safeInvoke(onClose, e);
// prevent browser-specific escape key behavior (Safari exits fullscreen)
e.preventDefault();
}
};
_this.state = { hasEverOpened: props.isOpen };
return _this;
}
Overlay_1 = Overlay;
Overlay.prototype.render = function () {
// oh snap! no reason to render anything at all if we're being truly lazy
if (this.props.lazy && !this.state.hasEverOpened) {
return null;
}
var _a = this.props, children = _a.children, className = _a.className, inline = _a.inline, isOpen = _a.isOpen, transitionDuration = _a.transitionDuration, transitionName = _a.transitionName;
// add a special class to each child that will automatically set the appropriate
// CSS position mode under the hood. also, make the container focusable so we can
// trap focus inside it (via `enforceFocus`).
var decoratedChildren = React.Children.map(children, function (child) {
return React.cloneElement(child, {
className: classNames(child.props.className, Classes.OVERLAY_CONTENT),
tabIndex: 0,
});
});
var transitionGroup = (React.createElement(CSSTransitionGroup, { transitionAppear: true, transitionAppearTimeout: transitionDuration, transitionEnterTimeout: transitionDuration, transitionLeaveTimeout: transitionDuration, transitionName: transitionName },
this.maybeRenderBackdrop(),
isOpen ? decoratedChildren : null));
var mergedClassName = classNames(Classes.OVERLAY, (_b = {},
_b[Classes.OVERLAY_OPEN] = isOpen,
_b[Classes.OVERLAY_INLINE] = inline,
_b), className);
var elementProps = {
className: mergedClassName,
onKeyDown: this.handleKeyDown,
};
if (inline) {
return (React.createElement("span", tslib_1.__assign({}, elementProps, { ref: this.refHandlers.container }), transitionGroup));
}
else {
return (React.createElement(Portal, tslib_1.__assign({}, elementProps, { containerRef: this.refHandlers.container, onChildrenMount: this.handleContentMount }), transitionGroup));
}
var _b;
};
Overlay.prototype.componentDidMount = function () {
if (this.props.isOpen) {
this.overlayWillOpen();
}
};
Overlay.prototype.componentWillReceiveProps = function (nextProps) {
this.setState({ hasEverOpened: this.state.hasEverOpened || nextProps.isOpen });
};
Overlay.prototype.componentDidUpdate = function (prevProps) {
if (prevProps.isOpen && !this.props.isOpen) {
this.overlayWillClose();
}
else if (!prevProps.isOpen && this.props.isOpen) {
this.overlayWillOpen();
}
};
Overlay.prototype.componentWillUnmount = function () {
this.overlayWillClose();
};
/**
* @public for testing
* @internal
*/
Overlay.prototype.bringFocusInsideOverlay = function () {
var _this = this;
// always delay focus manipulation to just before repaint to prevent scroll jumping
return requestAnimationFrame(function () {
// container ref may be undefined between component mounting and Portal rendering
// activeElement may be undefined in some rare cases in IE
if (_this.containerElement == null || document.activeElement == null || !_this.props.isOpen) {
return;
}
var isFocusOutsideModal = !_this.containerElement.contains(document.activeElement);
if (isFocusOutsideModal) {
// element marked autofocus has higher priority than the other clowns
var autofocusElement = _this.containerElement.query("[autofocus]");
var wrapperElement = _this.containerElement.query("[tabindex]");
if (autofocusElement != null) {
autofocusElement.focus();
}
else if (wrapperElement != null) {
wrapperElement.focus();
}
}
});
};
Overlay.prototype.maybeRenderBackdrop = function () {
var _a = this.props, backdropClassName = _a.backdropClassName, backdropProps = _a.backdropProps, hasBackdrop = _a.hasBackdrop, isOpen = _a.isOpen;
if (hasBackdrop && isOpen) {
return (React.createElement("div", tslib_1.__assign({}, backdropProps, { className: classNames(Classes.OVERLAY_BACKDROP, backdropClassName, backdropProps.className), onMouseDown: this.handleBackdropMouseDown, tabIndex: this.props.canOutsideClickClose ? 0 : null })));
}
else {
return undefined;
}
};
Overlay.prototype.overlayWillClose = function () {
document.removeEventListener("focus", this.handleDocumentFocus, /* useCapture */ true);
document.removeEventListener("mousedown", this.handleDocumentClick);
var openStack = Overlay_1.openStack;
var stackIndex = openStack.indexOf(this);
if (stackIndex !== -1) {
openStack.splice(stackIndex, 1);
if (openStack.length > 0) {
var lastOpenedOverlay = Overlay_1.getLastOpened();
if (lastOpenedOverlay.props.enforceFocus) {
document.addEventListener("focus", lastOpenedOverlay.handleDocumentFocus, /* useCapture */ true);
}
}
if (openStack.filter(function (o) { return !o.props.inline && o.props.hasBackdrop; }).length === 0) {
document.body.classList.remove(Classes.OVERLAY_OPEN);
}
}
};
Overlay.prototype.overlayWillOpen = function () {
var openStack = Overlay_1.openStack;
if (openStack.length > 0) {
document.removeEventListener("focus", Overlay_1.getLastOpened().handleDocumentFocus, /* useCapture */ true);
}
openStack.push(this);
if (this.props.canOutsideClickClose && !this.props.hasBackdrop) {
document.addEventListener("mousedown", this.handleDocumentClick);
}
if (this.props.enforceFocus) {
document.addEventListener("focus", this.handleDocumentFocus, /* useCapture */ true);
}
if (this.props.inline) {
safeInvoke(this.props.didOpen);
if (this.props.autoFocus) {
this.bringFocusInsideOverlay();
}
}
else if (this.props.hasBackdrop) {
// add a class to the body to prevent scrolling of content below the overlay
document.body.classList.add(Classes.OVERLAY_OPEN);
}
};
Overlay.displayName = "Blueprint.Overlay";
Overlay.defaultProps = {
autoFocus: true,
backdropProps: {},
canEscapeKeyClose: true,
canOutsideClickClose: true,
enforceFocus: true,
hasBackdrop: true,
inline: false,
isOpen: false,
lazy: true,
transitionDuration: 300,
transitionName: "pt-overlay",
};
Overlay.openStack = [];
Overlay.getLastOpened = function () { return Overlay_1.openStack[Overlay_1.openStack.length - 1]; };
Overlay = Overlay_1 = tslib_1.__decorate([
PureRender
], Overlay);
return Overlay;
var Overlay_1;
}(React.Component));
export { Overlay };
export var OverlayFactory = React.createFactory(Overlay);