@primer/behaviors 1.0.2-rc.b89e9b4 → 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.
Files changed (44) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/{anchored-position.d.ts → cjs/anchored-position.d.ts} +0 -0
  3. package/dist/cjs/anchored-position.js +210 -0
  4. package/dist/cjs/focus-trap.d.ts +1 -0
  5. package/dist/cjs/focus-trap.js +107 -0
  6. package/dist/{focus-zone.d.ts → cjs/focus-zone.d.ts} +0 -0
  7. package/dist/cjs/focus-zone.js +410 -0
  8. package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  9. package/dist/cjs/index.js +16 -0
  10. package/dist/{polyfills → cjs/polyfills}/event-listener-signal.d.ts +0 -0
  11. package/dist/cjs/polyfills/event-listener-signal.js +44 -0
  12. package/dist/{scroll-into-view.d.ts → cjs/scroll-into-view.d.ts} +0 -0
  13. package/dist/cjs/scroll-into-view.js +21 -0
  14. package/dist/{utils → cjs/utils}/index.d.ts +0 -0
  15. package/dist/cjs/utils/index.js +15 -0
  16. package/dist/{utils → cjs/utils}/iterate-focusable-elements.d.ts +1 -0
  17. package/dist/cjs/utils/iterate-focusable-elements.js +68 -0
  18. package/dist/{utils → cjs/utils}/unique-id.d.ts +0 -0
  19. package/dist/cjs/utils/unique-id.js +8 -0
  20. package/dist/{utils → cjs/utils}/user-agent.d.ts +0 -0
  21. package/dist/cjs/utils/user-agent.js +11 -0
  22. package/dist/esm/anchored-position.d.ts +15 -0
  23. package/dist/{anchored-position.js → esm/anchored-position.js} +0 -0
  24. package/dist/esm/focus-trap.d.ts +1 -0
  25. package/dist/{focus-trap.js → esm/focus-trap.js} +24 -28
  26. package/dist/esm/focus-zone.d.ts +32 -0
  27. package/dist/{focus-zone.js → esm/focus-zone.js} +0 -0
  28. package/dist/{index.js → esm/index.d.ts} +0 -0
  29. package/dist/esm/index.js +4 -0
  30. package/dist/esm/polyfills/event-listener-signal.d.ts +6 -0
  31. package/dist/{polyfills → esm/polyfills}/event-listener-signal.js +0 -0
  32. package/dist/esm/scroll-into-view.d.ts +7 -0
  33. package/dist/{scroll-into-view.js → esm/scroll-into-view.js} +0 -0
  34. package/dist/{utils/index.js → esm/utils/index.d.ts} +0 -0
  35. package/dist/esm/utils/index.js +3 -0
  36. package/dist/esm/utils/iterate-focusable-elements.d.ts +9 -0
  37. package/dist/{utils → esm/utils}/iterate-focusable-elements.js +5 -1
  38. package/dist/esm/utils/unique-id.d.ts +1 -0
  39. package/dist/{utils → esm/utils}/unique-id.js +0 -0
  40. package/dist/esm/utils/user-agent.d.ts +1 -0
  41. package/dist/{utils → esm/utils}/user-agent.js +0 -0
  42. package/package.json +24 -8
  43. package/utils/package.json +2 -2
  44. package/dist/focus-trap.d.ts +0 -2
