@primer/behaviors 0.0.0-202292020482 → 0.0.0-202317223846

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.
@@ -1,5 +1,5 @@
1
1
  export declare type Direction = 'previous' | 'next' | 'start' | 'end';
2
- export declare type FocusMovementKeys = 'ArrowLeft' | 'ArrowDown' | 'ArrowUp' | 'ArrowRight' | 'h' | 'j' | 'k' | 'l' | 'a' | 's' | 'w' | 'd' | 'Tab' | 'Home' | 'End' | 'PageUp' | 'PageDown';
2
+ export declare type FocusMovementKeys = 'ArrowLeft' | 'ArrowDown' | 'ArrowUp' | 'ArrowRight' | 'h' | 'j' | 'k' | 'l' | 'a' | 's' | 'w' | 'd' | 'Tab' | 'Home' | 'End' | 'PageUp' | 'PageDown' | 'Backspace';
3
3
  export declare enum FocusKeys {
4
4
  ArrowHorizontal = 1,
5
5
  ArrowVertical = 2,
@@ -10,6 +10,7 @@ export declare enum FocusKeys {
10
10
  WS = 32,
11
11
  AD = 64,
12
12
  Tab = 128,
13
+ Backspace = 512,
13
14
  ArrowAll = 3,
14
15
  HJKL = 12,
15
16
  WASD = 96,
@@ -17,6 +17,7 @@ var FocusKeys;
17
17
  FocusKeys[FocusKeys["WS"] = 32] = "WS";
18
18
  FocusKeys[FocusKeys["AD"] = 64] = "AD";
19
19
  FocusKeys[FocusKeys["Tab"] = 128] = "Tab";
20
+ FocusKeys[FocusKeys["Backspace"] = 512] = "Backspace";
20
21
  FocusKeys[FocusKeys["ArrowAll"] = 3] = "ArrowAll";
21
22
  FocusKeys[FocusKeys["HJKL"] = 12] = "HJKL";
22
23
  FocusKeys[FocusKeys["WASD"] = 96] = "WASD";
@@ -39,7 +40,8 @@ const KEY_TO_BIT = {
39
40
  Home: FocusKeys.HomeAndEnd,
40
41
  End: FocusKeys.HomeAndEnd,
41
42
  PageUp: FocusKeys.PageUpDown,
42
- PageDown: FocusKeys.PageUpDown
43
+ PageDown: FocusKeys.PageUpDown,
44
+ Backspace: FocusKeys.Backspace
43
45
  };
44
46
  const KEY_TO_DIRECTION = {
45
47
  ArrowLeft: 'previous',
@@ -58,7 +60,8 @@ const KEY_TO_DIRECTION = {
58
60
  Home: 'start',
59
61
  End: 'end',
60
62
  PageUp: 'start',
61
- PageDown: 'end'
63
+ PageDown: 'end',
64
+ Backspace: 'previous'
62
65
  };
63
66
  function getDirection(keyboardEvent) {
64
67
  const direction = KEY_TO_DIRECTION[keyboardEvent.key];
@@ -187,8 +190,7 @@ function focusZone(container, settings) {
187
190
  if (filteredElements.length === 0) {
188
191
  return;
189
192
  }
190
- const insertIndex = focusableElements.findIndex(e => (e.compareDocumentPosition(filteredElements[0]) & Node.DOCUMENT_POSITION_PRECEDING) > 0);
191
- focusableElements.splice(insertIndex === -1 ? focusableElements.length : insertIndex, 0, ...filteredElements);
193
+ focusableElements.splice(findInsertionIndex(filteredElements), 0, ...filteredElements);
192
194
  for (const element of filteredElements) {
193
195
  if (!savedTabIndex.has(element)) {
194
196
  savedTabIndex.set(element, element.getAttribute('tabindex'));
@@ -196,9 +198,31 @@ function focusZone(container, settings) {
196
198
  element.setAttribute('tabindex', '-1');
197
199
  }
198
200
  if (!currentFocusedElement) {
199
- updateFocusedElement(getFirstFocusableElement());
201
+ const initialElement = typeof focusInStrategy === 'function' ? focusInStrategy(document.body) : getFirstFocusableElement();
202
+ updateFocusedElement(initialElement);
200
203
  }
201
204
  }
205
+ function findInsertionIndex(elementsToInsert) {
206
+ const firstElementToInsert = elementsToInsert[0];
207
+ if (focusableElements.length === 0)
208
+ return 0;
209
+ let iMin = 0;
210
+ let iMax = focusableElements.length - 1;
211
+ while (iMin <= iMax) {
212
+ const i = Math.floor((iMin + iMax) / 2);
213
+ const element = focusableElements[i];
214
+ if (followsInDocument(firstElementToInsert, element)) {
215
+ iMax = i - 1;
216
+ }
217
+ else {
218
+ iMin = i + 1;
219
+ }
220
+ }
221
+ return iMin;
222
+ }
223
+ function followsInDocument(first, second) {
224
+ return (second.compareDocumentPosition(first) & Node.DOCUMENT_POSITION_PRECEDING) > 0;
225
+ }
202
226
  function endFocusManagement(...elements) {
203
227
  for (const element of elements) {
204
228
  const focusableElementIndex = focusableElements.indexOf(element);
@@ -1,5 +1,5 @@
1
1
  export declare type Direction = 'previous' | 'next' | 'start' | 'end';
2
- export declare type FocusMovementKeys = 'ArrowLeft' | 'ArrowDown' | 'ArrowUp' | 'ArrowRight' | 'h' | 'j' | 'k' | 'l' | 'a' | 's' | 'w' | 'd' | 'Tab' | 'Home' | 'End' | 'PageUp' | 'PageDown';
2
+ export declare type FocusMovementKeys = 'ArrowLeft' | 'ArrowDown' | 'ArrowUp' | 'ArrowRight' | 'h' | 'j' | 'k' | 'l' | 'a' | 's' | 'w' | 'd' | 'Tab' | 'Home' | 'End' | 'PageUp' | 'PageDown' | 'Backspace';
3
3
  export declare enum FocusKeys {
4
4
  ArrowHorizontal = 1,
5
5
  ArrowVertical = 2,
@@ -10,6 +10,7 @@ export declare enum FocusKeys {
10
10
  WS = 32,
11
11
  AD = 64,
12
12
  Tab = 128,
13
+ Backspace = 512,
13
14
  ArrowAll = 3,
14
15
  HJKL = 12,
15
16
  WASD = 96,
@@ -14,6 +14,7 @@ export var FocusKeys;
14
14
  FocusKeys[FocusKeys["WS"] = 32] = "WS";
15
15
  FocusKeys[FocusKeys["AD"] = 64] = "AD";
16
16
  FocusKeys[FocusKeys["Tab"] = 128] = "Tab";
17
+ FocusKeys[FocusKeys["Backspace"] = 512] = "Backspace";
17
18
  FocusKeys[FocusKeys["ArrowAll"] = 3] = "ArrowAll";
18
19
  FocusKeys[FocusKeys["HJKL"] = 12] = "HJKL";
19
20
  FocusKeys[FocusKeys["WASD"] = 96] = "WASD";
@@ -36,7 +37,8 @@ const KEY_TO_BIT = {
36
37
  Home: FocusKeys.HomeAndEnd,
37
38
  End: FocusKeys.HomeAndEnd,
38
39
  PageUp: FocusKeys.PageUpDown,
39
- PageDown: FocusKeys.PageUpDown
40
+ PageDown: FocusKeys.PageUpDown,
41
+ Backspace: FocusKeys.Backspace
40
42
  };
41
43
  const KEY_TO_DIRECTION = {
42
44
  ArrowLeft: 'previous',
@@ -55,7 +57,8 @@ const KEY_TO_DIRECTION = {
55
57
  Home: 'start',
56
58
  End: 'end',
57
59
  PageUp: 'start',
58
- PageDown: 'end'
60
+ PageDown: 'end',
61
+ Backspace: 'previous'
59
62
  };
60
63
  function getDirection(keyboardEvent) {
61
64
  const direction = KEY_TO_DIRECTION[keyboardEvent.key];
@@ -184,8 +187,7 @@ export function focusZone(container, settings) {
184
187
  if (filteredElements.length === 0) {
185
188
  return;
186
189
  }
187
- const insertIndex = focusableElements.findIndex(e => (e.compareDocumentPosition(filteredElements[0]) & Node.DOCUMENT_POSITION_PRECEDING) > 0);
188
- focusableElements.splice(insertIndex === -1 ? focusableElements.length : insertIndex, 0, ...filteredElements);
190
+ focusableElements.splice(findInsertionIndex(filteredElements), 0, ...filteredElements);
189
191
  for (const element of filteredElements) {
190
192
  if (!savedTabIndex.has(element)) {
191
193
  savedTabIndex.set(element, element.getAttribute('tabindex'));
@@ -193,9 +195,31 @@ export function focusZone(container, settings) {
193
195
  element.setAttribute('tabindex', '-1');
194
196
  }
195
197
  if (!currentFocusedElement) {
196
- updateFocusedElement(getFirstFocusableElement());
198
+ const initialElement = typeof focusInStrategy === 'function' ? focusInStrategy(document.body) : getFirstFocusableElement();
199
+ updateFocusedElement(initialElement);
197
200
  }
198
201
  }
202
+ function findInsertionIndex(elementsToInsert) {
203
+ const firstElementToInsert = elementsToInsert[0];
204
+ if (focusableElements.length === 0)
205
+ return 0;
206
+ let iMin = 0;
207
+ let iMax = focusableElements.length - 1;
208
+ while (iMin <= iMax) {
209
+ const i = Math.floor((iMin + iMax) / 2);
210
+ const element = focusableElements[i];
211
+ if (followsInDocument(firstElementToInsert, element)) {
212
+ iMax = i - 1;
213
+ }
214
+ else {
215
+ iMin = i + 1;
216
+ }
217
+ }
218
+ return iMin;
219
+ }
220
+ function followsInDocument(first, second) {
221
+ return (second.compareDocumentPosition(first) & Node.DOCUMENT_POSITION_PRECEDING) > 0;
222
+ }
199
223
  function endFocusManagement(...elements) {
200
224
  for (const element of elements) {
201
225
  const focusableElementIndex = focusableElements.indexOf(element);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primer/behaviors",
3
- "version": "0.0.0-202292020482",
3
+ "version": "0.0.0-202317223846",
4
4
  "description": "Shared behaviors for JavaScript components",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -74,7 +74,7 @@
74
74
  "@testing-library/user-event": "^13.5.0",
75
75
  "@types/jest": "^27.0.3",
76
76
  "@types/react": "^17.0.37",
77
- "esbuild": "^0.14.1",
77
+ "esbuild": "^0.15.16",
78
78
  "esbuild-jest": "^0.5.0",
79
79
  "eslint": "^8.3.0",
80
80
  "eslint-plugin-github": "^4.3.5",