@primer/behaviors 0.0.0-2022031111953 → 0.0.0-20221313611
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/dist/cjs/focus-trap.js +28 -7
- package/dist/esm/focus-trap.js +28 -7
- package/package.json +3 -11
- package/dist/cjs/components/modal-dialog.d.ts +0 -17
- package/dist/cjs/components/modal-dialog.js +0 -92
- package/dist/esm/components/modal-dialog.d.ts +0 -17
- package/dist/esm/components/modal-dialog.js +0 -90
package/dist/cjs/focus-trap.js
CHANGED
|
@@ -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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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,
|
package/dist/esm/focus-trap.js
CHANGED
|
@@ -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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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-
|
|
3
|
+
"version": "0.0.0-20221313611",
|
|
4
4
|
"description": "Shared behaviors for JavaScript components",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -14,26 +14,18 @@
|
|
|
14
14
|
"types": "./dist/utils/index.d.ts",
|
|
15
15
|
"require": "./dist/cjs/utils/index.js",
|
|
16
16
|
"module": "./dist/esm/utils/index.js"
|
|
17
|
-
},
|
|
18
|
-
"./components/modal-dialog": {
|
|
19
|
-
"types": "./dist/components/modal-dialog.d.ts",
|
|
20
|
-
"require": "./dist/cjs/components/modal-dialog.js",
|
|
21
|
-
"module": "./dist/esm/components/modal-dialog.js"
|
|
22
17
|
}
|
|
23
18
|
},
|
|
24
19
|
"types": "dist/cjs/index.d.ts",
|
|
25
20
|
"files": [
|
|
26
21
|
"dist",
|
|
27
|
-
"utils"
|
|
28
|
-
"modal-dialog"
|
|
22
|
+
"utils"
|
|
29
23
|
],
|
|
30
24
|
"sideEffects": [
|
|
31
25
|
"dist/esm/focus-zone.js",
|
|
32
26
|
"dist/esm/focus-trap.js",
|
|
33
27
|
"dist/cjs/focus-zone.js",
|
|
34
|
-
"dist/cjs/focus-trap.js"
|
|
35
|
-
"dist/esm/components/modal-dialog.js",
|
|
36
|
-
"dist/cjs/components/modal-dialog.js"
|
|
28
|
+
"dist/cjs/focus-trap.js"
|
|
37
29
|
],
|
|
38
30
|
"scripts": {
|
|
39
31
|
"lint": "eslint src/",
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
declare class ModalDialogElement extends HTMLElement {
|
|
2
|
-
private abortController;
|
|
3
|
-
constructor();
|
|
4
|
-
connectedCallback(): void;
|
|
5
|
-
disconnectedCallback(): void;
|
|
6
|
-
show(): void;
|
|
7
|
-
close(): void;
|
|
8
|
-
}
|
|
9
|
-
declare global {
|
|
10
|
-
interface Window {
|
|
11
|
-
ModalDialogElement: typeof ModalDialogElement;
|
|
12
|
-
}
|
|
13
|
-
interface HTMLElementTagNameMap {
|
|
14
|
-
'modal-dialog': ModalDialogElement;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export default ModalDialogElement;
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const focus_trap_js_1 = require("../focus-trap.js");
|
|
4
|
-
class ModalDialogElement extends HTMLElement {
|
|
5
|
-
constructor() {
|
|
6
|
-
var _a, _b;
|
|
7
|
-
super();
|
|
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());
|
|
10
|
-
}
|
|
11
|
-
connectedCallback() {
|
|
12
|
-
if (!this.hasAttribute('role'))
|
|
13
|
-
this.setAttribute('role', 'dialog');
|
|
14
|
-
const subscriptions = [
|
|
15
|
-
fromEvent(this, 'compositionstart', e => trackComposition(this, e)),
|
|
16
|
-
fromEvent(this, 'compositionend', e => trackComposition(this, e)),
|
|
17
|
-
fromEvent(this, 'keydown', e => keydown(this, e))
|
|
18
|
-
];
|
|
19
|
-
states.set(this, { subscriptions, loaded: false, isComposing: false });
|
|
20
|
-
}
|
|
21
|
-
disconnectedCallback() {
|
|
22
|
-
const state = states.get(this);
|
|
23
|
-
if (!state)
|
|
24
|
-
return;
|
|
25
|
-
states.delete(this);
|
|
26
|
-
for (const sub of state.subscriptions) {
|
|
27
|
-
sub.unsubscribe();
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
show() {
|
|
31
|
-
var _a;
|
|
32
|
-
const isClosed = this.classList.contains('hidden');
|
|
33
|
-
if (!isClosed)
|
|
34
|
-
return;
|
|
35
|
-
this.classList.remove('hidden');
|
|
36
|
-
this.setAttribute('open', '');
|
|
37
|
-
if ((_a = this.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('modal-dialog-backdrop')) {
|
|
38
|
-
this.parentElement.classList.add('active');
|
|
39
|
-
}
|
|
40
|
-
document.body.style.overflow = 'hidden';
|
|
41
|
-
this.abortController = (0, focus_trap_js_1.focusTrap)(this);
|
|
42
|
-
}
|
|
43
|
-
close() {
|
|
44
|
-
var _a, _b;
|
|
45
|
-
const isClosed = this.classList.contains('hidden');
|
|
46
|
-
if (isClosed)
|
|
47
|
-
return;
|
|
48
|
-
this.classList.add('hidden');
|
|
49
|
-
this.removeAttribute('open');
|
|
50
|
-
if ((_a = this.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('modal-dialog-backdrop')) {
|
|
51
|
-
this.parentElement.classList.remove('active');
|
|
52
|
-
}
|
|
53
|
-
document.body.style.overflow = 'initial';
|
|
54
|
-
(_b = this.abortController) === null || _b === void 0 ? void 0 : _b.abort();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
const states = new WeakMap();
|
|
58
|
-
function fromEvent(target, eventName, onNext, options = false) {
|
|
59
|
-
target.addEventListener(eventName, onNext, options);
|
|
60
|
-
return {
|
|
61
|
-
unsubscribe: () => {
|
|
62
|
-
target.removeEventListener(eventName, onNext, options);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
function keydown(dialog, event) {
|
|
67
|
-
if (!(event instanceof KeyboardEvent))
|
|
68
|
-
return;
|
|
69
|
-
const state = states.get(dialog);
|
|
70
|
-
if (!state || state.isComposing)
|
|
71
|
-
return;
|
|
72
|
-
switch (event.key) {
|
|
73
|
-
case 'Escape':
|
|
74
|
-
if (dialog.hasAttribute('open')) {
|
|
75
|
-
dialog.close();
|
|
76
|
-
event.preventDefault();
|
|
77
|
-
event.stopPropagation();
|
|
78
|
-
}
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
function trackComposition(dialog, event) {
|
|
83
|
-
const state = states.get(dialog);
|
|
84
|
-
if (!state)
|
|
85
|
-
return;
|
|
86
|
-
state.isComposing = event.type === 'compositionstart';
|
|
87
|
-
}
|
|
88
|
-
exports.default = ModalDialogElement;
|
|
89
|
-
if (!window.customElements.get('modal-dialog')) {
|
|
90
|
-
window.ModalDialogElement = ModalDialogElement;
|
|
91
|
-
window.customElements.define('modal-dialog', ModalDialogElement);
|
|
92
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
declare class ModalDialogElement extends HTMLElement {
|
|
2
|
-
private abortController;
|
|
3
|
-
constructor();
|
|
4
|
-
connectedCallback(): void;
|
|
5
|
-
disconnectedCallback(): void;
|
|
6
|
-
show(): void;
|
|
7
|
-
close(): void;
|
|
8
|
-
}
|
|
9
|
-
declare global {
|
|
10
|
-
interface Window {
|
|
11
|
-
ModalDialogElement: typeof ModalDialogElement;
|
|
12
|
-
}
|
|
13
|
-
interface HTMLElementTagNameMap {
|
|
14
|
-
'modal-dialog': ModalDialogElement;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
export default ModalDialogElement;
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { focusTrap } from '../focus-trap.js';
|
|
2
|
-
class ModalDialogElement extends HTMLElement {
|
|
3
|
-
constructor() {
|
|
4
|
-
var _a, _b;
|
|
5
|
-
super();
|
|
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());
|
|
8
|
-
}
|
|
9
|
-
connectedCallback() {
|
|
10
|
-
if (!this.hasAttribute('role'))
|
|
11
|
-
this.setAttribute('role', 'dialog');
|
|
12
|
-
const subscriptions = [
|
|
13
|
-
fromEvent(this, 'compositionstart', e => trackComposition(this, e)),
|
|
14
|
-
fromEvent(this, 'compositionend', e => trackComposition(this, e)),
|
|
15
|
-
fromEvent(this, 'keydown', e => keydown(this, e))
|
|
16
|
-
];
|
|
17
|
-
states.set(this, { subscriptions, loaded: false, isComposing: false });
|
|
18
|
-
}
|
|
19
|
-
disconnectedCallback() {
|
|
20
|
-
const state = states.get(this);
|
|
21
|
-
if (!state)
|
|
22
|
-
return;
|
|
23
|
-
states.delete(this);
|
|
24
|
-
for (const sub of state.subscriptions) {
|
|
25
|
-
sub.unsubscribe();
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
show() {
|
|
29
|
-
var _a;
|
|
30
|
-
const isClosed = this.classList.contains('hidden');
|
|
31
|
-
if (!isClosed)
|
|
32
|
-
return;
|
|
33
|
-
this.classList.remove('hidden');
|
|
34
|
-
this.setAttribute('open', '');
|
|
35
|
-
if ((_a = this.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('modal-dialog-backdrop')) {
|
|
36
|
-
this.parentElement.classList.add('active');
|
|
37
|
-
}
|
|
38
|
-
document.body.style.overflow = 'hidden';
|
|
39
|
-
this.abortController = focusTrap(this);
|
|
40
|
-
}
|
|
41
|
-
close() {
|
|
42
|
-
var _a, _b;
|
|
43
|
-
const isClosed = this.classList.contains('hidden');
|
|
44
|
-
if (isClosed)
|
|
45
|
-
return;
|
|
46
|
-
this.classList.add('hidden');
|
|
47
|
-
this.removeAttribute('open');
|
|
48
|
-
if ((_a = this.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('modal-dialog-backdrop')) {
|
|
49
|
-
this.parentElement.classList.remove('active');
|
|
50
|
-
}
|
|
51
|
-
document.body.style.overflow = 'initial';
|
|
52
|
-
(_b = this.abortController) === null || _b === void 0 ? void 0 : _b.abort();
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
const states = new WeakMap();
|
|
56
|
-
function fromEvent(target, eventName, onNext, options = false) {
|
|
57
|
-
target.addEventListener(eventName, onNext, options);
|
|
58
|
-
return {
|
|
59
|
-
unsubscribe: () => {
|
|
60
|
-
target.removeEventListener(eventName, onNext, options);
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
function keydown(dialog, event) {
|
|
65
|
-
if (!(event instanceof KeyboardEvent))
|
|
66
|
-
return;
|
|
67
|
-
const state = states.get(dialog);
|
|
68
|
-
if (!state || state.isComposing)
|
|
69
|
-
return;
|
|
70
|
-
switch (event.key) {
|
|
71
|
-
case 'Escape':
|
|
72
|
-
if (dialog.hasAttribute('open')) {
|
|
73
|
-
dialog.close();
|
|
74
|
-
event.preventDefault();
|
|
75
|
-
event.stopPropagation();
|
|
76
|
-
}
|
|
77
|
-
break;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
function trackComposition(dialog, event) {
|
|
81
|
-
const state = states.get(dialog);
|
|
82
|
-
if (!state)
|
|
83
|
-
return;
|
|
84
|
-
state.isComposing = event.type === 'compositionstart';
|
|
85
|
-
}
|
|
86
|
-
export default ModalDialogElement;
|
|
87
|
-
if (!window.customElements.get('modal-dialog')) {
|
|
88
|
-
window.ModalDialogElement = ModalDialogElement;
|
|
89
|
-
window.customElements.define('modal-dialog', ModalDialogElement);
|
|
90
|
-
}
|