@primer/behaviors 0.0.0-2022927173910 → 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) {
@@ -25,6 +25,7 @@ export interface FocusZoneSettings {
25
25
  activeDescendantControl?: HTMLElement;
26
26
  onActiveDescendantChanged?: (newActiveDescendant: HTMLElement | undefined, previousActiveDescendant: HTMLElement | undefined, directlyActivated: boolean) => void;
27
27
  focusInStrategy?: 'first' | 'closest' | 'previous' | ((previousFocusedElement: Element) => HTMLElement | undefined);
28
+ preventScroll?: boolean;
28
29
  }
29
30
  export declare const isActiveDescendantAttribute = "data-is-active-descendant";
30
31
  export declare const activeDescendantActivatedDirectly = "activated-directly";
@@ -127,7 +127,7 @@ exports.activeDescendantActivatedDirectly = 'activated-directly';
127
127
  exports.activeDescendantActivatedIndirectly = 'activated-indirectly';
128
128
  exports.hasActiveDescendantAttribute = 'data-has-active-descendant';
129
129
  function focusZone(container, settings) {
130
- var _a, _b, _c, _d;
130
+ var _a, _b, _c, _d, _e;
131
131
  const focusableElements = [];
132
132
  const savedTabIndex = new WeakMap();
133
133
  const bindKeys = (_a = settings === null || settings === void 0 ? void 0 : settings.bindKeys) !== null && _a !== void 0 ? _a : ((settings === null || settings === void 0 ? void 0 : settings.getNextFocusable) ? FocusKeys.ArrowAll : FocusKeys.ArrowVertical) | FocusKeys.HomeAndEnd;
@@ -136,6 +136,7 @@ function focusZone(container, settings) {
136
136
  const activeDescendantControl = settings === null || settings === void 0 ? void 0 : settings.activeDescendantControl;
137
137
  const activeDescendantCallback = settings === null || settings === void 0 ? void 0 : settings.onActiveDescendantChanged;
138
138
  let currentFocusedElement;
139
+ const preventScroll = (_d = settings === null || settings === void 0 ? void 0 : settings.preventScroll) !== null && _d !== void 0 ? _d : false;
139
140
  function getFirstFocusableElement() {
140
141
  return focusableElements[0];
141
142
  }
@@ -189,8 +190,7 @@ function focusZone(container, settings) {
189
190
  if (filteredElements.length === 0) {
190
191
  return;
191
192
  }
192
- const insertIndex = focusableElements.findIndex(e => (e.compareDocumentPosition(filteredElements[0]) & Node.DOCUMENT_POSITION_PRECEDING) > 0);
193
- focusableElements.splice(insertIndex === -1 ? focusableElements.length : insertIndex, 0, ...filteredElements);
193
+ focusableElements.splice(findInsertionIndex(filteredElements), 0, ...filteredElements);
194
194
  for (const element of filteredElements) {
195
195
  if (!savedTabIndex.has(element)) {
196
196
  savedTabIndex.set(element, element.getAttribute('tabindex'));
@@ -201,6 +201,27 @@ function focusZone(container, settings) {
201
201
  updateFocusedElement(getFirstFocusableElement());
202
202
  }
203
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
+ }
204
225
  function endFocusManagement(...elements) {
205
226
  for (const element of elements) {
206
227
  const focusableElementIndex = focusableElements.indexOf(element);
@@ -224,7 +245,8 @@ function focusZone(container, settings) {
224
245
  }
225
246
  }
226
247
  beginFocusManagement(...(0, iterate_focusable_elements_js_1.iterateFocusableElements)(container));
227
- updateFocusedElement(getFirstFocusableElement());
248
+ const initialElement = typeof focusInStrategy === 'function' ? focusInStrategy(document.body) : getFirstFocusableElement();
249
+ updateFocusedElement(initialElement);
228
250
  const observer = new MutationObserver(mutations => {
229
251
  for (const mutation of mutations) {
230
252
  for (const removedNode of mutation.removedNodes) {
@@ -246,7 +268,7 @@ function focusZone(container, settings) {
246
268
  childList: true
247
269
  });
248
270
  const controller = new AbortController();
249
- const signal = (_d = settings === null || settings === void 0 ? void 0 : settings.abortSignal) !== null && _d !== void 0 ? _d : controller.signal;
271
+ const signal = (_e = settings === null || settings === void 0 ? void 0 : settings.abortSignal) !== null && _e !== void 0 ? _e : controller.signal;
250
272
  signal.addEventListener('abort', () => {
251
273
  endFocusManagement(...focusableElements);
252
274
  });
@@ -259,7 +281,7 @@ function focusZone(container, settings) {
259
281
  if (activeDescendantControl) {
260
282
  container.addEventListener('focusin', event => {
261
283
  if (event.target instanceof HTMLElement && focusableElements.includes(event.target)) {
262
- activeDescendantControl.focus();
284
+ activeDescendantControl.focus({ preventScroll });
263
285
  updateFocusedElement(event.target);
264
286
  }
265
287
  });
@@ -303,7 +325,7 @@ function focusZone(container, settings) {
303
325
  if (event.relatedTarget instanceof Element && !container.contains(event.relatedTarget)) {
304
326
  const targetElementIndex = lastKeyboardFocusDirection === 'previous' ? focusableElements.length - 1 : 0;
305
327
  const targetElement = focusableElements[targetElementIndex];
306
- targetElement === null || targetElement === void 0 ? void 0 : targetElement.focus();
328
+ targetElement === null || targetElement === void 0 ? void 0 : targetElement.focus({ preventScroll });
307
329
  return;
308
330
  }
309
331
  else {
@@ -315,7 +337,7 @@ function focusZone(container, settings) {
315
337
  const elementToFocus = focusInStrategy(event.relatedTarget);
316
338
  const requestedFocusElementIndex = elementToFocus ? focusableElements.indexOf(elementToFocus) : -1;
317
339
  if (requestedFocusElementIndex >= 0 && elementToFocus instanceof HTMLElement) {
318
- elementToFocus.focus();
340
+ elementToFocus.focus({ preventScroll });
319
341
  return;
320
342
  }
321
343
  else {
@@ -400,7 +422,7 @@ function focusZone(container, settings) {
400
422
  }
401
423
  else if (nextElementToFocus) {
402
424
  lastKeyboardFocusDirection = direction;
403
- nextElementToFocus.focus();
425
+ nextElementToFocus.focus({ preventScroll });
404
426
  }
405
427
  if (event.key !== 'Tab' || nextElementToFocus) {
406
428
  event.preventDefault();
@@ -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) {
@@ -25,6 +25,7 @@ export interface FocusZoneSettings {
25
25
  activeDescendantControl?: HTMLElement;
26
26
  onActiveDescendantChanged?: (newActiveDescendant: HTMLElement | undefined, previousActiveDescendant: HTMLElement | undefined, directlyActivated: boolean) => void;
27
27
  focusInStrategy?: 'first' | 'closest' | 'previous' | ((previousFocusedElement: Element) => HTMLElement | undefined);
28
+ preventScroll?: boolean;
28
29
  }
29
30
  export declare const isActiveDescendantAttribute = "data-is-active-descendant";
30
31
  export declare const activeDescendantActivatedDirectly = "activated-directly";
@@ -124,7 +124,7 @@ export const activeDescendantActivatedDirectly = 'activated-directly';
124
124
  export const activeDescendantActivatedIndirectly = 'activated-indirectly';
125
125
  export const hasActiveDescendantAttribute = 'data-has-active-descendant';
126
126
  export function focusZone(container, settings) {
127
- var _a, _b, _c, _d;
127
+ var _a, _b, _c, _d, _e;
128
128
  const focusableElements = [];
129
129
  const savedTabIndex = new WeakMap();
130
130
  const bindKeys = (_a = settings === null || settings === void 0 ? void 0 : settings.bindKeys) !== null && _a !== void 0 ? _a : ((settings === null || settings === void 0 ? void 0 : settings.getNextFocusable) ? FocusKeys.ArrowAll : FocusKeys.ArrowVertical) | FocusKeys.HomeAndEnd;
@@ -133,6 +133,7 @@ export function focusZone(container, settings) {
133
133
  const activeDescendantControl = settings === null || settings === void 0 ? void 0 : settings.activeDescendantControl;
134
134
  const activeDescendantCallback = settings === null || settings === void 0 ? void 0 : settings.onActiveDescendantChanged;
135
135
  let currentFocusedElement;
136
+ const preventScroll = (_d = settings === null || settings === void 0 ? void 0 : settings.preventScroll) !== null && _d !== void 0 ? _d : false;
136
137
  function getFirstFocusableElement() {
137
138
  return focusableElements[0];
138
139
  }
@@ -186,8 +187,7 @@ export function focusZone(container, settings) {
186
187
  if (filteredElements.length === 0) {
187
188
  return;
188
189
  }
189
- const insertIndex = focusableElements.findIndex(e => (e.compareDocumentPosition(filteredElements[0]) & Node.DOCUMENT_POSITION_PRECEDING) > 0);
190
- focusableElements.splice(insertIndex === -1 ? focusableElements.length : insertIndex, 0, ...filteredElements);
190
+ focusableElements.splice(findInsertionIndex(filteredElements), 0, ...filteredElements);
191
191
  for (const element of filteredElements) {
192
192
  if (!savedTabIndex.has(element)) {
193
193
  savedTabIndex.set(element, element.getAttribute('tabindex'));
@@ -198,6 +198,27 @@ export function focusZone(container, settings) {
198
198
  updateFocusedElement(getFirstFocusableElement());
199
199
  }
200
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
+ }
201
222
  function endFocusManagement(...elements) {
202
223
  for (const element of elements) {
203
224
  const focusableElementIndex = focusableElements.indexOf(element);
@@ -221,7 +242,8 @@ export function focusZone(container, settings) {
221
242
  }
222
243
  }
223
244
  beginFocusManagement(...iterateFocusableElements(container));
224
- updateFocusedElement(getFirstFocusableElement());
245
+ const initialElement = typeof focusInStrategy === 'function' ? focusInStrategy(document.body) : getFirstFocusableElement();
246
+ updateFocusedElement(initialElement);
225
247
  const observer = new MutationObserver(mutations => {
226
248
  for (const mutation of mutations) {
227
249
  for (const removedNode of mutation.removedNodes) {
@@ -243,7 +265,7 @@ export function focusZone(container, settings) {
243
265
  childList: true
244
266
  });
245
267
  const controller = new AbortController();
246
- const signal = (_d = settings === null || settings === void 0 ? void 0 : settings.abortSignal) !== null && _d !== void 0 ? _d : controller.signal;
268
+ const signal = (_e = settings === null || settings === void 0 ? void 0 : settings.abortSignal) !== null && _e !== void 0 ? _e : controller.signal;
247
269
  signal.addEventListener('abort', () => {
248
270
  endFocusManagement(...focusableElements);
249
271
  });
@@ -256,7 +278,7 @@ export function focusZone(container, settings) {
256
278
  if (activeDescendantControl) {
257
279
  container.addEventListener('focusin', event => {
258
280
  if (event.target instanceof HTMLElement && focusableElements.includes(event.target)) {
259
- activeDescendantControl.focus();
281
+ activeDescendantControl.focus({ preventScroll });
260
282
  updateFocusedElement(event.target);
261
283
  }
262
284
  });
@@ -300,7 +322,7 @@ export function focusZone(container, settings) {
300
322
  if (event.relatedTarget instanceof Element && !container.contains(event.relatedTarget)) {
301
323
  const targetElementIndex = lastKeyboardFocusDirection === 'previous' ? focusableElements.length - 1 : 0;
302
324
  const targetElement = focusableElements[targetElementIndex];
303
- targetElement === null || targetElement === void 0 ? void 0 : targetElement.focus();
325
+ targetElement === null || targetElement === void 0 ? void 0 : targetElement.focus({ preventScroll });
304
326
  return;
305
327
  }
306
328
  else {
@@ -312,7 +334,7 @@ export function focusZone(container, settings) {
312
334
  const elementToFocus = focusInStrategy(event.relatedTarget);
313
335
  const requestedFocusElementIndex = elementToFocus ? focusableElements.indexOf(elementToFocus) : -1;
314
336
  if (requestedFocusElementIndex >= 0 && elementToFocus instanceof HTMLElement) {
315
- elementToFocus.focus();
337
+ elementToFocus.focus({ preventScroll });
316
338
  return;
317
339
  }
318
340
  else {
@@ -397,7 +419,7 @@ export function focusZone(container, settings) {
397
419
  }
398
420
  else if (nextElementToFocus) {
399
421
  lastKeyboardFocusDirection = direction;
400
- nextElementToFocus.focus();
422
+ nextElementToFocus.focus({ preventScroll });
401
423
  }
402
424
  if (event.key !== 'Tab' || nextElementToFocus) {
403
425
  event.preventDefault();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@primer/behaviors",
3
- "version": "0.0.0-2022927173910",
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
- "release": "npm run build && changeset publish"
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": "^7.0.3",
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.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",
@@ -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": "^7.0.3",
85
+ "size-limit": "^8.2.4",
86
86
  "typescript": "^4.5.2"
87
87
  }
88
88
  }