@primer/behaviors 1.0.3 → 1.1.1-rc.fe5ded1

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 ADDED
@@ -0,0 +1,33 @@
1
+ # @primer/behaviors
2
+
3
+ ## 1.1.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#73](https://github.com/primer/behaviors/pull/73) [`a96d60f`](https://github.com/primer/behaviors/commit/a96d60fdb2a2ffdde71a22ea29fa2c788bf4c6aa) Thanks [@dgreif](https://github.com/dgreif)! - Add `import` conditional export type to the package for better NodeJS ESM compatibility
8
+
9
+ ## 1.1.0
10
+
11
+ ### Minor Changes
12
+
13
+ - [#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
14
+
15
+ ## 1.0.3
16
+
17
+ ### Patch Changes
18
+
19
+ - [#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
20
+
21
+ ## 1.0.2
22
+
23
+ ### Patch Changes
24
+
25
+ - [#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`
26
+
27
+ * [#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
28
+
29
+ ## 1.0.1
30
+
31
+ ### Patch Changes
32
+
33
+ - [#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
@@ -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;
@@ -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 containerNeedsTemporaryTabIndex = container.getAttribute('tabindex') === null;
47
- if (containerNeedsTemporaryTabIndex) {
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
- if (disabledAttrInert || hiddenInert || hiddenInputInert) {
44
+ const sentinelInert = elem.classList.contains('sentinel');
45
+ if (disabledAttrInert || hiddenInert || hiddenInputInert || sentinelInert) {
41
46
  return false;
42
47
  }
43
48
  if (strict) {
@@ -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;
@@ -1,4 +1,4 @@
1
- import { isTabbable, iterateFocusableElements } from './utils/iterate-focusable-elements.js';
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 containerNeedsTemporaryTabIndex = container.getAttribute('tabindex') === null;
44
- if (containerNeedsTemporaryTabIndex) {
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
- if (disabledAttrInert || hiddenInert || hiddenInputInert) {
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,22 +1,24 @@
1
1
  {
2
2
  "name": "@primer/behaviors",
3
- "version": "1.0.3",
3
+ "version": "1.1.1-rc.fe5ded1",
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
+ "module": "./dist/esm/index.js",
10
+ "import": "./dist/esm/index.js",
10
11
  "require": "./dist/cjs/index.js",
11
- "module": "./dist/esm/index.js"
12
+ "types": "./dist/esm/index.d.ts"
12
13
  },
13
14
  "./utils": {
14
- "types": "./dist/utils/index.d.ts",
15
+ "module": "./dist/esm/utils/index.js",
16
+ "import": "./dist/esm/utils/index.js",
15
17
  "require": "./dist/cjs/utils/index.js",
16
- "module": "./dist/esm/utils/index.js"
18
+ "types": "./dist/esm/utils/index.d.ts"
17
19
  }
18
20
  },
19
- "types": "dist/cjs/index.d.ts",
21
+ "types": "dist/esm/index.d.ts",
20
22
  "files": [
21
23
  "dist",
22
24
  "utils"
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@primer/behaviors/utils",
3
3
  "types": "../dist/esm/utils/index.d.ts",
4
- "main": "../dist/esm/utils/index.js",
5
- "type": "module",
4
+ "main": "../dist/cjs/utils/index.js",
5
+ "module": "../dist/esm/utils/index.js",
6
6
  "sideEffects": false
7
7
  }