@primer/behaviors 1.7.1 → 1.7.2
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 +25 -0
- package/dist/esm/focus-trap.mjs +25 -0
- package/package.json +1 -1
package/dist/cjs/focus-trap.js
CHANGED
|
@@ -19,6 +19,29 @@ function followSignal(signal) {
|
|
|
19
19
|
});
|
|
20
20
|
return controller;
|
|
21
21
|
}
|
|
22
|
+
function observeFocusTrap(container, sentinels) {
|
|
23
|
+
const observer = new MutationObserver(mutations => {
|
|
24
|
+
for (const mutation of mutations) {
|
|
25
|
+
if (mutation.type === 'childList' && mutation.addedNodes.length) {
|
|
26
|
+
const sentinelChildren = Array.from(mutation.addedNodes).filter(e => e instanceof HTMLElement && e.classList.contains('sentinel') && e.tagName === 'SPAN');
|
|
27
|
+
if (sentinelChildren.length) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const firstChild = container.firstElementChild;
|
|
31
|
+
const lastChild = container.lastElementChild;
|
|
32
|
+
const [sentinelStart, sentinelEnd] = sentinels;
|
|
33
|
+
if (!(firstChild === null || firstChild === void 0 ? void 0 : firstChild.classList.contains('sentinel'))) {
|
|
34
|
+
container.insertAdjacentElement('afterbegin', sentinelStart);
|
|
35
|
+
}
|
|
36
|
+
if (!(lastChild === null || lastChild === void 0 ? void 0 : lastChild.classList.contains('sentinel'))) {
|
|
37
|
+
container.insertAdjacentElement('beforeend', sentinelEnd);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
observer.observe(container, { childList: true });
|
|
43
|
+
return observer;
|
|
44
|
+
}
|
|
22
45
|
function focusTrap(container, initialFocus, abortSignal) {
|
|
23
46
|
const controller = new AbortController();
|
|
24
47
|
const signal = abortSignal !== null && abortSignal !== void 0 ? abortSignal : controller.signal;
|
|
@@ -41,6 +64,7 @@ function focusTrap(container, initialFocus, abortSignal) {
|
|
|
41
64
|
};
|
|
42
65
|
container.prepend(sentinelStart);
|
|
43
66
|
container.append(sentinelEnd);
|
|
67
|
+
const observer = observeFocusTrap(container, [sentinelStart, sentinelEnd]);
|
|
44
68
|
let lastFocusedChild = undefined;
|
|
45
69
|
function ensureTrapZoneHasFocus(focusedElement) {
|
|
46
70
|
if (focusedElement instanceof HTMLElement && document.contains(container)) {
|
|
@@ -84,6 +108,7 @@ function focusTrap(container, initialFocus, abortSignal) {
|
|
|
84
108
|
if (suspendedTrapIndex >= 0) {
|
|
85
109
|
suspendedTrapStack.splice(suspendedTrapIndex, 1);
|
|
86
110
|
}
|
|
111
|
+
observer.disconnect();
|
|
87
112
|
tryReactivate();
|
|
88
113
|
});
|
|
89
114
|
document.addEventListener('focus', event => {
|
package/dist/esm/focus-trap.mjs
CHANGED
|
@@ -17,6 +17,29 @@ function followSignal(signal) {
|
|
|
17
17
|
});
|
|
18
18
|
return controller;
|
|
19
19
|
}
|
|
20
|
+
function observeFocusTrap(container, sentinels) {
|
|
21
|
+
const observer = new MutationObserver(mutations => {
|
|
22
|
+
for (const mutation of mutations) {
|
|
23
|
+
if (mutation.type === 'childList' && mutation.addedNodes.length) {
|
|
24
|
+
const sentinelChildren = Array.from(mutation.addedNodes).filter(e => e instanceof HTMLElement && e.classList.contains('sentinel') && e.tagName === 'SPAN');
|
|
25
|
+
if (sentinelChildren.length) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const firstChild = container.firstElementChild;
|
|
29
|
+
const lastChild = container.lastElementChild;
|
|
30
|
+
const [sentinelStart, sentinelEnd] = sentinels;
|
|
31
|
+
if (!(firstChild === null || firstChild === void 0 ? void 0 : firstChild.classList.contains('sentinel'))) {
|
|
32
|
+
container.insertAdjacentElement('afterbegin', sentinelStart);
|
|
33
|
+
}
|
|
34
|
+
if (!(lastChild === null || lastChild === void 0 ? void 0 : lastChild.classList.contains('sentinel'))) {
|
|
35
|
+
container.insertAdjacentElement('beforeend', sentinelEnd);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
observer.observe(container, { childList: true });
|
|
41
|
+
return observer;
|
|
42
|
+
}
|
|
20
43
|
function focusTrap(container, initialFocus, abortSignal) {
|
|
21
44
|
const controller = new AbortController();
|
|
22
45
|
const signal = abortSignal !== null && abortSignal !== void 0 ? abortSignal : controller.signal;
|
|
@@ -39,6 +62,7 @@ function focusTrap(container, initialFocus, abortSignal) {
|
|
|
39
62
|
};
|
|
40
63
|
container.prepend(sentinelStart);
|
|
41
64
|
container.append(sentinelEnd);
|
|
65
|
+
const observer = observeFocusTrap(container, [sentinelStart, sentinelEnd]);
|
|
42
66
|
let lastFocusedChild = undefined;
|
|
43
67
|
function ensureTrapZoneHasFocus(focusedElement) {
|
|
44
68
|
if (focusedElement instanceof HTMLElement && document.contains(container)) {
|
|
@@ -82,6 +106,7 @@ function focusTrap(container, initialFocus, abortSignal) {
|
|
|
82
106
|
if (suspendedTrapIndex >= 0) {
|
|
83
107
|
suspendedTrapStack.splice(suspendedTrapIndex, 1);
|
|
84
108
|
}
|
|
109
|
+
observer.disconnect();
|
|
85
110
|
tryReactivate();
|
|
86
111
|
});
|
|
87
112
|
document.addEventListener('focus', event => {
|