@primer/behaviors 0.0.0-202292020482 → 0.0.0-20230925220411
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.
|
@@ -26,6 +26,8 @@ function getAnchoredPosition(floatingElement, anchorElement, settings = {}) {
|
|
|
26
26
|
}
|
|
27
27
|
exports.getAnchoredPosition = getAnchoredPosition;
|
|
28
28
|
function getPositionedParent(element) {
|
|
29
|
+
if (isOnTopLayer(element))
|
|
30
|
+
return document.body;
|
|
29
31
|
let parentNode = element.parentNode;
|
|
30
32
|
while (parentNode !== null) {
|
|
31
33
|
if (parentNode instanceof HTMLElement && getComputedStyle(parentNode).position !== 'static') {
|
|
@@ -35,6 +37,21 @@ function getPositionedParent(element) {
|
|
|
35
37
|
}
|
|
36
38
|
return document.body;
|
|
37
39
|
}
|
|
40
|
+
function isOnTopLayer(element) {
|
|
41
|
+
var _a;
|
|
42
|
+
if (element.tagName === 'DIALOG') {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
if (element.matches(':popover-open') && /native code/.test((_a = document.body.showPopover) === null || _a === void 0 ? void 0 : _a.toString())) {
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
catch (_b) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
38
55
|
function getClippingRect(element) {
|
|
39
56
|
let parentNode = element;
|
|
40
57
|
while (parentNode !== null) {
|
package/dist/cjs/focus-zone.d.ts
CHANGED
|
@@ -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,
|
package/dist/cjs/focus-zone.js
CHANGED
|
@@ -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
|
-
|
|
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'));
|
|
@@ -199,6 +201,27 @@ function focusZone(container, settings) {
|
|
|
199
201
|
updateFocusedElement(getFirstFocusableElement());
|
|
200
202
|
}
|
|
201
203
|
}
|
|
204
|
+
function findInsertionIndex(elementsToInsert) {
|
|
205
|
+
const firstElementToInsert = elementsToInsert[0];
|
|
206
|
+
if (focusableElements.length === 0)
|
|
207
|
+
return 0;
|
|
208
|
+
let iMin = 0;
|
|
209
|
+
let iMax = focusableElements.length - 1;
|
|
210
|
+
while (iMin <= iMax) {
|
|
211
|
+
const i = Math.floor((iMin + iMax) / 2);
|
|
212
|
+
const element = focusableElements[i];
|
|
213
|
+
if (followsInDocument(firstElementToInsert, element)) {
|
|
214
|
+
iMax = i - 1;
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
iMin = i + 1;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return iMin;
|
|
221
|
+
}
|
|
222
|
+
function followsInDocument(first, second) {
|
|
223
|
+
return (second.compareDocumentPosition(first) & Node.DOCUMENT_POSITION_PRECEDING) > 0;
|
|
224
|
+
}
|
|
202
225
|
function endFocusManagement(...elements) {
|
|
203
226
|
for (const element of elements) {
|
|
204
227
|
const focusableElementIndex = focusableElements.indexOf(element);
|
|
@@ -222,7 +245,8 @@ function focusZone(container, settings) {
|
|
|
222
245
|
}
|
|
223
246
|
}
|
|
224
247
|
beginFocusManagement(...(0, iterate_focusable_elements_js_1.iterateFocusableElements)(container));
|
|
225
|
-
|
|
248
|
+
const initialElement = typeof focusInStrategy === 'function' ? focusInStrategy(document.body) : getFirstFocusableElement();
|
|
249
|
+
updateFocusedElement(initialElement);
|
|
226
250
|
const observer = new MutationObserver(mutations => {
|
|
227
251
|
for (const mutation of mutations) {
|
|
228
252
|
for (const removedNode of mutation.removedNodes) {
|
|
@@ -22,6 +22,8 @@ export function getAnchoredPosition(floatingElement, anchorElement, settings = {
|
|
|
22
22
|
return pureCalculateAnchoredPosition(clippingRect, relativeRect, floatingElement.getBoundingClientRect(), anchorElement instanceof Element ? anchorElement.getBoundingClientRect() : anchorElement, getDefaultSettings(settings));
|
|
23
23
|
}
|
|
24
24
|
function getPositionedParent(element) {
|
|
25
|
+
if (isOnTopLayer(element))
|
|
26
|
+
return document.body;
|
|
25
27
|
let parentNode = element.parentNode;
|
|
26
28
|
while (parentNode !== null) {
|
|
27
29
|
if (parentNode instanceof HTMLElement && getComputedStyle(parentNode).position !== 'static') {
|
|
@@ -31,6 +33,21 @@ function getPositionedParent(element) {
|
|
|
31
33
|
}
|
|
32
34
|
return document.body;
|
|
33
35
|
}
|
|
36
|
+
function isOnTopLayer(element) {
|
|
37
|
+
var _a;
|
|
38
|
+
if (element.tagName === 'DIALOG') {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
if (element.matches(':popover-open') && /native code/.test((_a = document.body.showPopover) === null || _a === void 0 ? void 0 : _a.toString())) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (_b) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
34
51
|
function getClippingRect(element) {
|
|
35
52
|
let parentNode = element;
|
|
36
53
|
while (parentNode !== null) {
|
package/dist/esm/focus-zone.d.ts
CHANGED
|
@@ -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,
|
package/dist/esm/focus-zone.js
CHANGED
|
@@ -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
|
-
|
|
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'));
|
|
@@ -196,6 +198,27 @@ export function focusZone(container, settings) {
|
|
|
196
198
|
updateFocusedElement(getFirstFocusableElement());
|
|
197
199
|
}
|
|
198
200
|
}
|
|
201
|
+
function findInsertionIndex(elementsToInsert) {
|
|
202
|
+
const firstElementToInsert = elementsToInsert[0];
|
|
203
|
+
if (focusableElements.length === 0)
|
|
204
|
+
return 0;
|
|
205
|
+
let iMin = 0;
|
|
206
|
+
let iMax = focusableElements.length - 1;
|
|
207
|
+
while (iMin <= iMax) {
|
|
208
|
+
const i = Math.floor((iMin + iMax) / 2);
|
|
209
|
+
const element = focusableElements[i];
|
|
210
|
+
if (followsInDocument(firstElementToInsert, element)) {
|
|
211
|
+
iMax = i - 1;
|
|
212
|
+
}
|
|
213
|
+
else {
|
|
214
|
+
iMin = i + 1;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return iMin;
|
|
218
|
+
}
|
|
219
|
+
function followsInDocument(first, second) {
|
|
220
|
+
return (second.compareDocumentPosition(first) & Node.DOCUMENT_POSITION_PRECEDING) > 0;
|
|
221
|
+
}
|
|
199
222
|
function endFocusManagement(...elements) {
|
|
200
223
|
for (const element of elements) {
|
|
201
224
|
const focusableElementIndex = focusableElements.indexOf(element);
|
|
@@ -219,7 +242,8 @@ export function focusZone(container, settings) {
|
|
|
219
242
|
}
|
|
220
243
|
}
|
|
221
244
|
beginFocusManagement(...iterateFocusableElements(container));
|
|
222
|
-
|
|
245
|
+
const initialElement = typeof focusInStrategy === 'function' ? focusInStrategy(document.body) : getFirstFocusableElement();
|
|
246
|
+
updateFocusedElement(initialElement);
|
|
223
247
|
const observer = new MutationObserver(mutations => {
|
|
224
248
|
for (const mutation of mutations) {
|
|
225
249
|
for (const removedNode of mutation.removedNodes) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@primer/behaviors",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-20230925220411",
|
|
4
4
|
"description": "Shared behaviors for JavaScript components",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -30,17 +30,17 @@
|
|
|
30
30
|
"dist/cjs/focus-trap.js"
|
|
31
31
|
],
|
|
32
32
|
"scripts": {
|
|
33
|
-
"lint": "eslint src/",
|
|
34
|
-
"test": "npm run jest && npm run lint",
|
|
35
|
-
"test:watch": "jest --watch",
|
|
36
|
-
"jest": "jest",
|
|
37
33
|
"clean": "rm -rf dist",
|
|
38
|
-
"prebuild": "npm run clean",
|
|
39
34
|
"build": "npm run build:esm && npm run build:cjs",
|
|
40
35
|
"build:esm": "tsc",
|
|
41
36
|
"build:cjs": "tsc --module commonjs --outDir dist/cjs",
|
|
37
|
+
"lint": "eslint src/",
|
|
38
|
+
"test": "jest",
|
|
39
|
+
"test:watch": "jest --watch",
|
|
40
|
+
"prebuild": "npm run clean",
|
|
41
|
+
"release": "npm run build && changeset publish",
|
|
42
42
|
"size-limit": "npm run build && size-limit",
|
|
43
|
-
"
|
|
43
|
+
"type-check": "tsc --noEmit"
|
|
44
44
|
},
|
|
45
45
|
"repository": {
|
|
46
46
|
"type": "git",
|
|
@@ -69,12 +69,12 @@
|
|
|
69
69
|
"@changesets/changelog-github": "^0.4.2",
|
|
70
70
|
"@changesets/cli": "^2.18.1",
|
|
71
71
|
"@github/prettier-config": "0.0.4",
|
|
72
|
-
"@size-limit/preset-small-lib": "^
|
|
72
|
+
"@size-limit/preset-small-lib": "^8.2.4",
|
|
73
73
|
"@testing-library/react": "^12.1.2",
|
|
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.
|
|
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",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"prettier": "^2.5.0",
|
|
83
83
|
"react": "^17.0.2",
|
|
84
84
|
"react-dom": "^17.0.2",
|
|
85
|
-
"size-limit": "^
|
|
85
|
+
"size-limit": "^8.2.4",
|
|
86
86
|
"typescript": "^4.5.2"
|
|
87
87
|
}
|
|
88
88
|
}
|