UNPKG

21.7 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.Overlay = void 0;
19var tslib_1 = require("tslib");
20var classnames_1 = tslib_1.__importDefault(require("classnames"));
21var React = tslib_1.__importStar(require("react"));
22var react_dom_1 = require("react-dom");
23var react_transition_group_1 = require("react-transition-group");
24var common_1 = require("../../common");
25var props_1 = require("../../common/props");
26var utils_1 = require("../../common/utils");
27var portal_1 = require("../portal/portal");
28/**
29 * Overlay component.
30 *
31 * @see https://blueprintjs.com/docs/#core/components/overlay
32 */
33var Overlay = /** @class */ (function (_super) {
34 tslib_1.__extends(Overlay, _super);
35 function Overlay() {
36 var _this = _super !== null && _super.apply(this, arguments) || this;
37 _this.isAutoFocusing = false;
38 _this.state = {
39 hasEverOpened: _this.props.isOpen,
40 };
41 // an HTMLElement that contains the backdrop and any children, to query for focus target
42 _this.containerElement = null;
43 // An empty, keyboard-focusable div at the beginning of the Overlay content
44 _this.startFocusTrapElement = null;
45 // An empty, keyboard-focusable div at the end of the Overlay content
46 _this.endFocusTrapElement = null;
47 _this.refHandlers = {
48 // HACKHACK: see https://github.com/palantir/blueprint/issues/3979
49 /* eslint-disable-next-line react/no-find-dom-node */
50 container: function (ref) { return (_this.containerElement = (0, react_dom_1.findDOMNode)(ref)); },
51 endFocusTrap: function (ref) { return (_this.endFocusTrapElement = ref); },
52 startFocusTrap: function (ref) { return (_this.startFocusTrapElement = ref); },
53 };
54 _this.maybeRenderChild = function (child) {
55 if ((0, utils_1.isFunction)(child)) {
56 child = child();
57 }
58 if (child == null) {
59 return null;
60 }
61 // add a special class to each child element that will automatically set the appropriate
62 // CSS position mode under the hood.
63 var decoratedChild = typeof child === "object" ? (React.cloneElement(child, {
64 className: (0, classnames_1.default)(child.props.className, common_1.Classes.OVERLAY_CONTENT),
65 })) : (React.createElement("span", { className: common_1.Classes.OVERLAY_CONTENT }, child));
66 var _a = _this.props, onOpening = _a.onOpening, onOpened = _a.onOpened, onClosing = _a.onClosing, transitionDuration = _a.transitionDuration, transitionName = _a.transitionName;
67 return (React.createElement(react_transition_group_1.CSSTransition, { classNames: transitionName, onEntering: onOpening, onEntered: onOpened, onExiting: onClosing, onExited: _this.handleTransitionExited, timeout: transitionDuration, addEndListener: _this.handleTransitionAddEnd }, decoratedChild));
68 };
69 /**
70 * Ensures repeatedly pressing shift+tab keeps focus inside the Overlay. Moves focus to
71 * the `endFocusTrapElement` or the first keyboard-focusable element in the Overlay (excluding
72 * the `startFocusTrapElement`), depending on whether the element losing focus is inside the
73 * Overlay.
74 */
75 _this.handleStartFocusTrapElementFocus = function (e) {
76 var _a;
77 if (!_this.props.enforceFocus || _this.isAutoFocusing) {
78 return;
79 }
80 // e.relatedTarget will not be defined if this was a programmatic focus event, as is the
81 // case when we call this.bringFocusInsideOverlay() after a user clicked on the backdrop.
82 // Otherwise, we're handling a user interaction, and we should wrap around to the last
83 // element in this transition group.
84 if (e.relatedTarget != null &&
85 _this.containerElement.contains(e.relatedTarget) &&
86 e.relatedTarget !== _this.endFocusTrapElement) {
87 (_a = _this.endFocusTrapElement) === null || _a === void 0 ? void 0 : _a.focus({ preventScroll: true });
88 }
89 };
90 /**
91 * Wrap around to the end of the dialog if `enforceFocus` is enabled.
92 */
93 _this.handleStartFocusTrapElementKeyDown = function (e) {
94 var _a;
95 if (!_this.props.enforceFocus) {
96 return;
97 }
98 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
99 /* eslint-disable-next-line deprecation/deprecation */
100 if (e.shiftKey && e.which === common_1.Keys.TAB) {
101 var lastFocusableElement = _this.getKeyboardFocusableElements().pop();
102 if (lastFocusableElement != null) {
103 lastFocusableElement.focus();
104 }
105 else {
106 (_a = _this.endFocusTrapElement) === null || _a === void 0 ? void 0 : _a.focus({ preventScroll: true });
107 }
108 }
109 };
110 /**
111 * Ensures repeatedly pressing tab keeps focus inside the Overlay. Moves focus to the
112 * `startFocusTrapElement` or the last keyboard-focusable element in the Overlay (excluding the
113 * `startFocusTrapElement`), depending on whether the element losing focus is inside the
114 * Overlay.
115 */
116 _this.handleEndFocusTrapElementFocus = function (e) {
117 var _a, _b;
118 // No need for this.props.enforceFocus check here because this element is only rendered
119 // when that prop is true.
120 // During user interactions, e.relatedTarget will be defined, and we should wrap around to the
121 // "start focus trap" element.
122 // Otherwise, we're handling a programmatic focus event, which can only happen after a user
123 // presses shift+tab from the first focusable element in the overlay.
124 if (e.relatedTarget != null &&
125 _this.containerElement.contains(e.relatedTarget) &&
126 e.relatedTarget !== _this.startFocusTrapElement) {
127 var firstFocusableElement = _this.getKeyboardFocusableElements().shift();
128 // ensure we don't re-focus an already active element by comparing against e.relatedTarget
129 if (!_this.isAutoFocusing && firstFocusableElement != null && firstFocusableElement !== e.relatedTarget) {
130 firstFocusableElement.focus();
131 }
132 else {
133 (_a = _this.startFocusTrapElement) === null || _a === void 0 ? void 0 : _a.focus({ preventScroll: true });
134 }
135 }
136 else {
137 var lastFocusableElement = _this.getKeyboardFocusableElements().pop();
138 if (lastFocusableElement != null) {
139 lastFocusableElement.focus();
140 }
141 else {
142 // Keeps focus within Overlay even if there are no keyboard-focusable children
143 (_b = _this.startFocusTrapElement) === null || _b === void 0 ? void 0 : _b.focus({ preventScroll: true });
144 }
145 }
146 };
147 _this.handleTransitionExited = function (node) {
148 var _a, _b;
149 if (_this.props.shouldReturnFocusOnClose && _this.lastActiveElementBeforeOpened instanceof HTMLElement) {
150 _this.lastActiveElementBeforeOpened.focus();
151 }
152 (_b = (_a = _this.props).onClosed) === null || _b === void 0 ? void 0 : _b.call(_a, node);
153 };
154 _this.handleBackdropMouseDown = function (e) {
155 var _a;
156 var _b = _this.props, backdropProps = _b.backdropProps, canOutsideClickClose = _b.canOutsideClickClose, enforceFocus = _b.enforceFocus, onClose = _b.onClose;
157 if (canOutsideClickClose) {
158 onClose === null || onClose === void 0 ? void 0 : onClose(e);
159 }
160 if (enforceFocus) {
161 _this.bringFocusInsideOverlay();
162 }
163 (_a = backdropProps === null || backdropProps === void 0 ? void 0 : backdropProps.onMouseDown) === null || _a === void 0 ? void 0 : _a.call(backdropProps, e);
164 };
165 _this.handleDocumentClick = function (e) {
166 var _a = _this.props, canOutsideClickClose = _a.canOutsideClickClose, isOpen = _a.isOpen, onClose = _a.onClose;
167 // get the actual target even in the Shadow DOM
168 var eventTarget = (e.composed ? e.composedPath()[0] : e.target);
169 var stackIndex = Overlay.openStack.indexOf(_this);
170 var isClickInThisOverlayOrDescendant = Overlay.openStack
171 .slice(stackIndex)
172 .some(function (_a) {
173 var elem = _a.containerElement;
174 // `elem` is the container of backdrop & content, so clicking on that container
175 // should not count as being "inside" the overlay.
176 return elem && elem.contains(eventTarget) && !elem.isSameNode(eventTarget);
177 });
178 if (isOpen && !isClickInThisOverlayOrDescendant && canOutsideClickClose) {
179 // casting to any because this is a native event
180 onClose === null || onClose === void 0 ? void 0 : onClose(e);
181 }
182 };
183 /**
184 * When multiple Overlays are open, this event handler is only active for the most recently
185 * opened one to avoid Overlays competing with each other for focus.
186 */
187 _this.handleDocumentFocus = function (e) {
188 // get the actual target even in the Shadow DOM
189 var eventTarget = e.composed ? e.composedPath()[0] : e.target;
190 if (_this.props.enforceFocus &&
191 _this.containerElement != null &&
192 eventTarget instanceof Node &&
193 !_this.containerElement.contains(eventTarget)) {
194 // prevent default focus behavior (sometimes auto-scrolls the page)
195 e.preventDefault();
196 e.stopImmediatePropagation();
197 _this.bringFocusInsideOverlay();
198 }
199 };
200 _this.handleKeyDown = function (e) {
201 var _a = _this.props, canEscapeKeyClose = _a.canEscapeKeyClose, onClose = _a.onClose;
202 if (e.key === "Escape" && canEscapeKeyClose) {
203 onClose === null || onClose === void 0 ? void 0 : onClose(e);
204 // prevent other overlays from closing
205 e.stopPropagation();
206 // prevent browser-specific escape key behavior (Safari exits fullscreen)
207 e.preventDefault();
208 }
209 };
210 _this.handleTransitionAddEnd = function () {
211 // no-op
212 };
213 return _this;
214 }
215 Overlay.getDerivedStateFromProps = function (_a) {
216 var hasEverOpened = _a.isOpen;
217 if (hasEverOpened) {
218 return { hasEverOpened: hasEverOpened };
219 }
220 return null;
221 };
222 Overlay.prototype.render = function () {
223 var _a;
224 var _b;
225 // oh snap! no reason to render anything at all if we're being truly lazy
226 if (this.props.lazy && !this.state.hasEverOpened) {
227 return null;
228 }
229 var _c = this.props, autoFocus = _c.autoFocus, children = _c.children, className = _c.className, enforceFocus = _c.enforceFocus, usePortal = _c.usePortal, isOpen = _c.isOpen;
230 // TransitionGroup types require single array of children; does not support nested arrays.
231 // So we must collapse backdrop and children into one array, and every item must be wrapped in a
232 // Transition element (no ReactText allowed).
233 var childrenWithTransitions = isOpen ? (_b = React.Children.map(children, this.maybeRenderChild)) !== null && _b !== void 0 ? _b : [] : [];
234 var maybeBackdrop = this.maybeRenderBackdrop();
235 if (maybeBackdrop !== null) {
236 childrenWithTransitions.unshift(maybeBackdrop);
237 }
238 if (isOpen && (autoFocus || enforceFocus) && childrenWithTransitions.length > 0) {
239 childrenWithTransitions.unshift(this.renderDummyElement("__start", {
240 className: common_1.Classes.OVERLAY_START_FOCUS_TRAP,
241 onFocus: this.handleStartFocusTrapElementFocus,
242 onKeyDown: this.handleStartFocusTrapElementKeyDown,
243 ref: this.refHandlers.startFocusTrap,
244 }));
245 if (enforceFocus) {
246 childrenWithTransitions.push(this.renderDummyElement("__end", {
247 className: common_1.Classes.OVERLAY_END_FOCUS_TRAP,
248 onFocus: this.handleEndFocusTrapElementFocus,
249 ref: this.refHandlers.endFocusTrap,
250 }));
251 }
252 }
253 var containerClasses = (0, classnames_1.default)(common_1.Classes.OVERLAY, (_a = {},
254 _a[common_1.Classes.OVERLAY_OPEN] = isOpen,
255 _a[common_1.Classes.OVERLAY_INLINE] = !usePortal,
256 _a), className);
257 var transitionGroup = (React.createElement(react_transition_group_1.TransitionGroup, { appear: true, "aria-live": "polite", className: containerClasses, component: "div", onKeyDown: this.handleKeyDown, ref: this.refHandlers.container }, childrenWithTransitions));
258 if (usePortal) {
259 return (React.createElement(portal_1.Portal, { className: this.props.portalClassName, container: this.props.portalContainer }, transitionGroup));
260 }
261 else {
262 return transitionGroup;
263 }
264 };
265 Overlay.prototype.componentDidMount = function () {
266 if (this.props.isOpen) {
267 this.overlayWillOpen();
268 }
269 };
270 Overlay.prototype.componentDidUpdate = function (prevProps) {
271 if (prevProps.isOpen && !this.props.isOpen) {
272 this.overlayWillClose();
273 }
274 else if (!prevProps.isOpen && this.props.isOpen) {
275 this.overlayWillOpen();
276 }
277 };
278 Overlay.prototype.componentWillUnmount = function () {
279 this.overlayWillClose();
280 };
281 /**
282 * @public for testing
283 * @internal
284 */
285 Overlay.prototype.bringFocusInsideOverlay = function () {
286 var _this = this;
287 // always delay focus manipulation to just before repaint to prevent scroll jumping
288 return this.requestAnimationFrame(function () {
289 var _a;
290 // container ref may be undefined between component mounting and Portal rendering
291 // activeElement may be undefined in some rare cases in IE
292 var activeElement = (0, utils_1.getActiveElement)(_this.containerElement);
293 if (_this.containerElement == null || activeElement == null || !_this.props.isOpen) {
294 return;
295 }
296 var isFocusOutsideModal = !_this.containerElement.contains(activeElement);
297 if (isFocusOutsideModal) {
298 (_a = _this.startFocusTrapElement) === null || _a === void 0 ? void 0 : _a.focus({ preventScroll: true });
299 _this.isAutoFocusing = false;
300 }
301 });
302 };
303 Overlay.prototype.maybeRenderBackdrop = function () {
304 var _a = this.props, backdropClassName = _a.backdropClassName, backdropProps = _a.backdropProps, hasBackdrop = _a.hasBackdrop, isOpen = _a.isOpen, transitionDuration = _a.transitionDuration, transitionName = _a.transitionName;
305 if (hasBackdrop && isOpen) {
306 return (React.createElement(react_transition_group_1.CSSTransition, { classNames: transitionName, key: "__backdrop", timeout: transitionDuration, addEndListener: this.handleTransitionAddEnd },
307 React.createElement("div", tslib_1.__assign({}, backdropProps, { className: (0, classnames_1.default)(common_1.Classes.OVERLAY_BACKDROP, backdropClassName, backdropProps === null || backdropProps === void 0 ? void 0 : backdropProps.className), onMouseDown: this.handleBackdropMouseDown }))));
308 }
309 else {
310 return null;
311 }
312 };
313 Overlay.prototype.renderDummyElement = function (key, props) {
314 var _a = this.props, transitionDuration = _a.transitionDuration, transitionName = _a.transitionName;
315 return (React.createElement(react_transition_group_1.CSSTransition, { classNames: transitionName, key: key, addEndListener: this.handleTransitionAddEnd, timeout: transitionDuration, unmountOnExit: true },
316 React.createElement("div", tslib_1.__assign({ tabIndex: 0 }, props))));
317 };
318 Overlay.prototype.getKeyboardFocusableElements = function () {
319 var focusableElements = this.containerElement !== null
320 ? Array.from(
321 // Order may not be correct if children elements use tabindex values > 0.
322 // Selectors derived from this SO question:
323 // https://stackoverflow.com/questions/1599660/which-html-elements-can-receive-focus
324 this.containerElement.querySelectorAll([
325 'a[href]:not([tabindex="-1"])',
326 'button:not([disabled]):not([tabindex="-1"])',
327 'details:not([tabindex="-1"])',
328 'input:not([disabled]):not([tabindex="-1"])',
329 'select:not([disabled]):not([tabindex="-1"])',
330 'textarea:not([disabled]):not([tabindex="-1"])',
331 '[tabindex]:not([tabindex="-1"])',
332 ].join(",")))
333 : [];
334 return focusableElements.filter(function (el) {
335 return !el.classList.contains(common_1.Classes.OVERLAY_START_FOCUS_TRAP) &&
336 !el.classList.contains(common_1.Classes.OVERLAY_END_FOCUS_TRAP);
337 });
338 };
339 Overlay.prototype.overlayWillClose = function () {
340 document.removeEventListener("focus", this.handleDocumentFocus, /* useCapture */ true);
341 document.removeEventListener("mousedown", this.handleDocumentClick);
342 var openStack = Overlay.openStack;
343 var stackIndex = openStack.indexOf(this);
344 if (stackIndex !== -1) {
345 openStack.splice(stackIndex, 1);
346 if (openStack.length > 0) {
347 var lastOpenedOverlay = Overlay.getLastOpened();
348 // Only bring focus back to last overlay if it had autoFocus _and_ enforceFocus enabled.
349 // If `autoFocus={false}`, it's likely that the overlay never received focus in the first place,
350 // so it would be surprising for us to send it there. See https://github.com/palantir/blueprint/issues/4921
351 if (lastOpenedOverlay.props.autoFocus && lastOpenedOverlay.props.enforceFocus) {
352 lastOpenedOverlay.bringFocusInsideOverlay();
353 document.addEventListener("focus", lastOpenedOverlay.handleDocumentFocus, /* useCapture */ true);
354 }
355 }
356 if (openStack.filter(function (o) { return o.props.usePortal && o.props.hasBackdrop; }).length === 0) {
357 document.body.classList.remove(common_1.Classes.OVERLAY_OPEN);
358 }
359 }
360 };
361 Overlay.prototype.overlayWillOpen = function () {
362 var getLastOpened = Overlay.getLastOpened, openStack = Overlay.openStack;
363 if (openStack.length > 0) {
364 document.removeEventListener("focus", getLastOpened().handleDocumentFocus, /* useCapture */ true);
365 }
366 openStack.push(this);
367 if (this.props.autoFocus) {
368 this.isAutoFocusing = true;
369 this.bringFocusInsideOverlay();
370 }
371 if (this.props.enforceFocus) {
372 // Focus events do not bubble, but setting useCapture allows us to listen in and execute
373 // our handler before all others
374 document.addEventListener("focus", this.handleDocumentFocus, /* useCapture */ true);
375 }
376 if (this.props.canOutsideClickClose && !this.props.hasBackdrop) {
377 document.addEventListener("mousedown", this.handleDocumentClick);
378 }
379 if (this.props.hasBackdrop && this.props.usePortal) {
380 // add a class to the body to prevent scrolling of content below the overlay
381 document.body.classList.add(common_1.Classes.OVERLAY_OPEN);
382 }
383 this.lastActiveElementBeforeOpened = (0, utils_1.getActiveElement)(this.containerElement);
384 };
385 Overlay.displayName = "".concat(props_1.DISPLAYNAME_PREFIX, ".Overlay");
386 Overlay.defaultProps = {
387 autoFocus: true,
388 backdropProps: {},
389 canEscapeKeyClose: true,
390 canOutsideClickClose: true,
391 enforceFocus: true,
392 hasBackdrop: true,
393 isOpen: false,
394 lazy: true,
395 shouldReturnFocusOnClose: true,
396 transitionDuration: 300,
397 transitionName: common_1.Classes.OVERLAY,
398 usePortal: true,
399 };
400 Overlay.openStack = [];
401 Overlay.getLastOpened = function () { return Overlay.openStack[Overlay.openStack.length - 1]; };
402 return Overlay;
403}(common_1.AbstractPureComponent2));
404exports.Overlay = Overlay;
405//# sourceMappingURL=overlay.js.map
\No newline at end of file