@primer/behaviors 0.0.0-2022020145740 → 0.0.0-202202813730
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/components/modal-dialog.d.ts +8 -0
- package/dist/cjs/components/modal-dialog.js +89 -0
- package/dist/cjs/focus-trap.d.ts +1 -2
- package/dist/cjs/focus-trap.js +28 -53
- package/dist/cjs/utils/iterate-focusable-elements.d.ts +1 -0
- package/dist/cjs/utils/iterate-focusable-elements.js +7 -2
- package/dist/esm/components/modal-dialog.d.ts +8 -0
- package/dist/esm/components/modal-dialog.js +85 -0
- package/dist/esm/focus-trap.d.ts +1 -2
- package/dist/esm/focus-trap.js +28 -53
- package/dist/esm/utils/iterate-focusable-elements.d.ts +1 -0
- package/dist/esm/utils/iterate-focusable-elements.js +5 -1
- package/package.json +8 -3
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ModalDialogElement = void 0;
|
|
4
|
+
const focus_trap_js_1 = require("../focus-trap.js");
|
|
5
|
+
class ModalDialogElement extends HTMLElement {
|
|
6
|
+
constructor() {
|
|
7
|
+
var _a, _b;
|
|
8
|
+
super();
|
|
9
|
+
(_a = this.querySelector('.close-button')) === null || _a === void 0 ? void 0 : _a.addEventListener('click', () => this.close());
|
|
10
|
+
(_b = document.body.querySelector(`.js-dialog-show-${this.id}`)) === null || _b === void 0 ? void 0 : _b.addEventListener('click', () => this.show());
|
|
11
|
+
}
|
|
12
|
+
connectedCallback() {
|
|
13
|
+
if (!this.hasAttribute('role'))
|
|
14
|
+
this.setAttribute('role', 'dialog');
|
|
15
|
+
const subscriptions = [
|
|
16
|
+
fromEvent(this, 'compositionstart', e => trackComposition(this, e)),
|
|
17
|
+
fromEvent(this, 'compositionend', e => trackComposition(this, e)),
|
|
18
|
+
fromEvent(this, 'keydown', e => keydown(this, e))
|
|
19
|
+
];
|
|
20
|
+
states.set(this, { subscriptions, loaded: false, isComposing: false });
|
|
21
|
+
}
|
|
22
|
+
disconnectedCallback() {
|
|
23
|
+
const state = states.get(this);
|
|
24
|
+
if (!state)
|
|
25
|
+
return;
|
|
26
|
+
states.delete(this);
|
|
27
|
+
for (const sub of state.subscriptions) {
|
|
28
|
+
sub.unsubscribe();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
show() {
|
|
32
|
+
var _a;
|
|
33
|
+
const isClosed = this.classList.contains('hidden');
|
|
34
|
+
if (!isClosed)
|
|
35
|
+
return;
|
|
36
|
+
this.classList.remove('hidden');
|
|
37
|
+
this.setAttribute('open', '');
|
|
38
|
+
if ((_a = this.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('modal-dialog-backdrop')) {
|
|
39
|
+
this.parentElement.classList.add('active');
|
|
40
|
+
}
|
|
41
|
+
document.body.style.overflow = 'hidden';
|
|
42
|
+
this.abortController = (0, focus_trap_js_1.focusTrap)(this);
|
|
43
|
+
}
|
|
44
|
+
close() {
|
|
45
|
+
var _a, _b;
|
|
46
|
+
const isClosed = this.classList.contains('hidden');
|
|
47
|
+
if (isClosed)
|
|
48
|
+
return;
|
|
49
|
+
this.classList.add('hidden');
|
|
50
|
+
this.removeAttribute('open');
|
|
51
|
+
if ((_a = this.parentElement) === null || _a === void 0 ? void 0 : _a.classList.contains('modal-dialog-backdrop')) {
|
|
52
|
+
this.parentElement.classList.remove('active');
|
|
53
|
+
}
|
|
54
|
+
document.body.style.overflow = 'initial';
|
|
55
|
+
(_b = this.abortController) === null || _b === void 0 ? void 0 : _b.abort();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.ModalDialogElement = ModalDialogElement;
|
|
59
|
+
const states = new WeakMap();
|
|
60
|
+
function fromEvent(target, eventName, onNext, options = false) {
|
|
61
|
+
target.addEventListener(eventName, onNext, options);
|
|
62
|
+
return {
|
|
63
|
+
unsubscribe: () => {
|
|
64
|
+
target.removeEventListener(eventName, onNext, options);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
function keydown(dialog, event) {
|
|
69
|
+
if (!(event instanceof KeyboardEvent))
|
|
70
|
+
return;
|
|
71
|
+
const state = states.get(dialog);
|
|
72
|
+
if (!state || state.isComposing)
|
|
73
|
+
return;
|
|
74
|
+
switch (event.key) {
|
|
75
|
+
case 'Escape':
|
|
76
|
+
if (dialog.hasAttribute('open')) {
|
|
77
|
+
dialog.close();
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
event.stopPropagation();
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function trackComposition(dialog, event) {
|
|
85
|
+
const state = states.get(dialog);
|
|
86
|
+
if (!state)
|
|
87
|
+
return;
|
|
88
|
+
state.isComposing = event.type === 'compositionstart';
|
|
89
|
+
}
|
package/dist/cjs/focus-trap.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export declare function focusTrap(container: HTMLElement, initialFocus?: HTMLElement): AbortController;
|
|
2
|
-
export declare function focusTrap(container: HTMLElement, initialFocus: HTMLElement | undefined, abortSignal: AbortSignal): void;
|
|
1
|
+
export declare function focusTrap(container: HTMLElement, initialFocus?: HTMLElement, abortSignal?: AbortSignal): AbortController | undefined;
|
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 iterate_focusable_elements_js_1 = require("./utils/iterate-focusable-elements.js");
|
|
5
4
|
const event_listener_signal_js_1 = require("./polyfills/event-listener-signal.js");
|
|
5
|
+
const iterate_focusable_elements_js_1 = require("./utils/iterate-focusable-elements.js");
|
|
6
6
|
(0, event_listener_signal_js_1.polyfill)();
|
|
7
7
|
const suspendedTrapStack = [];
|
|
8
8
|
let activeTrap = undefined;
|
|
@@ -19,60 +19,36 @@ function followSignal(signal) {
|
|
|
19
19
|
});
|
|
20
20
|
return controller;
|
|
21
21
|
}
|
|
22
|
-
function getFocusableChild(container, lastChild = false) {
|
|
23
|
-
return (0, iterate_focusable_elements_js_1.iterateFocusableElements)(container, { reverse: lastChild, strict: true, onlyTabbable: true }).next().value;
|
|
24
|
-
}
|
|
25
22
|
function focusTrap(container, initialFocus, abortSignal) {
|
|
26
23
|
const controller = new AbortController();
|
|
27
24
|
const signal = abortSignal !== null && abortSignal !== void 0 ? abortSignal : controller.signal;
|
|
28
25
|
container.setAttribute('data-focus-trap', 'active');
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
26
|
+
const sentinelStart = document.createElement('span');
|
|
27
|
+
sentinelStart.setAttribute('class', 'sentinel');
|
|
28
|
+
sentinelStart.setAttribute('tabindex', '0');
|
|
29
|
+
sentinelStart.setAttribute('aria-hidden', 'true');
|
|
30
|
+
sentinelStart.onfocus = () => {
|
|
31
|
+
const lastFocusableChild = (0, iterate_focusable_elements_js_1.getFocusableChild)(container, true);
|
|
32
|
+
lastFocusableChild === null || lastFocusableChild === void 0 ? void 0 : lastFocusableChild.focus();
|
|
33
|
+
};
|
|
34
|
+
const sentinelEnd = document.createElement('span');
|
|
35
|
+
sentinelEnd.setAttribute('class', 'sentinel');
|
|
36
|
+
sentinelEnd.setAttribute('tabindex', '0');
|
|
37
|
+
sentinelEnd.setAttribute('aria-hidden', 'true');
|
|
38
|
+
sentinelEnd.onfocus = () => {
|
|
39
|
+
const firstFocusableChild = (0, iterate_focusable_elements_js_1.getFocusableChild)(container);
|
|
40
|
+
firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
|
|
41
|
+
};
|
|
42
|
+
container.prepend(sentinelStart);
|
|
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();
|
|
58
50
|
}
|
|
59
51
|
const wrappingController = followSignal(signal);
|
|
60
|
-
container.addEventListener('keydown', event => {
|
|
61
|
-
if (event.key !== 'Tab' || event.defaultPrevented) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
const { target } = event;
|
|
65
|
-
const firstFocusableChild = getFocusableChild(container);
|
|
66
|
-
const lastFocusableChild = getFocusableChild(container, true);
|
|
67
|
-
if (target === firstFocusableChild && event.shiftKey) {
|
|
68
|
-
event.preventDefault();
|
|
69
|
-
lastFocusableChild === null || lastFocusableChild === void 0 ? void 0 : lastFocusableChild.focus();
|
|
70
|
-
}
|
|
71
|
-
else if (target === lastFocusableChild && !event.shiftKey) {
|
|
72
|
-
event.preventDefault();
|
|
73
|
-
firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
|
|
74
|
-
}
|
|
75
|
-
}, { signal: wrappingController.signal });
|
|
76
52
|
if (activeTrap) {
|
|
77
53
|
const suspendedTrap = activeTrap;
|
|
78
54
|
activeTrap.container.setAttribute('data-focus-trap', 'suspended');
|
|
@@ -84,16 +60,15 @@ function focusTrap(container, initialFocus, abortSignal) {
|
|
|
84
60
|
});
|
|
85
61
|
signal.addEventListener('abort', () => {
|
|
86
62
|
container.removeAttribute('data-focus-trap');
|
|
63
|
+
const sentinels = container.getElementsByClassName('sentinel');
|
|
64
|
+
while (sentinels.length > 0)
|
|
65
|
+
sentinels[0].remove();
|
|
87
66
|
const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
|
|
88
67
|
if (suspendedTrapIndex >= 0) {
|
|
89
68
|
suspendedTrapStack.splice(suspendedTrapIndex, 1);
|
|
90
69
|
}
|
|
91
70
|
tryReactivate();
|
|
92
71
|
});
|
|
93
|
-
document.addEventListener('focus', event => {
|
|
94
|
-
ensureTrapZoneHasFocus(event.target);
|
|
95
|
-
}, { signal: wrappingController.signal, capture: true });
|
|
96
|
-
ensureTrapZoneHasFocus(document.activeElement);
|
|
97
72
|
activeTrap = {
|
|
98
73
|
container,
|
|
99
74
|
controller: wrappingController,
|
|
@@ -4,5 +4,6 @@ export interface IterateFocusableElements {
|
|
|
4
4
|
onlyTabbable?: boolean;
|
|
5
5
|
}
|
|
6
6
|
export declare function iterateFocusableElements(container: HTMLElement, options?: IterateFocusableElements): Generator<HTMLElement, undefined, undefined>;
|
|
7
|
+
export declare function getFocusableChild(container: HTMLElement, lastChild?: boolean): HTMLElement | undefined;
|
|
7
8
|
export declare function isFocusable(elem: HTMLElement, strict?: boolean): boolean;
|
|
8
9
|
export declare function isTabbable(elem: HTMLElement, strict?: boolean): boolean;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isTabbable = exports.isFocusable = exports.iterateFocusableElements = void 0;
|
|
3
|
+
exports.isTabbable = exports.isFocusable = exports.getFocusableChild = exports.iterateFocusableElements = void 0;
|
|
4
4
|
function* iterateFocusableElements(container, options = {}) {
|
|
5
5
|
var _a, _b;
|
|
6
6
|
const strict = (_a = options.strict) !== null && _a !== void 0 ? _a : false;
|
|
@@ -32,12 +32,17 @@ function* iterateFocusableElements(container, options = {}) {
|
|
|
32
32
|
return undefined;
|
|
33
33
|
}
|
|
34
34
|
exports.iterateFocusableElements = iterateFocusableElements;
|
|
35
|
+
function getFocusableChild(container, lastChild = false) {
|
|
36
|
+
return iterateFocusableElements(container, { reverse: lastChild, strict: true, onlyTabbable: true }).next().value;
|
|
37
|
+
}
|
|
38
|
+
exports.getFocusableChild = getFocusableChild;
|
|
35
39
|
function isFocusable(elem, strict = false) {
|
|
36
40
|
const disabledAttrInert = ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'OPTGROUP', 'OPTION', 'FIELDSET'].includes(elem.tagName) &&
|
|
37
41
|
elem.disabled;
|
|
38
42
|
const hiddenInert = elem.hidden;
|
|
39
43
|
const hiddenInputInert = elem instanceof HTMLInputElement && elem.type === 'hidden';
|
|
40
|
-
|
|
44
|
+
const sentinelInert = elem.classList.contains('sentinel');
|
|
45
|
+
if (disabledAttrInert || hiddenInert || hiddenInputInert || sentinelInert) {
|
|
41
46
|
return false;
|
|
42
47
|
}
|
|
43
48
|
if (strict) {
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { focusTrap } from '../focus-trap.js';
|
|
2
|
+
export 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
|
+
}
|
package/dist/esm/focus-trap.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
export declare function focusTrap(container: HTMLElement, initialFocus?: HTMLElement): AbortController;
|
|
2
|
-
export declare function focusTrap(container: HTMLElement, initialFocus: HTMLElement | undefined, abortSignal: AbortSignal): void;
|
|
1
|
+
export declare function focusTrap(container: HTMLElement, initialFocus?: HTMLElement, abortSignal?: AbortSignal): AbortController | undefined;
|
package/dist/esm/focus-trap.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { isTabbable, iterateFocusableElements } from './utils/iterate-focusable-elements.js';
|
|
2
1
|
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;
|
|
@@ -16,60 +16,36 @@ function followSignal(signal) {
|
|
|
16
16
|
});
|
|
17
17
|
return controller;
|
|
18
18
|
}
|
|
19
|
-
function getFocusableChild(container, lastChild = false) {
|
|
20
|
-
return iterateFocusableElements(container, { reverse: lastChild, strict: true, onlyTabbable: true }).next().value;
|
|
21
|
-
}
|
|
22
19
|
export function focusTrap(container, initialFocus, abortSignal) {
|
|
23
20
|
const controller = new AbortController();
|
|
24
21
|
const signal = abortSignal !== null && abortSignal !== void 0 ? abortSignal : controller.signal;
|
|
25
22
|
container.setAttribute('data-focus-trap', 'active');
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
container.focus();
|
|
48
|
-
if (containerNeedsTemporaryTabIndex) {
|
|
49
|
-
container.addEventListener('blur', () => container.removeAttribute('tabindex'), { once: true });
|
|
50
|
-
}
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
23
|
+
const sentinelStart = document.createElement('span');
|
|
24
|
+
sentinelStart.setAttribute('class', 'sentinel');
|
|
25
|
+
sentinelStart.setAttribute('tabindex', '0');
|
|
26
|
+
sentinelStart.setAttribute('aria-hidden', 'true');
|
|
27
|
+
sentinelStart.onfocus = () => {
|
|
28
|
+
const lastFocusableChild = getFocusableChild(container, true);
|
|
29
|
+
lastFocusableChild === null || lastFocusableChild === void 0 ? void 0 : lastFocusableChild.focus();
|
|
30
|
+
};
|
|
31
|
+
const sentinelEnd = document.createElement('span');
|
|
32
|
+
sentinelEnd.setAttribute('class', 'sentinel');
|
|
33
|
+
sentinelEnd.setAttribute('tabindex', '0');
|
|
34
|
+
sentinelEnd.setAttribute('aria-hidden', 'true');
|
|
35
|
+
sentinelEnd.onfocus = () => {
|
|
36
|
+
const firstFocusableChild = getFocusableChild(container);
|
|
37
|
+
firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
|
|
38
|
+
};
|
|
39
|
+
container.prepend(sentinelStart);
|
|
40
|
+
container.append(sentinelEnd);
|
|
41
|
+
if (initialFocus) {
|
|
42
|
+
initialFocus.focus();
|
|
55
43
|
}
|
|
56
|
-
|
|
57
|
-
container.addEventListener('keydown', event => {
|
|
58
|
-
if (event.key !== 'Tab' || event.defaultPrevented) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const { target } = event;
|
|
44
|
+
else {
|
|
62
45
|
const firstFocusableChild = getFocusableChild(container);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
lastFocusableChild === null || lastFocusableChild === void 0 ? void 0 : lastFocusableChild.focus();
|
|
67
|
-
}
|
|
68
|
-
else if (target === lastFocusableChild && !event.shiftKey) {
|
|
69
|
-
event.preventDefault();
|
|
70
|
-
firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
|
|
71
|
-
}
|
|
72
|
-
}, { signal: wrappingController.signal });
|
|
46
|
+
firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
|
|
47
|
+
}
|
|
48
|
+
const wrappingController = followSignal(signal);
|
|
73
49
|
if (activeTrap) {
|
|
74
50
|
const suspendedTrap = activeTrap;
|
|
75
51
|
activeTrap.container.setAttribute('data-focus-trap', 'suspended');
|
|
@@ -81,16 +57,15 @@ export function focusTrap(container, initialFocus, abortSignal) {
|
|
|
81
57
|
});
|
|
82
58
|
signal.addEventListener('abort', () => {
|
|
83
59
|
container.removeAttribute('data-focus-trap');
|
|
60
|
+
const sentinels = container.getElementsByClassName('sentinel');
|
|
61
|
+
while (sentinels.length > 0)
|
|
62
|
+
sentinels[0].remove();
|
|
84
63
|
const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
|
|
85
64
|
if (suspendedTrapIndex >= 0) {
|
|
86
65
|
suspendedTrapStack.splice(suspendedTrapIndex, 1);
|
|
87
66
|
}
|
|
88
67
|
tryReactivate();
|
|
89
68
|
});
|
|
90
|
-
document.addEventListener('focus', event => {
|
|
91
|
-
ensureTrapZoneHasFocus(event.target);
|
|
92
|
-
}, { signal: wrappingController.signal, capture: true });
|
|
93
|
-
ensureTrapZoneHasFocus(document.activeElement);
|
|
94
69
|
activeTrap = {
|
|
95
70
|
container,
|
|
96
71
|
controller: wrappingController,
|
|
@@ -4,5 +4,6 @@ export interface IterateFocusableElements {
|
|
|
4
4
|
onlyTabbable?: boolean;
|
|
5
5
|
}
|
|
6
6
|
export declare function iterateFocusableElements(container: HTMLElement, options?: IterateFocusableElements): Generator<HTMLElement, undefined, undefined>;
|
|
7
|
+
export declare function getFocusableChild(container: HTMLElement, lastChild?: boolean): HTMLElement | undefined;
|
|
7
8
|
export declare function isFocusable(elem: HTMLElement, strict?: boolean): boolean;
|
|
8
9
|
export declare function isTabbable(elem: HTMLElement, strict?: boolean): boolean;
|
|
@@ -28,12 +28,16 @@ export function* iterateFocusableElements(container, options = {}) {
|
|
|
28
28
|
}
|
|
29
29
|
return undefined;
|
|
30
30
|
}
|
|
31
|
+
export function getFocusableChild(container, lastChild = false) {
|
|
32
|
+
return iterateFocusableElements(container, { reverse: lastChild, strict: true, onlyTabbable: true }).next().value;
|
|
33
|
+
}
|
|
31
34
|
export function isFocusable(elem, strict = false) {
|
|
32
35
|
const disabledAttrInert = ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'OPTGROUP', 'OPTION', 'FIELDSET'].includes(elem.tagName) &&
|
|
33
36
|
elem.disabled;
|
|
34
37
|
const hiddenInert = elem.hidden;
|
|
35
38
|
const hiddenInputInert = elem instanceof HTMLInputElement && elem.type === 'hidden';
|
|
36
|
-
|
|
39
|
+
const sentinelInert = elem.classList.contains('sentinel');
|
|
40
|
+
if (disabledAttrInert || hiddenInert || hiddenInputInert || sentinelInert) {
|
|
37
41
|
return false;
|
|
38
42
|
}
|
|
39
43
|
if (strict) {
|
package/package.json
CHANGED
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@primer/behaviors",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-202202813730",
|
|
4
4
|
"description": "Shared behaviors for JavaScript components",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
|
-
"types": "./dist/index.d.ts",
|
|
9
|
+
"types": "./dist/cjs/index.d.ts",
|
|
10
10
|
"require": "./dist/cjs/index.js",
|
|
11
11
|
"module": "./dist/esm/index.js"
|
|
12
12
|
},
|
|
13
13
|
"./utils": {
|
|
14
|
-
"types": "./dist/utils/index.d.ts",
|
|
14
|
+
"types": "./dist/cjs/utils/index.d.ts",
|
|
15
15
|
"require": "./dist/cjs/utils/index.js",
|
|
16
16
|
"module": "./dist/esm/utils/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./modal-dialog": {
|
|
19
|
+
"types": "./dist/cjs/components/modal-dialog.d.ts",
|
|
20
|
+
"require": "./dist/cjs/components/modal-dialog.js",
|
|
21
|
+
"module": "./dist/esm/components/modal-dialog.js"
|
|
17
22
|
}
|
|
18
23
|
},
|
|
19
24
|
"types": "dist/cjs/index.d.ts",
|