focus-trap 7.4.2 → 7.5.0
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 +14 -2
- package/dist/focus-trap.esm.js +280 -98
- 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 +279 -97
- 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 +279 -97
- 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 +308 -95
- package/package.json +15 -15
package/index.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
tabbable,
|
|
3
|
+
focusable,
|
|
4
|
+
isFocusable,
|
|
5
|
+
isTabbable,
|
|
6
|
+
getTabIndex,
|
|
7
|
+
} from 'tabbable';
|
|
2
8
|
|
|
3
9
|
const activeFocusTraps = {
|
|
4
10
|
activateTrap(trapStack, trap) {
|
|
@@ -40,11 +46,11 @@ const isSelectableInput = function (node) {
|
|
|
40
46
|
};
|
|
41
47
|
|
|
42
48
|
const isEscapeEvent = function (e) {
|
|
43
|
-
return e
|
|
49
|
+
return e?.key === 'Escape' || e?.key === 'Esc' || e?.keyCode === 27;
|
|
44
50
|
};
|
|
45
51
|
|
|
46
52
|
const isTabEvent = function (e) {
|
|
47
|
-
return e
|
|
53
|
+
return e?.key === 'Tab' || e?.keyCode === 9;
|
|
48
54
|
};
|
|
49
55
|
|
|
50
56
|
// checks for TAB by default
|
|
@@ -136,8 +142,11 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
136
142
|
// container: HTMLElement,
|
|
137
143
|
// tabbableNodes: Array<HTMLElement>, // empty if none
|
|
138
144
|
// focusableNodes: Array<HTMLElement>, // empty if none
|
|
139
|
-
//
|
|
140
|
-
//
|
|
145
|
+
// posTabIndexesFound: boolean,
|
|
146
|
+
// firstTabbableNode: HTMLElement|undefined,
|
|
147
|
+
// lastTabbableNode: HTMLElement|undefined,
|
|
148
|
+
// firstDomTabbableNode: HTMLElement|undefined,
|
|
149
|
+
// lastDomTabbableNode: HTMLElement|undefined,
|
|
141
150
|
// nextTabbableNode: (node: HTMLElement, forward: boolean) => HTMLElement|undefined
|
|
142
151
|
// }>}
|
|
143
152
|
containerGroups: [], // same order/length as `containers` list
|
|
@@ -156,6 +165,9 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
156
165
|
// timer ID for when delayInitialFocus is true and initial focus in this trap
|
|
157
166
|
// has been delayed during activation
|
|
158
167
|
delayInitialFocusTimer: undefined,
|
|
168
|
+
|
|
169
|
+
// the most recent KeyboardEvent for the configured nav key (typically [SHIFT+]TAB), if any
|
|
170
|
+
recentNavEvent: undefined,
|
|
159
171
|
};
|
|
160
172
|
|
|
161
173
|
let 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
|
|
@@ -178,7 +190,9 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
178
190
|
/**
|
|
179
191
|
* Finds the index of the container that contains the element.
|
|
180
192
|
* @param {HTMLElement} element
|
|
181
|
-
* @param {Event} [event]
|
|
193
|
+
* @param {Event} [event] If available, and `element` isn't directly found in any container,
|
|
194
|
+
* the event's composed path is used to see if includes any known trap containers in the
|
|
195
|
+
* case where the element is inside a Shadow DOM.
|
|
182
196
|
* @returns {number} Index of the container in either `state.containers` or
|
|
183
197
|
* `state.containerGroups` (the order/length of these lists are the same); -1
|
|
184
198
|
* if the element isn't found.
|
|
@@ -260,8 +274,8 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
260
274
|
return false;
|
|
261
275
|
}
|
|
262
276
|
|
|
263
|
-
if (node === undefined) {
|
|
264
|
-
// option not specified: use fallback options
|
|
277
|
+
if (node === undefined || !isFocusable(node, config.tabbableOptions)) {
|
|
278
|
+
// option not specified nor focusable: use fallback options
|
|
265
279
|
if (findContainerIndex(doc.activeElement) >= 0) {
|
|
266
280
|
node = doc.activeElement;
|
|
267
281
|
} else {
|
|
@@ -288,18 +302,52 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
288
302
|
const tabbableNodes = tabbable(container, config.tabbableOptions);
|
|
289
303
|
|
|
290
304
|
// NOTE: if we have tabbable nodes, we must have focusable nodes; focusable nodes
|
|
291
|
-
// are a superset of tabbable nodes
|
|
305
|
+
// are a superset of tabbable nodes since nodes with negative `tabindex` attributes
|
|
306
|
+
// are focusable but not tabbable
|
|
292
307
|
const focusableNodes = focusable(container, config.tabbableOptions);
|
|
293
308
|
|
|
309
|
+
const firstTabbableNode =
|
|
310
|
+
tabbableNodes.length > 0 ? tabbableNodes[0] : undefined;
|
|
311
|
+
const lastTabbableNode =
|
|
312
|
+
tabbableNodes.length > 0
|
|
313
|
+
? tabbableNodes[tabbableNodes.length - 1]
|
|
314
|
+
: undefined;
|
|
315
|
+
|
|
316
|
+
const firstDomTabbableNode = focusableNodes.find((node) =>
|
|
317
|
+
isTabbable(node)
|
|
318
|
+
);
|
|
319
|
+
const lastDomTabbableNode = focusableNodes.findLast((node) =>
|
|
320
|
+
isTabbable(node)
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
const posTabIndexesFound = !!tabbableNodes.find(
|
|
324
|
+
(node) => getTabIndex(node) > 0
|
|
325
|
+
);
|
|
326
|
+
|
|
294
327
|
return {
|
|
295
328
|
container,
|
|
296
329
|
tabbableNodes,
|
|
297
330
|
focusableNodes,
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
331
|
+
|
|
332
|
+
/** True if at least one node with positive `tabindex` was found in this container. */
|
|
333
|
+
posTabIndexesFound,
|
|
334
|
+
|
|
335
|
+
/** First tabbable node in container, __tabindex__ order; `undefined` if none. */
|
|
336
|
+
firstTabbableNode,
|
|
337
|
+
/** Last tabbable node in container, __tabindex__ order; `undefined` if none. */
|
|
338
|
+
lastTabbableNode,
|
|
339
|
+
|
|
340
|
+
// NOTE: DOM order is NOT NECESSARILY "document position" order, but figuring that out
|
|
341
|
+
// would require more than just https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition
|
|
342
|
+
// because that API doesn't work with Shadow DOM as well as it should (@see
|
|
343
|
+
// https://github.com/whatwg/dom/issues/320) and since this first/last is only needed, so far,
|
|
344
|
+
// to address an edge case related to positive tabindex support, this seems like a much easier,
|
|
345
|
+
// "close enough most of the time" alternative for positive tabindexes which should generally
|
|
346
|
+
// be avoided anyway...
|
|
347
|
+
/** First tabbable node in container, __DOM__ order; `undefined` if none. */
|
|
348
|
+
firstDomTabbableNode,
|
|
349
|
+
/** Last tabbable node in container, __DOM__ order; `undefined` if none. */
|
|
350
|
+
lastDomTabbableNode,
|
|
303
351
|
|
|
304
352
|
/**
|
|
305
353
|
* Finds the __tabbable__ node that follows the given node in the specified direction,
|
|
@@ -310,31 +358,26 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
310
358
|
* @returns {HTMLElement|undefined} The next tabbable node, if any.
|
|
311
359
|
*/
|
|
312
360
|
nextTabbableNode(node, forward = true) {
|
|
313
|
-
|
|
314
|
-
// from the DOM order), this __will not work__ because the list of focusableNodes,
|
|
315
|
-
// while it contains tabbable nodes, does not sort its nodes in any order other
|
|
316
|
-
// than DOM order, because it can't: Where would you place focusable (but not
|
|
317
|
-
// tabbable) nodes in that order? They have no order, because they aren't tabbale...
|
|
318
|
-
// Support for positive tabindex is already broken and hard to manage (possibly
|
|
319
|
-
// not supportable, TBD), so this isn't going to make things worse than they
|
|
320
|
-
// already are, and at least makes things better for the majority of cases where
|
|
321
|
-
// tabindex is either 0/unset or negative.
|
|
322
|
-
// FYI, positive tabindex issue: https://github.com/focus-trap/focus-trap/issues/375
|
|
323
|
-
const nodeIdx = focusableNodes.findIndex((n) => n === node);
|
|
361
|
+
const nodeIdx = tabbableNodes.indexOf(node);
|
|
324
362
|
if (nodeIdx < 0) {
|
|
325
|
-
|
|
326
|
-
|
|
363
|
+
// either not tabbable nor focusable, or was focused but not tabbable (negative tabindex):
|
|
364
|
+
// since `node` should at least have been focusable, we assume that's the case and mimic
|
|
365
|
+
// what browsers do, which is set focus to the next node in __document position order__,
|
|
366
|
+
// regardless of positive tabindexes, if any -- and for reasons explained in the NOTE
|
|
367
|
+
// above related to `firstDomTabbable` and `lastDomTabbable` properties, we fall back to
|
|
368
|
+
// basic DOM order
|
|
369
|
+
if (forward) {
|
|
370
|
+
return focusableNodes
|
|
371
|
+
.slice(focusableNodes.indexOf(node) + 1)
|
|
372
|
+
.find((el) => isTabbable(el));
|
|
373
|
+
}
|
|
327
374
|
|
|
328
|
-
if (forward) {
|
|
329
375
|
return focusableNodes
|
|
330
|
-
.slice(
|
|
331
|
-
.
|
|
376
|
+
.slice(0, focusableNodes.indexOf(node))
|
|
377
|
+
.findLast((el) => isTabbable(el));
|
|
332
378
|
}
|
|
333
379
|
|
|
334
|
-
return
|
|
335
|
-
.slice(0, nodeIdx)
|
|
336
|
-
.reverse()
|
|
337
|
-
.find((n) => isTabbable(n, config.tabbableOptions));
|
|
380
|
+
return tabbableNodes[nodeIdx + (forward ? 1 : -1)];
|
|
338
381
|
},
|
|
339
382
|
};
|
|
340
383
|
});
|
|
@@ -352,6 +395,22 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
352
395
|
'Your focus-trap must have at least one container with at least one tabbable node in it at all times'
|
|
353
396
|
);
|
|
354
397
|
}
|
|
398
|
+
|
|
399
|
+
// NOTE: Positive tabindexes are only properly supported in single-container traps because
|
|
400
|
+
// doing it across multiple containers where tabindexes could be all over the place
|
|
401
|
+
// would require Tabbable to support multiple containers, would require additional
|
|
402
|
+
// specialized Shadow DOM support, and would require Tabbable's multi-container support
|
|
403
|
+
// to look at those containers in document position order rather than user-provided
|
|
404
|
+
// order (as they are treated in Focus-trap, for legacy reasons). See discussion on
|
|
405
|
+
// https://github.com/focus-trap/focus-trap/issues/375 for more details.
|
|
406
|
+
if (
|
|
407
|
+
state.containerGroups.find((g) => g.posTabIndexesFound) &&
|
|
408
|
+
state.containerGroups.length > 1
|
|
409
|
+
) {
|
|
410
|
+
throw new Error(
|
|
411
|
+
"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."
|
|
412
|
+
);
|
|
413
|
+
}
|
|
355
414
|
};
|
|
356
415
|
|
|
357
416
|
const tryFocus = function (node) {
|
|
@@ -369,6 +428,7 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
369
428
|
}
|
|
370
429
|
|
|
371
430
|
node.focus({ preventScroll: !!config.preventScroll });
|
|
431
|
+
// NOTE: focus() API does not trigger focusIn event so set MRU node manually
|
|
372
432
|
state.mostRecentlyFocusedNode = node;
|
|
373
433
|
|
|
374
434
|
if (isSelectableInput(node)) {
|
|
@@ -381,65 +441,19 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
381
441
|
return node ? node : node === false ? false : previousActiveElement;
|
|
382
442
|
};
|
|
383
443
|
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
// NOTE: by setting `returnFocus: false`, deactivate() will do nothing,
|
|
398
|
-
// which will result in the outside click setting focus to the node
|
|
399
|
-
// that was clicked (and if not focusable, to "nothing"); by setting
|
|
400
|
-
// `returnFocus: true`, we'll attempt to re-focus the node originally-focused
|
|
401
|
-
// on activation (or the configured `setReturnFocus` node), whether the
|
|
402
|
-
// outside click was on a focusable node or not
|
|
403
|
-
returnFocus: config.returnFocusOnDeactivate,
|
|
404
|
-
});
|
|
405
|
-
return;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
// This is needed for mobile devices.
|
|
409
|
-
// (If we'll only let `click` events through,
|
|
410
|
-
// then on mobile they will be blocked anyways if `touchstart` is blocked.)
|
|
411
|
-
if (valueOrHandler(config.allowOutsideClick, e)) {
|
|
412
|
-
// allow the click outside the trap to take place
|
|
413
|
-
return;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
// otherwise, prevent the click
|
|
417
|
-
e.preventDefault();
|
|
418
|
-
};
|
|
419
|
-
|
|
420
|
-
// In case focus escapes the trap for some strange reason, pull it back in.
|
|
421
|
-
const checkFocusIn = function (e) {
|
|
422
|
-
const target = getActualTarget(e);
|
|
423
|
-
const targetContained = findContainerIndex(target, e) >= 0;
|
|
424
|
-
|
|
425
|
-
// In Firefox when you Tab out of an iframe the Document is briefly focused.
|
|
426
|
-
if (targetContained || target instanceof Document) {
|
|
427
|
-
if (targetContained) {
|
|
428
|
-
state.mostRecentlyFocusedNode = target;
|
|
429
|
-
}
|
|
430
|
-
} else {
|
|
431
|
-
// escaped! pull it back in to where it just left
|
|
432
|
-
e.stopImmediatePropagation();
|
|
433
|
-
tryFocus(state.mostRecentlyFocusedNode || getInitialFocusNode());
|
|
434
|
-
}
|
|
435
|
-
};
|
|
436
|
-
|
|
437
|
-
// Hijack key nav events on the first and last focusable nodes of the trap,
|
|
438
|
-
// in order to prevent focus from escaping. If it escapes for even a
|
|
439
|
-
// moment it can end up scrolling the page and causing confusion so we
|
|
440
|
-
// kind of need to capture the action at the keydown phase.
|
|
441
|
-
const checkKeyNav = function (event, isBackward = false) {
|
|
442
|
-
const target = getActualTarget(event);
|
|
444
|
+
/**
|
|
445
|
+
* Finds the next node (in either direction) where focus should move according to a
|
|
446
|
+
* keyboard focus-in event.
|
|
447
|
+
* @param {Object} params
|
|
448
|
+
* @param {Node} [params.target] Known target __from which__ to navigate, if any.
|
|
449
|
+
* @param {KeyboardEvent|FocusEvent} [params.event] Event to use if `target` isn't known (event
|
|
450
|
+
* will be used to determine the `target`). Ignored if `target` is specified.
|
|
451
|
+
* @param {boolean} [params.isBackward] True if focus should move backward.
|
|
452
|
+
* @returns {Node|undefined} The next node, or `undefined` if a next node couldn't be
|
|
453
|
+
* determined given the current state of the trap.
|
|
454
|
+
*/
|
|
455
|
+
const findNextNavNode = function ({ target, event, isBackward = false }) {
|
|
456
|
+
target = target || getActualTarget(event);
|
|
443
457
|
updateTabbableNodes();
|
|
444
458
|
|
|
445
459
|
let destinationNode = null;
|
|
@@ -499,7 +513,11 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
499
513
|
: startOfGroupIndex - 1;
|
|
500
514
|
|
|
501
515
|
const destinationGroup = state.tabbableGroups[destinationGroupIndex];
|
|
502
|
-
|
|
516
|
+
|
|
517
|
+
destinationNode =
|
|
518
|
+
getTabIndex(target) >= 0
|
|
519
|
+
? destinationGroup.lastTabbableNode
|
|
520
|
+
: destinationGroup.lastDomTabbableNode;
|
|
503
521
|
} else if (!isTabEvent(event)) {
|
|
504
522
|
// user must have customized the nav keys so we have to move focus manually _within_
|
|
505
523
|
// the active group: do this based on the order determined by tabbable()
|
|
@@ -540,7 +558,11 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
540
558
|
: lastOfGroupIndex + 1;
|
|
541
559
|
|
|
542
560
|
const destinationGroup = state.tabbableGroups[destinationGroupIndex];
|
|
543
|
-
|
|
561
|
+
|
|
562
|
+
destinationNode =
|
|
563
|
+
getTabIndex(target) >= 0
|
|
564
|
+
? destinationGroup.firstTabbableNode
|
|
565
|
+
: destinationGroup.firstDomTabbableNode;
|
|
544
566
|
} else if (!isTabEvent(event)) {
|
|
545
567
|
// user must have customized the nav keys so we have to move focus manually _within_
|
|
546
568
|
// the active group: do this based on the order determined by tabbable()
|
|
@@ -553,6 +575,149 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
553
575
|
destinationNode = getNodeForOption('fallbackFocus');
|
|
554
576
|
}
|
|
555
577
|
|
|
578
|
+
return destinationNode;
|
|
579
|
+
};
|
|
580
|
+
|
|
581
|
+
// This needs to be done on mousedown and touchstart instead of click
|
|
582
|
+
// so that it precedes the focus event.
|
|
583
|
+
const checkPointerDown = function (e) {
|
|
584
|
+
const target = getActualTarget(e);
|
|
585
|
+
|
|
586
|
+
if (findContainerIndex(target, e) >= 0) {
|
|
587
|
+
// allow the click since it ocurred inside the trap
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
if (valueOrHandler(config.clickOutsideDeactivates, e)) {
|
|
592
|
+
// immediately deactivate the trap
|
|
593
|
+
trap.deactivate({
|
|
594
|
+
// NOTE: by setting `returnFocus: false`, deactivate() will do nothing,
|
|
595
|
+
// which will result in the outside click setting focus to the node
|
|
596
|
+
// that was clicked (and if not focusable, to "nothing"); by setting
|
|
597
|
+
// `returnFocus: true`, we'll attempt to re-focus the node originally-focused
|
|
598
|
+
// on activation (or the configured `setReturnFocus` node), whether the
|
|
599
|
+
// outside click was on a focusable node or not
|
|
600
|
+
returnFocus: config.returnFocusOnDeactivate,
|
|
601
|
+
});
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
// This is needed for mobile devices.
|
|
606
|
+
// (If we'll only let `click` events through,
|
|
607
|
+
// then on mobile they will be blocked anyways if `touchstart` is blocked.)
|
|
608
|
+
if (valueOrHandler(config.allowOutsideClick, e)) {
|
|
609
|
+
// allow the click outside the trap to take place
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
// otherwise, prevent the click
|
|
614
|
+
e.preventDefault();
|
|
615
|
+
};
|
|
616
|
+
|
|
617
|
+
// In case focus escapes the trap for some strange reason, pull it back in.
|
|
618
|
+
// NOTE: the focusIn event is NOT cancelable, so if focus escapes, it may cause unexpected
|
|
619
|
+
// scrolling if the node that got focused was out of view; there's nothing we can do to
|
|
620
|
+
// prevent that from happening by the time we discover that focus escaped
|
|
621
|
+
const checkFocusIn = function (event) {
|
|
622
|
+
const target = getActualTarget(event);
|
|
623
|
+
const targetContained = findContainerIndex(target, event) >= 0;
|
|
624
|
+
|
|
625
|
+
// In Firefox when you Tab out of an iframe the Document is briefly focused.
|
|
626
|
+
if (targetContained || target instanceof Document) {
|
|
627
|
+
if (targetContained) {
|
|
628
|
+
state.mostRecentlyFocusedNode = target;
|
|
629
|
+
}
|
|
630
|
+
} else {
|
|
631
|
+
// escaped! pull it back in to where it just left
|
|
632
|
+
event.stopImmediatePropagation();
|
|
633
|
+
|
|
634
|
+
// focus will escape if the MRU node had a positive tab index and user tried to nav forward;
|
|
635
|
+
// it will also escape if the MRU node had a 0 tab index and user tried to nav backward
|
|
636
|
+
// toward a node with a positive tab index
|
|
637
|
+
let nextNode; // next node to focus, if we find one
|
|
638
|
+
let navAcrossContainers = true;
|
|
639
|
+
if (getTabIndex(state.mostRecentlyFocusedNode) > 0) {
|
|
640
|
+
// MRU container index must be >=0 otherwise we wouldn't have it as an MRU node...
|
|
641
|
+
const mruContainerIdx = findContainerIndex(
|
|
642
|
+
state.mostRecentlyFocusedNode
|
|
643
|
+
);
|
|
644
|
+
// there MAY not be any tabbable nodes in the container if there are at least 2 containers
|
|
645
|
+
// and the MRU node is focusable but not tabbable (focus-trap requires at least 1 container
|
|
646
|
+
// with at least one tabbable node in order to function, so this could be the other container
|
|
647
|
+
// with nothing tabbable in it)
|
|
648
|
+
const { tabbableNodes } = state.containerGroups[mruContainerIdx];
|
|
649
|
+
if (tabbableNodes.length > 0) {
|
|
650
|
+
// MRU tab index MAY not be found if the MRU node is focusable but not tabbable
|
|
651
|
+
const mruTabIdx = tabbableNodes.findIndex(
|
|
652
|
+
(node) => node === state.mostRecentlyFocusedNode
|
|
653
|
+
);
|
|
654
|
+
if (mruTabIdx >= 0) {
|
|
655
|
+
if (config.isKeyForward(state.recentNavEvent)) {
|
|
656
|
+
if (mruTabIdx + 1 < tabbableNodes.length) {
|
|
657
|
+
nextNode = tabbableNodes[mruTabIdx + 1];
|
|
658
|
+
navAcrossContainers = false;
|
|
659
|
+
}
|
|
660
|
+
// else, don't wrap within the container as focus should move to next/previous
|
|
661
|
+
// container
|
|
662
|
+
} else {
|
|
663
|
+
if (mruTabIdx - 1 >= 0) {
|
|
664
|
+
nextNode = tabbableNodes[mruTabIdx - 1];
|
|
665
|
+
navAcrossContainers = false;
|
|
666
|
+
}
|
|
667
|
+
// else, don't wrap within the container as focus should move to next/previous
|
|
668
|
+
// container
|
|
669
|
+
}
|
|
670
|
+
// else, don't find in container order without considering direction too
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
// else, no tabbable nodes in that container (which means we must have at least one other
|
|
674
|
+
// container with at least one tabbable node in it, otherwise focus-trap would've thrown
|
|
675
|
+
// an error the last time updateTabbableNodes() was run): find next node among all known
|
|
676
|
+
// containers
|
|
677
|
+
} else {
|
|
678
|
+
// check to see if there's at least one tabbable node with a positive tab index inside
|
|
679
|
+
// the trap because focus seems to escape when navigating backward from a tabbable node
|
|
680
|
+
// with tabindex=0 when this is the case (instead of wrapping to the tabbable node with
|
|
681
|
+
// the greatest positive tab index like it should)
|
|
682
|
+
if (
|
|
683
|
+
!state.containerGroups.some((g) =>
|
|
684
|
+
g.tabbableNodes.some((n) => getTabIndex(n) > 0)
|
|
685
|
+
)
|
|
686
|
+
) {
|
|
687
|
+
// no containers with tabbable nodes with positive tab indexes which means the focus
|
|
688
|
+
// escaped for some other reason and we should just execute the fallback to the
|
|
689
|
+
// MRU node or initial focus node, if any
|
|
690
|
+
navAcrossContainers = false;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
if (navAcrossContainers) {
|
|
695
|
+
nextNode = findNextNavNode({
|
|
696
|
+
// move FROM the MRU node, not event-related node (which will be the node that is
|
|
697
|
+
// outside the trap causing the focus escape we're trying to fix)
|
|
698
|
+
target: state.mostRecentlyFocusedNode,
|
|
699
|
+
isBackward: config.isKeyBackward(state.recentNavEvent),
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
if (nextNode) {
|
|
704
|
+
tryFocus(nextNode);
|
|
705
|
+
} else {
|
|
706
|
+
tryFocus(state.mostRecentlyFocusedNode || getInitialFocusNode());
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
state.recentNavEvent = undefined; // clear
|
|
711
|
+
};
|
|
712
|
+
|
|
713
|
+
// Hijack key nav events on the first and last focusable nodes of the trap,
|
|
714
|
+
// in order to prevent focus from escaping. If it escapes for even a
|
|
715
|
+
// moment it can end up scrolling the page and causing confusion so we
|
|
716
|
+
// kind of need to capture the action at the keydown phase.
|
|
717
|
+
const checkKeyNav = function (event, isBackward = false) {
|
|
718
|
+
state.recentNavEvent = event;
|
|
719
|
+
|
|
720
|
+
const destinationNode = findNextNavNode({ event, isBackward });
|
|
556
721
|
if (destinationNode) {
|
|
557
722
|
if (isTabEvent(event)) {
|
|
558
723
|
// since tab natively moves focus, we wouldn't have a destination node unless we
|
|
@@ -655,6 +820,48 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
655
820
|
return trap;
|
|
656
821
|
};
|
|
657
822
|
|
|
823
|
+
//
|
|
824
|
+
// MUTATION OBSERVER
|
|
825
|
+
//
|
|
826
|
+
|
|
827
|
+
const checkDomRemoval = function (mutations) {
|
|
828
|
+
const isFocusedNodeRemoved = mutations.some(function (mutation) {
|
|
829
|
+
const removedNodes = Array.from(mutation.removedNodes);
|
|
830
|
+
return removedNodes.some(function (node) {
|
|
831
|
+
return node === state.mostRecentlyFocusedNode;
|
|
832
|
+
});
|
|
833
|
+
});
|
|
834
|
+
|
|
835
|
+
// If the currently focused is removed then browsers will move focus to the
|
|
836
|
+
// <body> element. If this happens, try to move focus back into the trap.
|
|
837
|
+
if (isFocusedNodeRemoved) {
|
|
838
|
+
tryFocus(getInitialFocusNode());
|
|
839
|
+
}
|
|
840
|
+
};
|
|
841
|
+
|
|
842
|
+
// Use MutationObserver - if supported - to detect if focused node is removed
|
|
843
|
+
// from the DOM.
|
|
844
|
+
const mutationObserver =
|
|
845
|
+
typeof window !== 'undefined' && 'MutationObserver' in window
|
|
846
|
+
? new MutationObserver(checkDomRemoval)
|
|
847
|
+
: undefined;
|
|
848
|
+
|
|
849
|
+
const updateObservedNodes = function () {
|
|
850
|
+
if (!mutationObserver) {
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
mutationObserver.disconnect();
|
|
855
|
+
if (state.active && !state.paused) {
|
|
856
|
+
state.containers.map(function (container) {
|
|
857
|
+
mutationObserver.observe(container, {
|
|
858
|
+
subtree: true,
|
|
859
|
+
childList: true,
|
|
860
|
+
});
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
};
|
|
864
|
+
|
|
658
865
|
//
|
|
659
866
|
// TRAP DEFINITION
|
|
660
867
|
//
|
|
@@ -692,6 +899,7 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
692
899
|
updateTabbableNodes();
|
|
693
900
|
}
|
|
694
901
|
addListeners();
|
|
902
|
+
updateObservedNodes();
|
|
695
903
|
onPostActivate?.();
|
|
696
904
|
};
|
|
697
905
|
|
|
@@ -725,6 +933,7 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
725
933
|
removeListeners();
|
|
726
934
|
state.active = false;
|
|
727
935
|
state.paused = false;
|
|
936
|
+
updateObservedNodes();
|
|
728
937
|
|
|
729
938
|
activeFocusTraps.deactivateTrap(trapStack, trap);
|
|
730
939
|
|
|
@@ -771,6 +980,7 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
771
980
|
onPause?.();
|
|
772
981
|
|
|
773
982
|
removeListeners();
|
|
983
|
+
updateObservedNodes();
|
|
774
984
|
|
|
775
985
|
onPostPause?.();
|
|
776
986
|
return this;
|
|
@@ -789,6 +999,7 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
789
999
|
|
|
790
1000
|
updateTabbableNodes();
|
|
791
1001
|
addListeners();
|
|
1002
|
+
updateObservedNodes();
|
|
792
1003
|
|
|
793
1004
|
onPostUnpause?.();
|
|
794
1005
|
return this;
|
|
@@ -805,6 +1016,8 @@ const createFocusTrap = function (elements, userOptions) {
|
|
|
805
1016
|
updateTabbableNodes();
|
|
806
1017
|
}
|
|
807
1018
|
|
|
1019
|
+
updateObservedNodes();
|
|
1020
|
+
|
|
808
1021
|
return this;
|
|
809
1022
|
},
|
|
810
1023
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "focus-trap",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.5.0",
|
|
4
4
|
"description": "Trap focus within a DOM node.",
|
|
5
5
|
"main": "dist/focus-trap.js",
|
|
6
6
|
"module": "dist/focus-trap.esm.js",
|
|
@@ -63,28 +63,29 @@
|
|
|
63
63
|
},
|
|
64
64
|
"homepage": "https://github.com/focus-trap/focus-trap#readme",
|
|
65
65
|
"dependencies": {
|
|
66
|
-
"tabbable": "^6.
|
|
66
|
+
"tabbable": "^6.2.0"
|
|
67
67
|
},
|
|
68
68
|
"devDependencies": {
|
|
69
|
-
"@babel/cli": "^7.
|
|
70
|
-
"@babel/core": "^7.
|
|
71
|
-
"@babel/eslint-parser": "^7.
|
|
72
|
-
"@babel/preset-env": "^7.
|
|
73
|
-
"@changesets/cli": "^2.26.
|
|
69
|
+
"@babel/cli": "^7.22.5",
|
|
70
|
+
"@babel/core": "^7.22.5",
|
|
71
|
+
"@babel/eslint-parser": "^7.22.5",
|
|
72
|
+
"@babel/preset-env": "^7.22.5",
|
|
73
|
+
"@changesets/cli": "^2.26.2",
|
|
74
74
|
"@rollup/plugin-babel": "^6.0.3",
|
|
75
|
-
"@rollup/plugin-commonjs": "^25.0.
|
|
76
|
-
"@rollup/plugin-node-resolve": "^15.0
|
|
75
|
+
"@rollup/plugin-commonjs": "^25.0.2",
|
|
76
|
+
"@rollup/plugin-node-resolve": "^15.1.0",
|
|
77
|
+
"@rollup/plugin-terser": "^0.4.3",
|
|
77
78
|
"@testing-library/cypress": "^9.0.0",
|
|
78
79
|
"@types/jquery": "^3.5.16",
|
|
79
|
-
"all-contributors-cli": "^6.
|
|
80
|
+
"all-contributors-cli": "^6.26.0",
|
|
80
81
|
"babel-loader": "^9.1.2",
|
|
81
82
|
"cross-env": "^7.0.3",
|
|
82
|
-
"cypress": "^12.
|
|
83
|
+
"cypress": "^12.16.0",
|
|
83
84
|
"cypress-plugin-tab": "^1.0.5",
|
|
84
|
-
"eslint": "^8.
|
|
85
|
+
"eslint": "^8.43.0",
|
|
85
86
|
"eslint-config-prettier": "^8.8.0",
|
|
86
87
|
"eslint-plugin-cypress": "^2.13.3",
|
|
87
|
-
"eslint-plugin-jest": "^27.2.
|
|
88
|
+
"eslint-plugin-jest": "^27.2.2",
|
|
88
89
|
"onchange": "^7.1.0",
|
|
89
90
|
"prettier": "^2.8.8",
|
|
90
91
|
"rollup": "^2.79.1",
|
|
@@ -92,8 +93,7 @@
|
|
|
92
93
|
"rollup-plugin-livereload": "^2.0.5",
|
|
93
94
|
"rollup-plugin-serve": "^2.0.2",
|
|
94
95
|
"rollup-plugin-sourcemaps": "^0.6.3",
|
|
95
|
-
"rollup-plugin-terser": "^7.0.1",
|
|
96
96
|
"start-server-and-test": "^2.0.0",
|
|
97
|
-
"typescript": "^5.
|
|
97
|
+
"typescript": "^5.1.5"
|
|
98
98
|
}
|
|
99
99
|
}
|