@@ -0,0 +1,410 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.focusZone = exports.hasActiveDescendantAttribute = exports.activeDescendantActivatedIndirectly = exports.activeDescendantActivatedDirectly = exports.isActiveDescendantAttribute = exports.FocusKeys = void 0;
4
+ const event_listener_signal_js_1 = require("./polyfills/event-listener-signal.js");
5
+ const user_agent_js_1 = require("./utils/user-agent.js");
6
+ const iterate_focusable_elements_js_1 = require("./utils/iterate-focusable-elements.js");
7
+ const unique_id_js_1 = require("./utils/unique-id.js");
8
+ (0, event_listener_signal_js_1.polyfill)();
9
+ var FocusKeys;
10
+ (function (FocusKeys) {
11
+ FocusKeys[FocusKeys["ArrowHorizontal"] = 1] = "ArrowHorizontal";
12
+ FocusKeys[FocusKeys["ArrowVertical"] = 2] = "ArrowVertical";
13
+ FocusKeys[FocusKeys["JK"] = 4] = "JK";
14
+ FocusKeys[FocusKeys["HL"] = 8] = "HL";
15
+ FocusKeys[FocusKeys["HomeAndEnd"] = 16] = "HomeAndEnd";
16
+ FocusKeys[FocusKeys["PageUpDown"] = 256] = "PageUpDown";
17
+ FocusKeys[FocusKeys["WS"] = 32] = "WS";
18
+ FocusKeys[FocusKeys["AD"] = 64] = "AD";
19
+ FocusKeys[FocusKeys["Tab"] = 128] = "Tab";
20
+ FocusKeys[FocusKeys["ArrowAll"] = 3] = "ArrowAll";
21
+ FocusKeys[FocusKeys["HJKL"] = 12] = "HJKL";
22
+ FocusKeys[FocusKeys["WASD"] = 96] = "WASD";
23
+ FocusKeys[FocusKeys["All"] = 511] = "All";
24
+ })(FocusKeys = exports.FocusKeys || (exports.FocusKeys = {}));
25
+ const KEY_TO_BIT = {
26
+ ArrowLeft: FocusKeys.ArrowHorizontal,
27
+ ArrowDown: FocusKeys.ArrowVertical,
28
+ ArrowUp: FocusKeys.ArrowVertical,
29
+ ArrowRight: FocusKeys.ArrowHorizontal,
30
+ h: FocusKeys.HL,
31
+ j: FocusKeys.JK,
32
+ k: FocusKeys.JK,
33
+ l: FocusKeys.HL,
34
+ a: FocusKeys.AD,
35
+ s: FocusKeys.WS,
36
+ w: FocusKeys.WS,
37
+ d: FocusKeys.AD,
38
+ Tab: FocusKeys.Tab,
39
+ Home: FocusKeys.HomeAndEnd,
40
+ End: FocusKeys.HomeAndEnd,
41
+ PageUp: FocusKeys.PageUpDown,
42
+ PageDown: FocusKeys.PageUpDown
43
+ };
44
+ const KEY_TO_DIRECTION = {
45
+ ArrowLeft: 'previous',
46
+ ArrowDown: 'next',
47
+ ArrowUp: 'previous',
48
+ ArrowRight: 'next',
49
+ h: 'previous',
50
+ j: 'next',
51
+ k: 'previous',
52
+ l: 'next',
53
+ a: 'previous',
54
+ s: 'next',
55
+ w: 'previous',
56
+ d: 'next',
57
+ Tab: 'next',
58
+ Home: 'start',
59
+ End: 'end',
60
+ PageUp: 'start',
61
+ PageDown: 'end'
62
+ };
63
+ function getDirection(keyboardEvent) {
64
+ const direction = KEY_TO_DIRECTION[keyboardEvent.key];
65
+ if (keyboardEvent.key === 'Tab' && keyboardEvent.shiftKey) {
66
+ return 'previous';
67
+ }
68
+ const isMac = (0, user_agent_js_1.isMacOS)();
69
+ if ((isMac && keyboardEvent.metaKey) || (!isMac && keyboardEvent.ctrlKey)) {
70
+ if (keyboardEvent.key === 'ArrowLeft' || keyboardEvent.key === 'ArrowUp') {
71
+ return 'start';
72
+ }
73
+ else if (keyboardEvent.key === 'ArrowRight' || keyboardEvent.key === 'ArrowDown') {
74
+ return 'end';
75
+ }
76
+ }
77
+ return direction;
78
+ }
79
+ function shouldIgnoreFocusHandling(keyboardEvent, activeElement) {
80
+ const key = keyboardEvent.key;
81
+ const keyLength = [...key].length;
82
+ const isTextInput = (activeElement instanceof HTMLInputElement && activeElement.type === 'text') ||
83
+ activeElement instanceof HTMLTextAreaElement;
84
+ if (isTextInput && (keyLength === 1 || key === 'Home' || key === 'End')) {
85
+ return true;
86
+ }
87
+ if (activeElement instanceof HTMLSelectElement) {
88
+ if (keyLength === 1) {
89
+ return true;
90
+ }
91
+ if (key === 'ArrowDown' && (0, user_agent_js_1.isMacOS)() && !keyboardEvent.metaKey) {
92
+ return true;
93
+ }
94
+ if (key === 'ArrowDown' && !(0, user_agent_js_1.isMacOS)() && keyboardEvent.altKey) {
95
+ return true;
96
+ }
97
+ }
98
+ if (activeElement instanceof HTMLTextAreaElement && (key === 'PageUp' || key === 'PageDown')) {
99
+ return true;
100
+ }
101
+ if (isTextInput) {
102
+ const textInput = activeElement;
103
+ const cursorAtStart = textInput.selectionStart === 0 && textInput.selectionEnd === 0;
104
+ const cursorAtEnd = textInput.selectionStart === textInput.value.length && textInput.selectionEnd === textInput.value.length;
105
+ if (key === 'ArrowLeft' && !cursorAtStart) {
106
+ return true;
107
+ }
108
+ if (key === 'ArrowRight' && !cursorAtEnd) {
109
+ return true;
110
+ }
111
+ if (textInput instanceof HTMLTextAreaElement) {
112
+ if (key === 'ArrowUp' && !cursorAtStart) {
113
+ return true;
114
+ }
115
+ if (key === 'ArrowDown' && !cursorAtEnd) {
116
+ return true;
117
+ }
118
+ }
119
+ }
120
+ return false;
121
+ }
122
+ exports.isActiveDescendantAttribute = 'data-is-active-descendant';
123
+ exports.activeDescendantActivatedDirectly = 'activated-directly';
124
+ exports.activeDescendantActivatedIndirectly = 'activated-indirectly';
125
+ exports.hasActiveDescendantAttribute = 'data-has-active-descendant';
126
+ function focusZone(container, settings) {
127
+ var _a, _b, _c, _d;
128
+ const focusableElements = [];
129
+ const savedTabIndex = new WeakMap();
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;
131
+ const focusOutBehavior = (_b = settings === null || settings === void 0 ? void 0 : settings.focusOutBehavior) !== null && _b !== void 0 ? _b : 'stop';
132
+ const focusInStrategy = (_c = settings === null || settings === void 0 ? void 0 : settings.focusInStrategy) !== null && _c !== void 0 ? _c : 'previous';
133
+ const activeDescendantControl = settings === null || settings === void 0 ? void 0 : settings.activeDescendantControl;
134
+ const activeDescendantCallback = settings === null || settings === void 0 ? void 0 : settings.onActiveDescendantChanged;
135
+ let currentFocusedElement;
136
+ function getFirstFocusableElement() {
137
+ return focusableElements[0];
138
+ }
139
+ function isActiveDescendantInputFocused() {
140
+ return document.activeElement === activeDescendantControl;
141
+ }
142
+ function updateFocusedElement(to, directlyActivated = false) {
143
+ const from = currentFocusedElement;
144
+ currentFocusedElement = to;
145
+ if (activeDescendantControl) {
146
+ if (to && isActiveDescendantInputFocused()) {
147
+ setActiveDescendant(from, to, directlyActivated);
148
+ }
149
+ else {
150
+ clearActiveDescendant();
151
+ }
152
+ return;
153
+ }
154
+ if (from && from !== to && savedTabIndex.has(from)) {
155
+ from.setAttribute('tabindex', '-1');
156
+ }
157
+ to === null || to === void 0 ? void 0 : to.setAttribute('tabindex', '0');
158
+ }
159
+ function setActiveDescendant(from, to, directlyActivated = false) {
160
+ if (!to.id) {
161
+ to.setAttribute('id', (0, unique_id_js_1.uniqueId)());
162
+ }
163
+ if (from && from !== to) {
164
+ from.removeAttribute(exports.isActiveDescendantAttribute);
165
+ }
166
+ if (!activeDescendantControl ||
167
+ (!directlyActivated && activeDescendantControl.getAttribute('aria-activedescendant') === to.id)) {
168
+ return;
169
+ }
170
+ activeDescendantControl.setAttribute('aria-activedescendant', to.id);
171
+ container.setAttribute(exports.hasActiveDescendantAttribute, to.id);
172
+ to.setAttribute(exports.isActiveDescendantAttribute, directlyActivated ? exports.activeDescendantActivatedDirectly : exports.activeDescendantActivatedIndirectly);
173
+ activeDescendantCallback === null || activeDescendantCallback === void 0 ? void 0 : activeDescendantCallback(to, from, directlyActivated);
174
+ }
175
+ function clearActiveDescendant(previouslyActiveElement = currentFocusedElement) {
176
+ if (focusInStrategy === 'first') {
177
+ currentFocusedElement = undefined;
178
+ }
179
+ activeDescendantControl === null || activeDescendantControl === void 0 ? void 0 : activeDescendantControl.removeAttribute('aria-activedescendant');
180
+ container.removeAttribute(exports.hasActiveDescendantAttribute);
181
+ previouslyActiveElement === null || previouslyActiveElement === void 0 ? void 0 : previouslyActiveElement.removeAttribute(exports.isActiveDescendantAttribute);
182
+ activeDescendantCallback === null || activeDescendantCallback === void 0 ? void 0 : activeDescendantCallback(undefined, previouslyActiveElement, false);
183
+ }
184
+ function beginFocusManagement(...elements) {
185
+ const filteredElements = elements.filter(e => { var _a, _b; return (_b = (_a = settings === null || settings === void 0 ? void 0 : settings.focusableElementFilter) === null || _a === void 0 ? void 0 : _a.call(settings, e)) !== null && _b !== void 0 ? _b : true; });
186
+ if (filteredElements.length === 0) {
187
+ return;
188
+ }
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);
191
+ for (const element of filteredElements) {
192
+ if (!savedTabIndex.has(element)) {
193
+ savedTabIndex.set(element, element.getAttribute('tabindex'));
194
+ }
195
+ element.setAttribute('tabindex', '-1');
196
+ }
197
+ if (!currentFocusedElement) {
198
+ updateFocusedElement(getFirstFocusableElement());
199
+ }
200
+ }
201
+ function endFocusManagement(...elements) {
202
+ for (const element of elements) {
203
+ const focusableElementIndex = focusableElements.indexOf(element);
204
+ if (focusableElementIndex >= 0) {
205
+ focusableElements.splice(focusableElementIndex, 1);
206
+ }
207
+ const savedIndex = savedTabIndex.get(element);
208
+ if (savedIndex !== undefined) {
209
+ if (savedIndex === null) {
210
+ element.removeAttribute('tabindex');
211
+ }
212
+ else {
213
+ element.setAttribute('tabindex', savedIndex);
214
+ }
215
+ savedTabIndex.delete(element);
216
+ }
217
+ if (element === currentFocusedElement) {
218
+ const nextElementToFocus = getFirstFocusableElement();
219
+ updateFocusedElement(nextElementToFocus);
220
+ }
221
+ }
222
+ }
223
+ beginFocusManagement(...(0, iterate_focusable_elements_js_1.iterateFocusableElements)(container));
224
+ updateFocusedElement(getFirstFocusableElement());
225
+ const observer = new MutationObserver(mutations => {
226
+ for (const mutation of mutations) {
227
+ for (const removedNode of mutation.removedNodes) {
228
+ if (removedNode instanceof HTMLElement) {
229
+ endFocusManagement(...(0, iterate_focusable_elements_js_1.iterateFocusableElements)(removedNode));
230
+ }
231
+ }
232
+ }
233
+ for (const mutation of mutations) {
234
+ for (const addedNode of mutation.addedNodes) {
235
+ if (addedNode instanceof HTMLElement) {
236
+ beginFocusManagement(...(0, iterate_focusable_elements_js_1.iterateFocusableElements)(addedNode));
237
+ }
238
+ }
239
+ }
240
+ });
241
+ observer.observe(container, {
242
+ subtree: true,
243
+ childList: true
244
+ });
245
+ const controller = new AbortController();
246
+ const signal = (_d = settings === null || settings === void 0 ? void 0 : settings.abortSignal) !== null && _d !== void 0 ? _d : controller.signal;
247
+ signal.addEventListener('abort', () => {
248
+ endFocusManagement(...focusableElements);
249
+ });
250
+ let elementIndexFocusedByClick = undefined;
251
+ container.addEventListener('mousedown', event => {
252
+ if (event.target instanceof HTMLElement && event.target !== document.activeElement) {
253
+ elementIndexFocusedByClick = focusableElements.indexOf(event.target);
254
+ }
255
+ }, { signal });
256
+ if (activeDescendantControl) {
257
+ container.addEventListener('focusin', event => {
258
+ if (event.target instanceof HTMLElement && focusableElements.includes(event.target)) {
259
+ activeDescendantControl.focus();
260
+ updateFocusedElement(event.target);
261
+ }
262
+ });
263
+ container.addEventListener('mousemove', ({ target }) => {
264
+ if (!(target instanceof Node)) {
265
+ return;
266
+ }
267
+ const focusableElement = focusableElements.find(element => element.contains(target));
268
+ if (focusableElement) {
269
+ updateFocusedElement(focusableElement);
270
+ }
271
+ }, { signal, capture: true });
272
+ activeDescendantControl.addEventListener('focusin', () => {
273
+ if (!currentFocusedElement) {
274
+ updateFocusedElement(getFirstFocusableElement());
275
+ }
276
+ else {
277
+ setActiveDescendant(undefined, currentFocusedElement);
278
+ }
279
+ });
280
+ activeDescendantControl.addEventListener('focusout', () => {
281
+ clearActiveDescendant();
282
+ });
283
+ }
284
+ else {
285
+ container.addEventListener('focusin', event => {
286
+ if (event.target instanceof HTMLElement) {
287
+ if (elementIndexFocusedByClick !== undefined) {
288
+ if (elementIndexFocusedByClick >= 0) {
289
+ if (focusableElements[elementIndexFocusedByClick] !== currentFocusedElement) {
290
+ updateFocusedElement(focusableElements[elementIndexFocusedByClick]);
291
+ }
292
+ }
293
+ elementIndexFocusedByClick = undefined;
294
+ }
295
+ else {
296
+ if (focusInStrategy === 'previous') {
297
+ updateFocusedElement(event.target);
298
+ }
299
+ else if (focusInStrategy === 'closest' || focusInStrategy === 'first') {
300
+ if (event.relatedTarget instanceof Element && !container.contains(event.relatedTarget)) {
301
+ const targetElementIndex = lastKeyboardFocusDirection === 'previous' ? focusableElements.length - 1 : 0;
302
+ const targetElement = focusableElements[targetElementIndex];
303
+ targetElement === null || targetElement === void 0 ? void 0 : targetElement.focus();
304
+ return;
305
+ }
306
+ else {
307
+ updateFocusedElement(event.target);
308
+ }
309
+ }
310
+ else if (typeof focusInStrategy === 'function') {
311
+ if (event.relatedTarget instanceof Element && !container.contains(event.relatedTarget)) {
312
+ const elementToFocus = focusInStrategy(event.relatedTarget);
313
+ const requestedFocusElementIndex = elementToFocus ? focusableElements.indexOf(elementToFocus) : -1;
314
+ if (requestedFocusElementIndex >= 0 && elementToFocus instanceof HTMLElement) {
315
+ elementToFocus.focus();
316
+ return;
317
+ }
318
+ else {
319
+ console.warn('Element requested is not a known focusable element.');
320
+ }
321
+ }
322
+ else {
323
+ updateFocusedElement(event.target);
324
+ }
325
+ }
326
+ }
327
+ }
328
+ lastKeyboardFocusDirection = undefined;
329
+ }, { signal });
330
+ }
331
+ const keyboardEventRecipient = activeDescendantControl !== null && activeDescendantControl !== void 0 ? activeDescendantControl : container;
332
+ let lastKeyboardFocusDirection = undefined;
333
+ if (focusInStrategy === 'closest') {
334
+ document.addEventListener('keydown', event => {
335
+ if (event.key === 'Tab') {
336
+ lastKeyboardFocusDirection = getDirection(event);
337
+ }
338
+ }, { signal, capture: true });
339
+ }
340
+ function getCurrentFocusedIndex() {
341
+ if (!currentFocusedElement) {
342
+ return 0;
343
+ }
344
+ const focusedIndex = focusableElements.indexOf(currentFocusedElement);
345
+ const fallbackIndex = currentFocusedElement === container ? -1 : 0;
346
+ return focusedIndex !== -1 ? focusedIndex : fallbackIndex;
347
+ }
348
+ keyboardEventRecipient.addEventListener('keydown', event => {
349
+ var _a;
350
+ if (event.key in KEY_TO_DIRECTION) {
351
+ const keyBit = KEY_TO_BIT[event.key];
352
+ if (!event.defaultPrevented &&
353
+ (keyBit & bindKeys) > 0 &&
354
+ !shouldIgnoreFocusHandling(event, document.activeElement)) {
355
+ const direction = getDirection(event);
356
+ let nextElementToFocus = undefined;
357
+ if (settings === null || settings === void 0 ? void 0 : settings.getNextFocusable) {
358
+ nextElementToFocus = settings.getNextFocusable(direction, (_a = document.activeElement) !== null && _a !== void 0 ? _a : undefined, event);
359
+ }
360
+ if (!nextElementToFocus) {
361
+ const lastFocusedIndex = getCurrentFocusedIndex();
362
+ let nextFocusedIndex = lastFocusedIndex;
363
+ if (direction === 'previous') {
364
+ nextFocusedIndex -= 1;
365
+ }
366
+ else if (direction === 'start') {
367
+ nextFocusedIndex = 0;
368
+ }
369
+ else if (direction === 'next') {
370
+ nextFocusedIndex += 1;
371
+ }
372
+ else {
373
+ nextFocusedIndex = focusableElements.length - 1;
374
+ }
375
+ if (nextFocusedIndex < 0) {
376
+ if (focusOutBehavior === 'wrap' && event.key !== 'Tab') {
377
+ nextFocusedIndex = focusableElements.length - 1;
378
+ }
379
+ else {
380
+ nextFocusedIndex = 0;
381
+ }
382
+ }
383
+ if (nextFocusedIndex >= focusableElements.length) {
384
+ if (focusOutBehavior === 'wrap' && event.key !== 'Tab') {
385
+ nextFocusedIndex = 0;
386
+ }
387
+ else {
388
+ nextFocusedIndex = focusableElements.length - 1;
389
+ }
390
+ }
391
+ if (lastFocusedIndex !== nextFocusedIndex) {
392
+ nextElementToFocus = focusableElements[nextFocusedIndex];
393
+ }
394
+ }
395
+ if (activeDescendantControl) {
396
+ updateFocusedElement(nextElementToFocus || currentFocusedElement, true);
397
+ }
398
+ else if (nextElementToFocus) {
399
+ lastKeyboardFocusDirection = direction;
400
+ nextElementToFocus.focus();
401
+ }
402
+ if (event.key !== 'Tab' || nextElementToFocus) {
403
+ event.preventDefault();
404
+ }
405
+ }
406
+ }
407
+ }, { signal });
408
+ return controller;
409
+ }
410
+ exports.focusZone = focusZone;
File without changes
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./anchored-position.js"), exports);
14
+ __exportStar(require("./focus-trap.js"), exports);
15
+ __exportStar(require("./focus-zone.js"), exports);
16
+ __exportStar(require("./scroll-into-view.js"), exports);
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.polyfill = void 0;
4
+ let signalSupported = false;
5
+ function noop() { }
6
+ try {
7
+ const options = Object.create({}, {
8
+ signal: {
9
+ get() {
10
+ signalSupported = true;
11
+ }
12
+ }
13
+ });
14
+ window.addEventListener('test', noop, options);
15
+ window.removeEventListener('test', noop, options);
16
+ }
17
+ catch (e) {
18
+ }
19
+ function featureSupported() {
20
+ return signalSupported;
21
+ }
22
+ function monkeyPatch() {
23
+ if (typeof window === 'undefined') {
24
+ return;
25
+ }
26
+ const originalAddEventListener = EventTarget.prototype.addEventListener;
27
+ EventTarget.prototype.addEventListener = function (name, originalCallback, optionsOrCapture) {
28
+ if (typeof optionsOrCapture === 'object' &&
29
+ 'signal' in optionsOrCapture &&
30
+ optionsOrCapture.signal instanceof AbortSignal) {
31
+ originalAddEventListener.call(optionsOrCapture.signal, 'abort', () => {
32
+ this.removeEventListener(name, originalCallback, optionsOrCapture);
33
+ });
34
+ }
35
+ return originalAddEventListener.call(this, name, originalCallback, optionsOrCapture);
36
+ };
37
+ }
38
+ function polyfill() {
39
+ if (!featureSupported()) {
40
+ monkeyPatch();
41
+ signalSupported = true;
42
+ }
43
+ }
44
+ exports.polyfill = polyfill;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.scrollIntoView = void 0;
4
+ function scrollIntoView(child, viewingArea, { direction = 'vertical', startMargin = 0, endMargin = 0, behavior = 'smooth' } = {}) {
5
+ const startSide = direction === 'vertical' ? 'top' : 'left';
6
+ const endSide = direction === 'vertical' ? 'bottom' : 'right';
7
+ const scrollSide = direction === 'vertical' ? 'scrollTop' : 'scrollLeft';
8
+ const { [startSide]: childStart, [endSide]: childEnd } = child.getBoundingClientRect();
9
+ const { [startSide]: viewingAreaStart, [endSide]: viewingAreaEnd } = viewingArea.getBoundingClientRect();
10
+ const isChildStartAboveViewingArea = childStart < viewingAreaStart + startMargin;
11
+ const isChildBottomBelowViewingArea = childEnd > viewingAreaEnd - endMargin;
12
+ if (isChildStartAboveViewingArea) {
13
+ const scrollHeightToChildStart = childStart - viewingAreaStart + viewingArea[scrollSide];
14
+ viewingArea.scrollTo({ behavior, [startSide]: scrollHeightToChildStart - startMargin });
15
+ }
16
+ else if (isChildBottomBelowViewingArea) {
17
+ const scrollHeightToChildBottom = childEnd - viewingAreaEnd + viewingArea[scrollSide];
18
+ viewingArea.scrollTo({ behavior, [startSide]: scrollHeightToChildBottom + endMargin });
19
+ }
20
+ }
21
+ exports.scrollIntoView = scrollIntoView;
File without changes
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./iterate-focusable-elements.js"), exports);
14
+ __exportStar(require("./unique-id.js"), exports);
15
+ __exportStar(require("./user-agent.js"), exports);
@@ -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;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isTabbable = exports.isFocusable = exports.getFocusableChild = exports.iterateFocusableElements = void 0;
4
+ function* iterateFocusableElements(container, options = {}) {
5
+ var _a, _b;
6
+ const strict = (_a = options.strict) !== null && _a !== void 0 ? _a : false;
7
+ const acceptFn = ((_b = options.onlyTabbable) !== null && _b !== void 0 ? _b : false) ? isTabbable : isFocusable;
8
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
9
+ acceptNode: node => node instanceof HTMLElement && acceptFn(node, strict) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP
10
+ });
11
+ let nextNode = null;
12
+ if (!options.reverse && acceptFn(container, strict)) {
13
+ yield container;
14
+ }
15
+ if (options.reverse) {
16
+ let lastChild = walker.lastChild();
17
+ while (lastChild) {
18
+ nextNode = lastChild;
19
+ lastChild = walker.lastChild();
20
+ }
21
+ }
22
+ else {
23
+ nextNode = walker.firstChild();
24
+ }
25
+ while (nextNode instanceof HTMLElement) {
26
+ yield nextNode;
27
+ nextNode = options.reverse ? walker.previousNode() : walker.nextNode();
28
+ }
29
+ if (options.reverse && acceptFn(container, strict)) {
30
+ yield container;
31
+ }
32
+ return undefined;
33
+ }
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;
39
+ function isFocusable(elem, strict = false) {
40
+ const disabledAttrInert = ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'OPTGROUP', 'OPTION', 'FIELDSET'].includes(elem.tagName) &&
41
+ elem.disabled;
42
+ const hiddenInert = elem.hidden;
43
+ const hiddenInputInert = elem instanceof HTMLInputElement && elem.type === 'hidden';
44
+ const sentinelInert = elem.classList.contains('sentinel');
45
+ if (disabledAttrInert || hiddenInert || hiddenInputInert || sentinelInert) {
46
+ return false;
47
+ }
48
+ if (strict) {
49
+ const sizeInert = elem.offsetWidth === 0 || elem.offsetHeight === 0;
50
+ const visibilityInert = ['hidden', 'collapse'].includes(getComputedStyle(elem).visibility);
51
+ const clientRectsInert = elem.getClientRects().length === 0;
52
+ if (sizeInert || visibilityInert || clientRectsInert) {
53
+ return false;
54
+ }
55
+ }
56
+ if (elem.getAttribute('tabindex') != null) {
57
+ return true;
58
+ }
59
+ if (elem instanceof HTMLAnchorElement && elem.getAttribute('href') == null) {
60
+ return false;
61
+ }
62
+ return elem.tabIndex !== -1;
63
+ }
64
+ exports.isFocusable = isFocusable;
65
+ function isTabbable(elem, strict = false) {
66
+ return isFocusable(elem, strict) && elem.getAttribute('tabindex') !== '-1';
67
+ }
68
+ exports.isTabbable = isTabbable;
File without changes
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uniqueId = void 0;
4
+ let idSeed = 10000;
5
+ function uniqueId() {
6
+ return `__primer_id_${idSeed++}`;
7
+ }
8
+ exports.uniqueId = uniqueId;
File without changes
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isMacOS = void 0;
4
+ let isMac = undefined;
5
+ function isMacOS() {
6
+ if (isMac === undefined) {
7
+ isMac = /^mac/i.test(window.navigator.platform);
8
+ }
9
+ return isMac;
10
+ }
11
+ exports.isMacOS = isMacOS;
@@ -0,0 +1,15 @@
1
+ export declare type AnchorAlignment = 'start' | 'center' | 'end';
2
+ export declare type AnchorSide = 'inside-top' | 'inside-bottom' | 'inside-left' | 'inside-right' | 'inside-center' | 'outside-top' | 'outside-bottom' | 'outside-left' | 'outside-right';
3
+ export interface PositionSettings {
4
+ side: AnchorSide;
5
+ align: AnchorAlignment;
6
+ anchorOffset: number;
7
+ alignmentOffset: number;
8
+ allowOutOfBounds: boolean;
9
+ }
10
+ export interface AnchorPosition {
11
+ top: number;
12
+ left: number;
13
+ anchorSide: AnchorSide;
14
+ }
15
+ export declare function getAnchoredPosition(floatingElement: Element, anchorElement: Element | DOMRect, settings?: Partial<PositionSettings>): AnchorPosition;
@@ -0,0 +1 @@
1
+ export declare function focusTrap(container: HTMLElement, initialFocus?: HTMLElement, abortSignal?: AbortSignal): AbortController | undefined;