focus-trap 7.4.3 → 7.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +13 -1
- package/dist/focus-trap.esm.js +244 -96
- package/dist/focus-trap.esm.js.map +1 -1
- package/dist/focus-trap.esm.min.js +2 -2
- package/dist/focus-trap.esm.min.js.map +1 -1
- package/dist/focus-trap.js +243 -95
- package/dist/focus-trap.js.map +1 -1
- package/dist/focus-trap.min.js +2 -2
- package/dist/focus-trap.min.js.map +1 -1
- package/dist/focus-trap.umd.js +243 -95
- package/dist/focus-trap.umd.js.map +1 -1
- package/dist/focus-trap.umd.min.js +2 -2
- package/dist/focus-trap.umd.min.js.map +1 -1
- package/index.js +266 -93
- package/package.json +15 -15
package/dist/focus-trap.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* focus-trap 7.
|
|
2
|
+
* focus-trap 7.5.1
|
|
3
3
|
* @license MIT, https://github.com/focus-trap/focus-trap/blob/master/LICENSE
|
|
4
4
|
*/
|
|
5
5
|
'use strict';
|
|
@@ -89,10 +89,10 @@ var isSelectableInput = function isSelectableInput(node) {
|
|
|
89
89
|
return node.tagName && node.tagName.toLowerCase() === 'input' && typeof node.select === 'function';
|
|
90
90
|
};
|
|
91
91
|
var isEscapeEvent = function isEscapeEvent(e) {
|
|
92
|
-
return e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27;
|
|
92
|
+
return (e === null || e === void 0 ? void 0 : e.key) === 'Escape' || (e === null || e === void 0 ? void 0 : e.key) === 'Esc' || (e === null || e === void 0 ? void 0 : e.keyCode) === 27;
|
|
93
93
|
};
|
|
94
94
|
var isTabEvent = function isTabEvent(e) {
|
|
95
|
-
return e.key === 'Tab' || e.keyCode === 9;
|
|
95
|
+
return (e === null || e === void 0 ? void 0 : e.key) === 'Tab' || (e === null || e === void 0 ? void 0 : e.keyCode) === 9;
|
|
96
96
|
};
|
|
97
97
|
|
|
98
98
|
// checks for TAB by default
|
|
@@ -176,8 +176,11 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
176
176
|
// container: HTMLElement,
|
|
177
177
|
// tabbableNodes: Array<HTMLElement>, // empty if none
|
|
178
178
|
// focusableNodes: Array<HTMLElement>, // empty if none
|
|
179
|
-
//
|
|
180
|
-
//
|
|
179
|
+
// posTabIndexesFound: boolean,
|
|
180
|
+
// firstTabbableNode: HTMLElement|undefined,
|
|
181
|
+
// lastTabbableNode: HTMLElement|undefined,
|
|
182
|
+
// firstDomTabbableNode: HTMLElement|undefined,
|
|
183
|
+
// lastDomTabbableNode: HTMLElement|undefined,
|
|
181
184
|
// nextTabbableNode: (node: HTMLElement, forward: boolean) => HTMLElement|undefined
|
|
182
185
|
// }>}
|
|
183
186
|
containerGroups: [],
|
|
@@ -194,7 +197,9 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
194
197
|
paused: false,
|
|
195
198
|
// timer ID for when delayInitialFocus is true and initial focus in this trap
|
|
196
199
|
// has been delayed during activation
|
|
197
|
-
delayInitialFocusTimer: undefined
|
|
200
|
+
delayInitialFocusTimer: undefined,
|
|
201
|
+
// the most recent KeyboardEvent for the configured nav key (typically [SHIFT+]TAB), if any
|
|
202
|
+
recentNavEvent: undefined
|
|
198
203
|
};
|
|
199
204
|
var trap; // eslint-disable-line prefer-const -- some private functions reference it, and its methods reference private functions, so we must declare here and define later
|
|
200
205
|
|
|
@@ -213,7 +218,9 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
213
218
|
/**
|
|
214
219
|
* Finds the index of the container that contains the element.
|
|
215
220
|
* @param {HTMLElement} element
|
|
216
|
-
* @param {Event} [event]
|
|
221
|
+
* @param {Event} [event] If available, and `element` isn't directly found in any container,
|
|
222
|
+
* the event's composed path is used to see if includes any known trap containers in the
|
|
223
|
+
* case where the element is inside a Shadow DOM.
|
|
217
224
|
* @returns {number} Index of the container in either `state.containers` or
|
|
218
225
|
* `state.containerGroups` (the order/length of these lists are the same); -1
|
|
219
226
|
* if the element isn't found.
|
|
@@ -308,14 +315,41 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
308
315
|
var tabbableNodes = tabbable.tabbable(container, config.tabbableOptions);
|
|
309
316
|
|
|
310
317
|
// NOTE: if we have tabbable nodes, we must have focusable nodes; focusable nodes
|
|
311
|
-
// are a superset of tabbable nodes
|
|
318
|
+
// are a superset of tabbable nodes since nodes with negative `tabindex` attributes
|
|
319
|
+
// are focusable but not tabbable
|
|
312
320
|
var focusableNodes = tabbable.focusable(container, config.tabbableOptions);
|
|
321
|
+
var firstTabbableNode = tabbableNodes.length > 0 ? tabbableNodes[0] : undefined;
|
|
322
|
+
var lastTabbableNode = tabbableNodes.length > 0 ? tabbableNodes[tabbableNodes.length - 1] : undefined;
|
|
323
|
+
var firstDomTabbableNode = focusableNodes.find(function (node) {
|
|
324
|
+
return tabbable.isTabbable(node);
|
|
325
|
+
});
|
|
326
|
+
var lastDomTabbableNode = focusableNodes.findLast(function (node) {
|
|
327
|
+
return tabbable.isTabbable(node);
|
|
328
|
+
});
|
|
329
|
+
var posTabIndexesFound = !!tabbableNodes.find(function (node) {
|
|
330
|
+
return tabbable.getTabIndex(node) > 0;
|
|
331
|
+
});
|
|
313
332
|
return {
|
|
314
333
|
container: container,
|
|
315
334
|
tabbableNodes: tabbableNodes,
|
|
316
335
|
focusableNodes: focusableNodes,
|
|
317
|
-
|
|
318
|
-
|
|
336
|
+
/** True if at least one node with positive `tabindex` was found in this container. */
|
|
337
|
+
posTabIndexesFound: posTabIndexesFound,
|
|
338
|
+
/** First tabbable node in container, __tabindex__ order; `undefined` if none. */
|
|
339
|
+
firstTabbableNode: firstTabbableNode,
|
|
340
|
+
/** Last tabbable node in container, __tabindex__ order; `undefined` if none. */
|
|
341
|
+
lastTabbableNode: lastTabbableNode,
|
|
342
|
+
// NOTE: DOM order is NOT NECESSARILY "document position" order, but figuring that out
|
|
343
|
+
// would require more than just https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition
|
|
344
|
+
// because that API doesn't work with Shadow DOM as well as it should (@see
|
|
345
|
+
// https://github.com/whatwg/dom/issues/320) and since this first/last is only needed, so far,
|
|
346
|
+
// to address an edge case related to positive tabindex support, this seems like a much easier,
|
|
347
|
+
// "close enough most of the time" alternative for positive tabindexes which should generally
|
|
348
|
+
// be avoided anyway...
|
|
349
|
+
/** First tabbable node in container, __DOM__ order; `undefined` if none. */
|
|
350
|
+
firstDomTabbableNode: firstDomTabbableNode,
|
|
351
|
+
/** Last tabbable node in container, __DOM__ order; `undefined` if none. */
|
|
352
|
+
lastDomTabbableNode: lastDomTabbableNode,
|
|
319
353
|
/**
|
|
320
354
|
* Finds the __tabbable__ node that follows the given node in the specified direction,
|
|
321
355
|
* in this container, if any.
|
|
@@ -326,30 +360,24 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
326
360
|
*/
|
|
327
361
|
nextTabbableNode: function nextTabbableNode(node) {
|
|
328
362
|
var forward = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
|
|
329
|
-
|
|
330
|
-
// from the DOM order), this __will not work__ because the list of focusableNodes,
|
|
331
|
-
// while it contains tabbable nodes, does not sort its nodes in any order other
|
|
332
|
-
// than DOM order, because it can't: Where would you place focusable (but not
|
|
333
|
-
// tabbable) nodes in that order? They have no order, because they aren't tabbale...
|
|
334
|
-
// Support for positive tabindex is already broken and hard to manage (possibly
|
|
335
|
-
// not supportable, TBD), so this isn't going to make things worse than they
|
|
336
|
-
// already are, and at least makes things better for the majority of cases where
|
|
337
|
-
// tabindex is either 0/unset or negative.
|
|
338
|
-
// FYI, positive tabindex issue: https://github.com/focus-trap/focus-trap/issues/375
|
|
339
|
-
var nodeIdx = focusableNodes.findIndex(function (n) {
|
|
340
|
-
return n === node;
|
|
341
|
-
});
|
|
363
|
+
var nodeIdx = tabbableNodes.indexOf(node);
|
|
342
364
|
if (nodeIdx < 0) {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
365
|
+
// either not tabbable nor focusable, or was focused but not tabbable (negative tabindex):
|
|
366
|
+
// since `node` should at least have been focusable, we assume that's the case and mimic
|
|
367
|
+
// what browsers do, which is set focus to the next node in __document position order__,
|
|
368
|
+
// regardless of positive tabindexes, if any -- and for reasons explained in the NOTE
|
|
369
|
+
// above related to `firstDomTabbable` and `lastDomTabbable` properties, we fall back to
|
|
370
|
+
// basic DOM order
|
|
371
|
+
if (forward) {
|
|
372
|
+
return focusableNodes.slice(focusableNodes.indexOf(node) + 1).find(function (el) {
|
|
373
|
+
return tabbable.isTabbable(el);
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
return focusableNodes.slice(0, focusableNodes.indexOf(node)).findLast(function (el) {
|
|
377
|
+
return tabbable.isTabbable(el);
|
|
348
378
|
});
|
|
349
379
|
}
|
|
350
|
-
return
|
|
351
|
-
return tabbable.isTabbable(n, config.tabbableOptions);
|
|
352
|
-
});
|
|
380
|
+
return tabbableNodes[nodeIdx + (forward ? 1 : -1)];
|
|
353
381
|
}
|
|
354
382
|
};
|
|
355
383
|
});
|
|
@@ -362,6 +390,19 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
362
390
|
) {
|
|
363
391
|
throw new Error('Your focus-trap must have at least one container with at least one tabbable node in it at all times');
|
|
364
392
|
}
|
|
393
|
+
|
|
394
|
+
// NOTE: Positive tabindexes are only properly supported in single-container traps because
|
|
395
|
+
// doing it across multiple containers where tabindexes could be all over the place
|
|
396
|
+
// would require Tabbable to support multiple containers, would require additional
|
|
397
|
+
// specialized Shadow DOM support, and would require Tabbable's multi-container support
|
|
398
|
+
// to look at those containers in document position order rather than user-provided
|
|
399
|
+
// order (as they are treated in Focus-trap, for legacy reasons). See discussion on
|
|
400
|
+
// https://github.com/focus-trap/focus-trap/issues/375 for more details.
|
|
401
|
+
if (state.containerGroups.find(function (g) {
|
|
402
|
+
return g.posTabIndexesFound;
|
|
403
|
+
}) && state.containerGroups.length > 1) {
|
|
404
|
+
throw new Error("At least one node with a positive tabindex was found in one of your focus-trap's multiple containers. Positive tabindexes are only supported in single-container focus-traps.");
|
|
405
|
+
}
|
|
365
406
|
};
|
|
366
407
|
var tryFocus = function tryFocus(node) {
|
|
367
408
|
if (node === false) {
|
|
@@ -377,6 +418,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
377
418
|
node.focus({
|
|
378
419
|
preventScroll: !!config.preventScroll
|
|
379
420
|
});
|
|
421
|
+
// NOTE: focus() API does not trigger focusIn event so set MRU node manually
|
|
380
422
|
state.mostRecentlyFocusedNode = node;
|
|
381
423
|
if (isSelectableInput(node)) {
|
|
382
424
|
node.select();
|
|
@@ -387,64 +429,23 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
387
429
|
return node ? node : node === false ? false : previousActiveElement;
|
|
388
430
|
};
|
|
389
431
|
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
returnFocus: config.returnFocusOnDeactivate
|
|
408
|
-
});
|
|
409
|
-
return;
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
// This is needed for mobile devices.
|
|
413
|
-
// (If we'll only let `click` events through,
|
|
414
|
-
// then on mobile they will be blocked anyways if `touchstart` is blocked.)
|
|
415
|
-
if (valueOrHandler(config.allowOutsideClick, e)) {
|
|
416
|
-
// allow the click outside the trap to take place
|
|
417
|
-
return;
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
// otherwise, prevent the click
|
|
421
|
-
e.preventDefault();
|
|
422
|
-
};
|
|
423
|
-
|
|
424
|
-
// In case focus escapes the trap for some strange reason, pull it back in.
|
|
425
|
-
var checkFocusIn = function checkFocusIn(e) {
|
|
426
|
-
var target = getActualTarget(e);
|
|
427
|
-
var targetContained = findContainerIndex(target, e) >= 0;
|
|
428
|
-
|
|
429
|
-
// In Firefox when you Tab out of an iframe the Document is briefly focused.
|
|
430
|
-
if (targetContained || target instanceof Document) {
|
|
431
|
-
if (targetContained) {
|
|
432
|
-
state.mostRecentlyFocusedNode = target;
|
|
433
|
-
}
|
|
434
|
-
} else {
|
|
435
|
-
// escaped! pull it back in to where it just left
|
|
436
|
-
e.stopImmediatePropagation();
|
|
437
|
-
tryFocus(state.mostRecentlyFocusedNode || getInitialFocusNode());
|
|
438
|
-
}
|
|
439
|
-
};
|
|
440
|
-
|
|
441
|
-
// Hijack key nav events on the first and last focusable nodes of the trap,
|
|
442
|
-
// in order to prevent focus from escaping. If it escapes for even a
|
|
443
|
-
// moment it can end up scrolling the page and causing confusion so we
|
|
444
|
-
// kind of need to capture the action at the keydown phase.
|
|
445
|
-
var checkKeyNav = function checkKeyNav(event) {
|
|
446
|
-
var isBackward = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
447
|
-
var target = getActualTarget(event);
|
|
432
|
+
/**
|
|
433
|
+
* Finds the next node (in either direction) where focus should move according to a
|
|
434
|
+
* keyboard focus-in event.
|
|
435
|
+
* @param {Object} params
|
|
436
|
+
* @param {Node} [params.target] Known target __from which__ to navigate, if any.
|
|
437
|
+
* @param {KeyboardEvent|FocusEvent} [params.event] Event to use if `target` isn't known (event
|
|
438
|
+
* will be used to determine the `target`). Ignored if `target` is specified.
|
|
439
|
+
* @param {boolean} [params.isBackward] True if focus should move backward.
|
|
440
|
+
* @returns {Node|undefined} The next node, or `undefined` if a next node couldn't be
|
|
441
|
+
* determined given the current state of the trap.
|
|
442
|
+
*/
|
|
443
|
+
var findNextNavNode = function findNextNavNode(_ref2) {
|
|
444
|
+
var target = _ref2.target,
|
|
445
|
+
event = _ref2.event,
|
|
446
|
+
_ref2$isBackward = _ref2.isBackward,
|
|
447
|
+
isBackward = _ref2$isBackward === void 0 ? false : _ref2$isBackward;
|
|
448
|
+
target = target || getActualTarget(event);
|
|
448
449
|
updateTabbableNodes();
|
|
449
450
|
var destinationNode = null;
|
|
450
451
|
if (state.tabbableGroups.length > 0) {
|
|
@@ -467,8 +468,8 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
467
468
|
// REVERSE
|
|
468
469
|
|
|
469
470
|
// is the target the first tabbable node in a group?
|
|
470
|
-
var startOfGroupIndex = findIndex(state.tabbableGroups, function (
|
|
471
|
-
var firstTabbableNode =
|
|
471
|
+
var startOfGroupIndex = findIndex(state.tabbableGroups, function (_ref3) {
|
|
472
|
+
var firstTabbableNode = _ref3.firstTabbableNode;
|
|
472
473
|
return target === firstTabbableNode;
|
|
473
474
|
});
|
|
474
475
|
if (startOfGroupIndex < 0 && (containerGroup.container === target || tabbable.isFocusable(target, config.tabbableOptions) && !tabbable.isTabbable(target, config.tabbableOptions) && !containerGroup.nextTabbableNode(target, false))) {
|
|
@@ -486,7 +487,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
486
487
|
// the LAST group if it's the first tabbable node of the FIRST group)
|
|
487
488
|
var destinationGroupIndex = startOfGroupIndex === 0 ? state.tabbableGroups.length - 1 : startOfGroupIndex - 1;
|
|
488
489
|
var destinationGroup = state.tabbableGroups[destinationGroupIndex];
|
|
489
|
-
destinationNode = destinationGroup.lastTabbableNode;
|
|
490
|
+
destinationNode = tabbable.getTabIndex(target) >= 0 ? destinationGroup.lastTabbableNode : destinationGroup.lastDomTabbableNode;
|
|
490
491
|
} else if (!isTabEvent(event)) {
|
|
491
492
|
// user must have customized the nav keys so we have to move focus manually _within_
|
|
492
493
|
// the active group: do this based on the order determined by tabbable()
|
|
@@ -496,8 +497,8 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
496
497
|
// FORWARD
|
|
497
498
|
|
|
498
499
|
// is the target the last tabbable node in a group?
|
|
499
|
-
var lastOfGroupIndex = findIndex(state.tabbableGroups, function (
|
|
500
|
-
var lastTabbableNode =
|
|
500
|
+
var lastOfGroupIndex = findIndex(state.tabbableGroups, function (_ref4) {
|
|
501
|
+
var lastTabbableNode = _ref4.lastTabbableNode;
|
|
501
502
|
return target === lastTabbableNode;
|
|
502
503
|
});
|
|
503
504
|
if (lastOfGroupIndex < 0 && (containerGroup.container === target || tabbable.isFocusable(target, config.tabbableOptions) && !tabbable.isTabbable(target, config.tabbableOptions) && !containerGroup.nextTabbableNode(target))) {
|
|
@@ -515,7 +516,7 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
515
516
|
// group if it's the last tabbable node of the LAST group)
|
|
516
517
|
var _destinationGroupIndex = lastOfGroupIndex === state.tabbableGroups.length - 1 ? 0 : lastOfGroupIndex + 1;
|
|
517
518
|
var _destinationGroup = state.tabbableGroups[_destinationGroupIndex];
|
|
518
|
-
destinationNode = _destinationGroup.firstTabbableNode;
|
|
519
|
+
destinationNode = tabbable.getTabIndex(target) >= 0 ? _destinationGroup.firstTabbableNode : _destinationGroup.firstDomTabbableNode;
|
|
519
520
|
} else if (!isTabEvent(event)) {
|
|
520
521
|
// user must have customized the nav keys so we have to move focus manually _within_
|
|
521
522
|
// the active group: do this based on the order determined by tabbable()
|
|
@@ -527,6 +528,153 @@ var createFocusTrap = function createFocusTrap(elements, userOptions) {
|
|
|
527
528
|
// NOTE: the fallbackFocus option does not support returning false to opt-out
|
|
528
529
|
destinationNode = getNodeForOption('fallbackFocus');
|
|
529
530
|
}
|
|
531
|
+
return destinationNode;
|
|
532
|
+
};
|
|
533
|
+
|
|
534
|
+
// This needs to be done on mousedown and touchstart instead of click
|
|
535
|
+
// so that it precedes the focus event.
|
|
536
|
+
var checkPointerDown = function checkPointerDown(e) {
|
|
537
|
+
var target = getActualTarget(e);
|
|
538
|
+
if (findContainerIndex(target, e) >= 0) {
|
|
539
|
+
// allow the click since it ocurred inside the trap
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
if (valueOrHandler(config.clickOutsideDeactivates, e)) {
|
|
543
|
+
// immediately deactivate the trap
|
|
544
|
+
trap.deactivate({
|
|
545
|
+
// NOTE: by setting `returnFocus: false`, deactivate() will do nothing,
|
|
546
|
+
// which will result in the outside click setting focus to the node
|
|
547
|
+
// that was clicked (and if not focusable, to "nothing"); by setting
|
|
548
|
+
// `returnFocus: true`, we'll attempt to re-focus the node originally-focused
|
|
549
|
+
// on activation (or the configured `setReturnFocus` node), whether the
|
|
550
|
+
// outside click was on a focusable node or not
|
|
551
|
+
returnFocus: config.returnFocusOnDeactivate
|
|
552
|
+
});
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// This is needed for mobile devices.
|
|
557
|
+
// (If we'll only let `click` events through,
|
|
558
|
+
// then on mobile they will be blocked anyways if `touchstart` is blocked.)
|
|
559
|
+
if (valueOrHandler(config.allowOutsideClick, e)) {
|
|
560
|
+
// allow the click outside the trap to take place
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
// otherwise, prevent the click
|
|
565
|
+
e.preventDefault();
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
// In case focus escapes the trap for some strange reason, pull it back in.
|
|
569
|
+
// NOTE: the focusIn event is NOT cancelable, so if focus escapes, it may cause unexpected
|
|
570
|
+
// scrolling if the node that got focused was out of view; there's nothing we can do to
|
|
571
|
+
// prevent that from happening by the time we discover that focus escaped
|
|
572
|
+
var checkFocusIn = function checkFocusIn(event) {
|
|
573
|
+
var target = getActualTarget(event);
|
|
574
|
+
var targetContained = findContainerIndex(target, event) >= 0;
|
|
575
|
+
|
|
576
|
+
// In Firefox when you Tab out of an iframe the Document is briefly focused.
|
|
577
|
+
if (targetContained || target instanceof Document) {
|
|
578
|
+
if (targetContained) {
|
|
579
|
+
state.mostRecentlyFocusedNode = target;
|
|
580
|
+
}
|
|
581
|
+
} else {
|
|
582
|
+
// escaped! pull it back in to where it just left
|
|
583
|
+
event.stopImmediatePropagation();
|
|
584
|
+
|
|
585
|
+
// focus will escape if the MRU node had a positive tab index and user tried to nav forward;
|
|
586
|
+
// it will also escape if the MRU node had a 0 tab index and user tried to nav backward
|
|
587
|
+
// toward a node with a positive tab index
|
|
588
|
+
var nextNode; // next node to focus, if we find one
|
|
589
|
+
var navAcrossContainers = true;
|
|
590
|
+
if (state.mostRecentlyFocusedNode) {
|
|
591
|
+
if (tabbable.getTabIndex(state.mostRecentlyFocusedNode) > 0) {
|
|
592
|
+
// MRU container index must be >=0 otherwise we wouldn't have it as an MRU node...
|
|
593
|
+
var mruContainerIdx = findContainerIndex(state.mostRecentlyFocusedNode);
|
|
594
|
+
// there MAY not be any tabbable nodes in the container if there are at least 2 containers
|
|
595
|
+
// and the MRU node is focusable but not tabbable (focus-trap requires at least 1 container
|
|
596
|
+
// with at least one tabbable node in order to function, so this could be the other container
|
|
597
|
+
// with nothing tabbable in it)
|
|
598
|
+
var tabbableNodes = state.containerGroups[mruContainerIdx].tabbableNodes;
|
|
599
|
+
if (tabbableNodes.length > 0) {
|
|
600
|
+
// MRU tab index MAY not be found if the MRU node is focusable but not tabbable
|
|
601
|
+
var mruTabIdx = tabbableNodes.findIndex(function (node) {
|
|
602
|
+
return node === state.mostRecentlyFocusedNode;
|
|
603
|
+
});
|
|
604
|
+
if (mruTabIdx >= 0) {
|
|
605
|
+
if (config.isKeyForward(state.recentNavEvent)) {
|
|
606
|
+
if (mruTabIdx + 1 < tabbableNodes.length) {
|
|
607
|
+
nextNode = tabbableNodes[mruTabIdx + 1];
|
|
608
|
+
navAcrossContainers = false;
|
|
609
|
+
}
|
|
610
|
+
// else, don't wrap within the container as focus should move to next/previous
|
|
611
|
+
// container
|
|
612
|
+
} else {
|
|
613
|
+
if (mruTabIdx - 1 >= 0) {
|
|
614
|
+
nextNode = tabbableNodes[mruTabIdx - 1];
|
|
615
|
+
navAcrossContainers = false;
|
|
616
|
+
}
|
|
617
|
+
// else, don't wrap within the container as focus should move to next/previous
|
|
618
|
+
// container
|
|
619
|
+
}
|
|
620
|
+
// else, don't find in container order without considering direction too
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
// else, no tabbable nodes in that container (which means we must have at least one other
|
|
624
|
+
// container with at least one tabbable node in it, otherwise focus-trap would've thrown
|
|
625
|
+
// an error the last time updateTabbableNodes() was run): find next node among all known
|
|
626
|
+
// containers
|
|
627
|
+
} else {
|
|
628
|
+
// check to see if there's at least one tabbable node with a positive tab index inside
|
|
629
|
+
// the trap because focus seems to escape when navigating backward from a tabbable node
|
|
630
|
+
// with tabindex=0 when this is the case (instead of wrapping to the tabbable node with
|
|
631
|
+
// the greatest positive tab index like it should)
|
|
632
|
+
if (!state.containerGroups.some(function (g) {
|
|
633
|
+
return g.tabbableNodes.some(function (n) {
|
|
634
|
+
return tabbable.getTabIndex(n) > 0;
|
|
635
|
+
});
|
|
636
|
+
})) {
|
|
637
|
+
// no containers with tabbable nodes with positive tab indexes which means the focus
|
|
638
|
+
// escaped for some other reason and we should just execute the fallback to the
|
|
639
|
+
// MRU node or initial focus node, if any
|
|
640
|
+
navAcrossContainers = false;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
} else {
|
|
644
|
+
// no MRU node means we're likely in some initial condition when the trap has just
|
|
645
|
+
// been activated and initial focus hasn't been given yet, in which case we should
|
|
646
|
+
// fall through to trying to focus the initial focus node, which is what should
|
|
647
|
+
// happen below at this point in the logic
|
|
648
|
+
navAcrossContainers = false;
|
|
649
|
+
}
|
|
650
|
+
if (navAcrossContainers) {
|
|
651
|
+
nextNode = findNextNavNode({
|
|
652
|
+
// move FROM the MRU node, not event-related node (which will be the node that is
|
|
653
|
+
// outside the trap causing the focus escape we're trying to fix)
|
|
654
|
+
target: state.mostRecentlyFocusedNode,
|
|
655
|
+
isBackward: config.isKeyBackward(state.recentNavEvent)
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
if (nextNode) {
|
|
659
|
+
tryFocus(nextNode);
|
|
660
|
+
} else {
|
|
661
|
+
tryFocus(state.mostRecentlyFocusedNode || getInitialFocusNode());
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
state.recentNavEvent = undefined; // clear
|
|
665
|
+
};
|
|
666
|
+
|
|
667
|
+
// Hijack key nav events on the first and last focusable nodes of the trap,
|
|
668
|
+
// in order to prevent focus from escaping. If it escapes for even a
|
|
669
|
+
// moment it can end up scrolling the page and causing confusion so we
|
|
670
|
+
// kind of need to capture the action at the keydown phase.
|
|
671
|
+
var checkKeyNav = function checkKeyNav(event) {
|
|
672
|
+
var isBackward = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
673
|
+
state.recentNavEvent = event;
|
|
674
|
+
var destinationNode = findNextNavNode({
|
|
675
|
+
event: event,
|
|
676
|
+
isBackward: isBackward
|
|
677
|
+
});
|
|
530
678
|
if (destinationNode) {
|
|
531
679
|
if (isTabEvent(event)) {
|
|
532
680
|
// since tab natively moves focus, we wouldn't have a destination node unless we
|