@primer/behaviors 1.0.3 → 1.1.0-rc.e605114
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/CHANGELOG.md +27 -0
- package/dist/cjs/focus-trap.d.ts +1 -2
- package/dist/cjs/focus-trap.js +23 -27
- package/dist/cjs/utils/iterate-focusable-elements.d.ts +1 -0
- package/dist/cjs/utils/iterate-focusable-elements.js +7 -2
- package/dist/esm/focus-trap.d.ts +1 -2
- package/dist/esm/focus-trap.js +24 -28
- package/dist/esm/utils/iterate-focusable-elements.d.ts +1 -0
- package/dist/esm/utils/iterate-focusable-elements.js +5 -1
- package/package.json +1 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @primer/behaviors
|
|
2
|
+
|
|
3
|
+
## 1.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#52](https://github.com/primer/behaviors/pull/52) [`1aa3027`](https://github.com/primer/behaviors/commit/1aa302782e3c833f9d9c27f602a046e81f05c3e5) Thanks [@owenniblock](https://github.com/owenniblock)! - Update focusTrap to use new methodology after accessibility discussions
|
|
8
|
+
|
|
9
|
+
## 1.0.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#42](https://github.com/primer/behaviors/pull/42) [`41945a3`](https://github.com/primer/behaviors/commit/41945a37ef07da82ce5a29feb03d7a7d96ec76ea) Thanks [@dgreif](https://github.com/dgreif)! - Build both esm and cjs output for the package
|
|
14
|
+
|
|
15
|
+
## 1.0.2
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [#17](https://github.com/primer/behaviors/pull/17) [`9194ba4`](https://github.com/primer/behaviors/commit/9194ba403502b4acba0be03bed1a765c1ba81340) Thanks [@dgreif](https://github.com/dgreif)! - Correct margin orientation for `scrollIntoView`
|
|
20
|
+
|
|
21
|
+
* [#18](https://github.com/primer/behaviors/pull/18) [`3b4dd41`](https://github.com/primer/behaviors/commit/3b4dd414175417f83bd144939fe74b2a01bc7136) Thanks [@dgreif](https://github.com/dgreif)! - Export utils as submodule
|
|
22
|
+
|
|
23
|
+
## 1.0.1
|
|
24
|
+
|
|
25
|
+
### Patch Changes
|
|
26
|
+
|
|
27
|
+
- [#10](https://github.com/primer/behaviors/pull/10) [`88b6f34`](https://github.com/primer/behaviors/commit/88b6f34bf4874f3c81473020a01a58b197dd6e16) Thanks [@dgreif](https://github.com/dgreif)! - Set up changesets
|
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
|
@@ -19,13 +19,28 @@ 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');
|
|
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);
|
|
29
44
|
let lastFocusedChild = undefined;
|
|
30
45
|
function ensureTrapZoneHasFocus(focusedElement) {
|
|
31
46
|
if (focusedElement instanceof HTMLElement && document.contains(container)) {
|
|
@@ -43,36 +58,14 @@ function focusTrap(container, initialFocus, abortSignal) {
|
|
|
43
58
|
return;
|
|
44
59
|
}
|
|
45
60
|
else {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
container.setAttribute('tabindex', '-1');
|
|
49
|
-
}
|
|
50
|
-
container.focus();
|
|
51
|
-
if (containerNeedsTemporaryTabIndex) {
|
|
52
|
-
container.addEventListener('blur', () => container.removeAttribute('tabindex'), { once: true });
|
|
53
|
-
}
|
|
61
|
+
const firstFocusableChild = (0, iterate_focusable_elements_js_1.getFocusableChild)(container);
|
|
62
|
+
firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
|
|
54
63
|
return;
|
|
55
64
|
}
|
|
56
65
|
}
|
|
57
66
|
}
|
|
58
67
|
}
|
|
59
68
|
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
69
|
if (activeTrap) {
|
|
77
70
|
const suspendedTrap = activeTrap;
|
|
78
71
|
activeTrap.container.setAttribute('data-focus-trap', 'suspended');
|
|
@@ -84,6 +77,9 @@ function focusTrap(container, initialFocus, abortSignal) {
|
|
|
84
77
|
});
|
|
85
78
|
signal.addEventListener('abort', () => {
|
|
86
79
|
container.removeAttribute('data-focus-trap');
|
|
80
|
+
const sentinels = container.getElementsByClassName('sentinel');
|
|
81
|
+
while (sentinels.length > 0)
|
|
82
|
+
sentinels[0].remove();
|
|
87
83
|
const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
|
|
88
84
|
if (suspendedTrapIndex >= 0) {
|
|
89
85
|
suspendedTrapStack.splice(suspendedTrapIndex, 1);
|
|
@@ -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) {
|
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,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getFocusableChild, isTabbable } from './utils/iterate-focusable-elements.js';
|
|
2
2
|
import { polyfill as eventListenerSignalPolyfill } from './polyfills/event-listener-signal.js';
|
|
3
3
|
eventListenerSignalPolyfill();
|
|
4
4
|
const suspendedTrapStack = [];
|
|
@@ -16,13 +16,28 @@ 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');
|
|
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);
|
|
26
41
|
let lastFocusedChild = undefined;
|
|
27
42
|
function ensureTrapZoneHasFocus(focusedElement) {
|
|
28
43
|
if (focusedElement instanceof HTMLElement && document.contains(container)) {
|
|
@@ -40,36 +55,14 @@ export function focusTrap(container, initialFocus, abortSignal) {
|
|
|
40
55
|
return;
|
|
41
56
|
}
|
|
42
57
|
else {
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
container.setAttribute('tabindex', '-1');
|
|
46
|
-
}
|
|
47
|
-
container.focus();
|
|
48
|
-
if (containerNeedsTemporaryTabIndex) {
|
|
49
|
-
container.addEventListener('blur', () => container.removeAttribute('tabindex'), { once: true });
|
|
50
|
-
}
|
|
58
|
+
const firstFocusableChild = getFocusableChild(container);
|
|
59
|
+
firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
|
|
51
60
|
return;
|
|
52
61
|
}
|
|
53
62
|
}
|
|
54
63
|
}
|
|
55
64
|
}
|
|
56
65
|
const wrappingController = followSignal(signal);
|
|
57
|
-
container.addEventListener('keydown', event => {
|
|
58
|
-
if (event.key !== 'Tab' || event.defaultPrevented) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
const { target } = event;
|
|
62
|
-
const firstFocusableChild = getFocusableChild(container);
|
|
63
|
-
const lastFocusableChild = getFocusableChild(container, true);
|
|
64
|
-
if (target === firstFocusableChild && event.shiftKey) {
|
|
65
|
-
event.preventDefault();
|
|
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 });
|
|
73
66
|
if (activeTrap) {
|
|
74
67
|
const suspendedTrap = activeTrap;
|
|
75
68
|
activeTrap.container.setAttribute('data-focus-trap', 'suspended');
|
|
@@ -81,6 +74,9 @@ export function focusTrap(container, initialFocus, abortSignal) {
|
|
|
81
74
|
});
|
|
82
75
|
signal.addEventListener('abort', () => {
|
|
83
76
|
container.removeAttribute('data-focus-trap');
|
|
77
|
+
const sentinels = container.getElementsByClassName('sentinel');
|
|
78
|
+
while (sentinels.length > 0)
|
|
79
|
+
sentinels[0].remove();
|
|
84
80
|
const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
|
|
85
81
|
if (suspendedTrapIndex >= 0) {
|
|
86
82
|
suspendedTrapStack.splice(suspendedTrapIndex, 1);
|
|
@@ -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) {
|