@primer/behaviors 0.0.0-2022031111953 → 0.0.0-2022031122217

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.
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.focusTrap = void 0;
4
- const event_listener_signal_js_1 = require("./polyfills/event-listener-signal.js");
5
4
  const iterate_focusable_elements_js_1 = require("./utils/iterate-focusable-elements.js");
5
+ const event_listener_signal_js_1 = require("./polyfills/event-listener-signal.js");
6
6
  (0, event_listener_signal_js_1.polyfill)();
7
7
  const suspendedTrapStack = [];
8
8
  let activeTrap = undefined;
@@ -41,12 +41,29 @@ function focusTrap(container, initialFocus, abortSignal) {
41
41
  };
42
42
  container.prepend(sentinelStart);
43
43
  container.append(sentinelEnd);
44
- if (initialFocus) {
45
- initialFocus.focus();
46
- }
47
- else {
48
- const firstFocusableChild = (0, iterate_focusable_elements_js_1.getFocusableChild)(container);
49
- firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
44
+ let lastFocusedChild = undefined;
45
+ function ensureTrapZoneHasFocus(focusedElement) {
46
+ if (focusedElement instanceof HTMLElement && document.contains(container)) {
47
+ if (container.contains(focusedElement)) {
48
+ lastFocusedChild = focusedElement;
49
+ return;
50
+ }
51
+ else {
52
+ if (lastFocusedChild && (0, iterate_focusable_elements_js_1.isTabbable)(lastFocusedChild) && container.contains(lastFocusedChild)) {
53
+ lastFocusedChild.focus();
54
+ return;
55
+ }
56
+ else if (initialFocus && container.contains(initialFocus)) {
57
+ initialFocus.focus();
58
+ return;
59
+ }
60
+ else {
61
+ const firstFocusableChild = (0, iterate_focusable_elements_js_1.getFocusableChild)(container);
62
+ firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
63
+ return;
64
+ }
65
+ }
66
+ }
50
67
  }
51
68
  const wrappingController = followSignal(signal);
52
69
  if (activeTrap) {
@@ -69,6 +86,10 @@ function focusTrap(container, initialFocus, abortSignal) {
69
86
  }
70
87
  tryReactivate();
71
88
  });
89
+ document.addEventListener('focus', event => {
90
+ ensureTrapZoneHasFocus(event.target);
91
+ }, { signal: wrappingController.signal, capture: true });
92
+ ensureTrapZoneHasFocus(document.activeElement);
72
93
  activeTrap = {
73
94
  container,
74
95
  controller: wrappingController,
@@ -1,5 +1,5 @@
1
+ import { getFocusableChild, isTabbable } from './utils/iterate-focusable-elements.js';
1
2
  import { polyfill as eventListenerSignalPolyfill } from './polyfills/event-listener-signal.js';
2
- import { getFocusableChild } from './utils/iterate-focusable-elements.js';
3
3
  eventListenerSignalPolyfill();
4
4
  const suspendedTrapStack = [];
5
5
  let activeTrap = undefined;
@@ -38,12 +38,29 @@ export function focusTrap(container, initialFocus, abortSignal) {
38
38
  };
39
39
  container.prepend(sentinelStart);
40
40
  container.append(sentinelEnd);
41
- if (initialFocus) {
42
- initialFocus.focus();
43
- }
44
- else {
45
- const firstFocusableChild = getFocusableChild(container);
46
- firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
41
+ let lastFocusedChild = undefined;
42
+ function ensureTrapZoneHasFocus(focusedElement) {
43
+ if (focusedElement instanceof HTMLElement && document.contains(container)) {
44
+ if (container.contains(focusedElement)) {
45
+ lastFocusedChild = focusedElement;
46
+ return;
47
+ }
48
+ else {
49
+ if (lastFocusedChild && isTabbable(lastFocusedChild) && container.contains(lastFocusedChild)) {
50
+ lastFocusedChild.focus();
51
+ return;
52
+ }
53
+ else if (initialFocus && container.contains(initialFocus)) {
54
+ initialFocus.focus();
55
+ return;
56
+ }
57
+ else {
58
+ const firstFocusableChild = getFocusableChild(container);
59
+ firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
60
+ return;
61
+ }
62
+ }
63
+ }
47
64
  }
48
65
  const wrappingController = followSignal(signal);
49
66
  if (activeTrap) {
@@ -66,6 +83,10 @@ export function focusTrap(container, initialFocus, abortSignal) {
66
83
  }
67
84
  tryReactivate();
68
85
  });
86
+ document.addEventListener('focus', event => {
87
+ ensureTrapZoneHasFocus(event.target);
88
+ }, { signal: wrappingController.signal, capture: true });
89
+ ensureTrapZoneHasFocus(document.activeElement);
69
90
  activeTrap = {
70
91
  container,
71
92
  controller: wrappingController,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primer/behaviors",
3
- "version": "0.0.0-2022031111953",
3
+ "version": "0.0.0-2022031122217",
4
4
  "description": "Shared behaviors for JavaScript components",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",