@primer/behaviors 0.0.0-2022031111842 → 0.0.0-20221313210

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.
@@ -6,7 +6,10 @@ class ModalDialogElement extends HTMLElement {
6
6
  var _a, _b;
7
7
  super();
8
8
  (_a = this.querySelector('.close-button')) === null || _a === void 0 ? void 0 : _a.addEventListener('click', () => this.close());
9
- (_b = document.body.querySelector(`.js-dialog-show-${this.id}`)) === null || _b === void 0 ? void 0 : _b.addEventListener('click', () => this.show());
9
+ (_b = document.body.querySelector(`.js-dialog-show-${this.id}`)) === null || _b === void 0 ? void 0 : _b.addEventListener('click', event => {
10
+ event.stopPropagation();
11
+ this.show();
12
+ });
10
13
  }
11
14
  connectedCallback() {
12
15
  if (!this.hasAttribute('role'))
@@ -14,7 +17,8 @@ class ModalDialogElement extends HTMLElement {
14
17
  const subscriptions = [
15
18
  fromEvent(this, 'compositionstart', e => trackComposition(this, e)),
16
19
  fromEvent(this, 'compositionend', e => trackComposition(this, e)),
17
- fromEvent(this, 'keydown', e => keydown(this, e))
20
+ fromEvent(this, 'keydown', e => keydown(this, e)),
21
+ fromEvent(window, 'click', e => clickToDismiss(this, e))
18
22
  ];
19
23
  states.set(this, { subscriptions, loaded: false, isComposing: false });
20
24
  }
@@ -85,6 +89,12 @@ function trackComposition(dialog, event) {
85
89
  return;
86
90
  state.isComposing = event.type === 'compositionstart';
87
91
  }
92
+ function clickToDismiss(dialog, event) {
93
+ const target = event.target;
94
+ if (dialog.hasAttribute('open') && target && !target.matches(`#${dialog.id}, #${dialog.id} *`)) {
95
+ dialog.close();
96
+ }
97
+ }
88
98
  exports.default = ModalDialogElement;
89
99
  if (!window.customElements.get('modal-dialog')) {
90
100
  window.ModalDialogElement = ModalDialogElement;
@@ -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,
@@ -4,7 +4,10 @@ class ModalDialogElement extends HTMLElement {
4
4
  var _a, _b;
5
5
  super();
6
6
  (_a = this.querySelector('.close-button')) === null || _a === void 0 ? void 0 : _a.addEventListener('click', () => this.close());
7
- (_b = document.body.querySelector(`.js-dialog-show-${this.id}`)) === null || _b === void 0 ? void 0 : _b.addEventListener('click', () => this.show());
7
+ (_b = document.body.querySelector(`.js-dialog-show-${this.id}`)) === null || _b === void 0 ? void 0 : _b.addEventListener('click', event => {
8
+ event.stopPropagation();
9
+ this.show();
10
+ });
8
11
  }
9
12
  connectedCallback() {
10
13
  if (!this.hasAttribute('role'))
@@ -12,7 +15,8 @@ class ModalDialogElement extends HTMLElement {
12
15
  const subscriptions = [
13
16
  fromEvent(this, 'compositionstart', e => trackComposition(this, e)),
14
17
  fromEvent(this, 'compositionend', e => trackComposition(this, e)),
15
- fromEvent(this, 'keydown', e => keydown(this, e))
18
+ fromEvent(this, 'keydown', e => keydown(this, e)),
19
+ fromEvent(window, 'click', e => clickToDismiss(this, e))
16
20
  ];
17
21
  states.set(this, { subscriptions, loaded: false, isComposing: false });
18
22
  }
@@ -83,6 +87,12 @@ function trackComposition(dialog, event) {
83
87
  return;
84
88
  state.isComposing = event.type === 'compositionstart';
85
89
  }
90
+ function clickToDismiss(dialog, event) {
91
+ const target = event.target;
92
+ if (dialog.hasAttribute('open') && target && !target.matches(`#${dialog.id}, #${dialog.id} *`)) {
93
+ dialog.close();
94
+ }
95
+ }
86
96
  export default ModalDialogElement;
87
97
  if (!window.customElements.get('modal-dialog')) {
88
98
  window.ModalDialogElement = ModalDialogElement;
@@ -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-2022031111842",
3
+ "version": "0.0.0-20221313210",
4
4
  "description": "Shared behaviors for JavaScript components",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",