@primer/components 0.0.0-2021116182158 → 0.0.0-202111619107

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 (63) hide show
  1. package/dist/browser.esm.js +2 -2
  2. package/dist/browser.esm.js.map +1 -1
  3. package/dist/browser.umd.js +2 -2
  4. package/dist/browser.umd.js.map +1 -1
  5. package/lib/ActionList/Item.js +3 -3
  6. package/lib/ActionList/List.js +2 -2
  7. package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +1 -1
  8. package/lib/Autocomplete/AutocompleteMenu.js +10 -5
  9. package/lib/Dialog/ConfirmationDialog.js +2 -2
  10. package/lib/Dialog/Dialog.js +2 -2
  11. package/lib/FilteredActionList/FilteredActionList.js +10 -3
  12. package/lib/Overlay.d.ts +1 -1
  13. package/lib/TextInputWithTokens.js +4 -4
  14. package/lib/hooks/useAnchoredPosition.d.ts +1 -1
  15. package/lib/hooks/useAnchoredPosition.js +2 -2
  16. package/lib/hooks/useFocusTrap.js +2 -2
  17. package/lib/hooks/useFocusZone.d.ts +1 -1
  18. package/lib/hooks/useFocusZone.js +2 -2
  19. package/lib/hooks/useOpenAndCloseFocus.js +2 -2
  20. package/lib-esm/ActionList/Item.js +1 -1
  21. package/lib-esm/ActionList/List.js +1 -1
  22. package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +1 -1
  23. package/lib-esm/Autocomplete/AutocompleteMenu.js +9 -4
  24. package/lib-esm/Dialog/ConfirmationDialog.js +1 -1
  25. package/lib-esm/Dialog/Dialog.js +1 -1
  26. package/lib-esm/FilteredActionList/FilteredActionList.js +9 -3
  27. package/lib-esm/Overlay.d.ts +1 -1
  28. package/lib-esm/TextInputWithTokens.js +2 -2
  29. package/lib-esm/hooks/useAnchoredPosition.d.ts +1 -1
  30. package/lib-esm/hooks/useAnchoredPosition.js +1 -1
  31. package/lib-esm/hooks/useFocusTrap.js +1 -1
  32. package/lib-esm/hooks/useFocusZone.d.ts +1 -1
  33. package/lib-esm/hooks/useFocusZone.js +1 -1
  34. package/lib-esm/hooks/useOpenAndCloseFocus.js +1 -1
  35. package/package.json +2 -5
  36. package/lib/behaviors/anchoredPosition.d.ts +0 -89
  37. package/lib/behaviors/anchoredPosition.js +0 -316
  38. package/lib/behaviors/focusTrap.d.ts +0 -12
  39. package/lib/behaviors/focusTrap.js +0 -179
  40. package/lib/behaviors/focusZone.d.ts +0 -137
  41. package/lib/behaviors/focusZone.js +0 -578
  42. package/lib/behaviors/scrollIntoViewingArea.d.ts +0 -1
  43. package/lib/behaviors/scrollIntoViewingArea.js +0 -39
  44. package/lib/utils/iterateFocusableElements.d.ts +0 -42
  45. package/lib/utils/iterateFocusableElements.js +0 -113
  46. package/lib/utils/uniqueId.d.ts +0 -1
  47. package/lib/utils/uniqueId.js +0 -12
  48. package/lib/utils/userAgent.d.ts +0 -1
  49. package/lib/utils/userAgent.js +0 -15
  50. package/lib-esm/behaviors/anchoredPosition.d.ts +0 -89
  51. package/lib-esm/behaviors/anchoredPosition.js +0 -309
  52. package/lib-esm/behaviors/focusTrap.d.ts +0 -12
  53. package/lib-esm/behaviors/focusTrap.js +0 -170
  54. package/lib-esm/behaviors/focusZone.d.ts +0 -137
  55. package/lib-esm/behaviors/focusZone.js +0 -560
  56. package/lib-esm/behaviors/scrollIntoViewingArea.d.ts +0 -1
  57. package/lib-esm/behaviors/scrollIntoViewingArea.js +0 -30
  58. package/lib-esm/utils/iterateFocusableElements.d.ts +0 -42
  59. package/lib-esm/utils/iterateFocusableElements.js +0 -102
  60. package/lib-esm/utils/uniqueId.d.ts +0 -1
  61. package/lib-esm/utils/uniqueId.js +0 -5
  62. package/lib-esm/utils/userAgent.d.ts +0 -1
  63. package/lib-esm/utils/userAgent.js +0 -8
@@ -1,316 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getAnchoredPosition = getAnchoredPosition;
7
- // When prettier supports template literal types...
8
- // export type AnchorSide = `${'inside' | 'outside'}-${'top' | 'bottom' | 'right' | 'left'}` | 'inside-center'
9
-
10
- /**
11
- * Settings that customize how a floating element is positioned
12
- * with respect to an anchor element.
13
- */
14
- // For each outside anchor position, list the order of alternate positions to try in
15
- // the event that the original position overflows. See comment on `allowOutOfBounds`
16
- // for a more detailed description.
17
- const alternateOrders = {
18
- 'outside-top': ['outside-bottom', 'outside-right', 'outside-left', 'outside-bottom'],
19
- 'outside-bottom': ['outside-top', 'outside-right', 'outside-left', 'outside-bottom'],
20
- 'outside-left': ['outside-right', 'outside-bottom', 'outside-top', 'outside-bottom'],
21
- 'outside-right': ['outside-left', 'outside-bottom', 'outside-top', 'outside-bottom']
22
- };
23
-
24
- /**
25
- * Given a floating element and an anchor element, return coordinates for the top-left
26
- * of the floating element in order to absolutely position it such that it appears
27
- * near the anchor element.
28
- *
29
- * @param floatingElement Element intended to be positioned near or within an anchor
30
- * @param anchorElement The element to serve as the position anchor
31
- * @param settings Settings to determine the rules for positioning the floating element
32
- * @returns {top: number, left: number} coordinates for the floating element
33
- */
34
- function getAnchoredPosition(floatingElement, anchorElement, settings = {}) {
35
- const parentElement = getPositionedParent(floatingElement);
36
- const clippingRect = getClippingRect(parentElement);
37
- const parentElementStyle = getComputedStyle(parentElement);
38
- const parentElementRect = parentElement.getBoundingClientRect();
39
- const [borderTop, borderLeft] = [parentElementStyle.borderTopWidth, parentElementStyle.borderLeftWidth].map(v => parseInt(v, 10) || 0);
40
- const relativeRect = {
41
- top: parentElementRect.top + borderTop,
42
- left: parentElementRect.left + borderLeft
43
- };
44
- return pureCalculateAnchoredPosition(clippingRect, relativeRect, floatingElement.getBoundingClientRect(), anchorElement instanceof Element ? anchorElement.getBoundingClientRect() : anchorElement, getDefaultSettings(settings));
45
- }
46
- /**
47
- * Returns the nearest proper HTMLElement parent of `element` whose
48
- * position is not "static", or document.body, whichever is closer
49
- */
50
-
51
-
52
- function getPositionedParent(element) {
53
- let parentNode = element.parentNode;
54
-
55
- while (parentNode !== null) {
56
- if (parentNode instanceof HTMLElement && getComputedStyle(parentNode).position !== 'static') {
57
- return parentNode;
58
- }
59
-
60
- parentNode = parentNode.parentNode;
61
- }
62
-
63
- return document.body;
64
- }
65
- /**
66
- * Returns the rectangle (relative to the window) that will clip the given element
67
- * if it is rendered outside of its bounds.
68
- * @param element
69
- * @returns
70
- */
71
-
72
-
73
- function getClippingRect(element) {
74
- let parentNode = element;
75
-
76
- while (parentNode !== null) {
77
- if (parentNode === document.body) {
78
- break;
79
- }
80
-
81
- const parentNodeStyle = getComputedStyle(parentNode);
82
-
83
- if (parentNodeStyle.overflow !== 'visible') {
84
- break;
85
- }
86
-
87
- parentNode = parentNode.parentNode;
88
- }
89
-
90
- const clippingNode = parentNode === document.body || !(parentNode instanceof HTMLElement) ? document.body : parentNode;
91
- const elemRect = clippingNode.getBoundingClientRect();
92
- const elemStyle = getComputedStyle(clippingNode);
93
- const [borderTop, borderLeft, borderRight, borderBottom] = [elemStyle.borderTopWidth, elemStyle.borderLeftWidth, elemStyle.borderRightWidth, elemStyle.borderBottomWidth].map(v => parseInt(v, 10) || 0);
94
- return {
95
- top: elemRect.top + borderTop,
96
- left: elemRect.left + borderLeft,
97
- width: elemRect.width - borderRight - borderLeft,
98
- // If the clipping node is document.body, it can expand to the full height of the window
99
- height: Math.max(elemRect.height - borderTop - borderBottom, clippingNode === document.body ? window.innerHeight : -Infinity)
100
- };
101
- } // Default settings to position a floating element
102
-
103
-
104
- const positionDefaults = {
105
- side: 'outside-bottom',
106
- align: 'start',
107
- // note: the following default is not applied if side === "inside-center"
108
- anchorOffset: 4,
109
- // note: the following default is only applied if side starts with "inside"
110
- // and align is not center
111
- alignmentOffset: 4,
112
- allowOutOfBounds: false
113
- };
114
- /**
115
- * Compute a full PositionSettings object from the given partial PositionSettings object
116
- * by filling in with defaults where applicable.
117
- * @param settings Partial settings - any omissions will be defaulted
118
- */
119
-
120
- function getDefaultSettings(settings = {}) {
121
- var _settings$side, _settings$align, _settings$anchorOffse, _settings$alignmentOf, _settings$allowOutOfB;
122
-
123
- const side = (_settings$side = settings.side) !== null && _settings$side !== void 0 ? _settings$side : positionDefaults.side;
124
- const align = (_settings$align = settings.align) !== null && _settings$align !== void 0 ? _settings$align : positionDefaults.align;
125
- return {
126
- side,
127
- align,
128
- // offsets always default to 0 if their respective side/alignment is centered
129
- anchorOffset: (_settings$anchorOffse = settings.anchorOffset) !== null && _settings$anchorOffse !== void 0 ? _settings$anchorOffse : side === 'inside-center' ? 0 : positionDefaults.anchorOffset,
130
- alignmentOffset: (_settings$alignmentOf = settings.alignmentOffset) !== null && _settings$alignmentOf !== void 0 ? _settings$alignmentOf : align !== 'center' && side.startsWith('inside') ? positionDefaults.alignmentOffset : 0,
131
- allowOutOfBounds: (_settings$allowOutOfB = settings.allowOutOfBounds) !== null && _settings$allowOutOfB !== void 0 ? _settings$allowOutOfB : positionDefaults.allowOutOfBounds
132
- };
133
- }
134
- /**
135
- * Note: This is a pure function with no dependency on DOM APIs.
136
- * @see getAnchoredPosition
137
- * @see getDefaultSettings
138
- * @param viewportRect BoxPosition for the rectangle that will clip the floating element if it is
139
- * rendered outside of the boundsof the rectangle.
140
- * @param relativePosition Position for the closest positioned proper parent of the floating element
141
- * @param floatingRect WidthAndHeight for the floating element
142
- * @param anchorRect BoxPosition for the anchor element
143
- * @param PositionSettings to customize the calculated position for the floating element.
144
- */
145
-
146
-
147
- function pureCalculateAnchoredPosition(viewportRect, relativePosition, floatingRect, anchorRect, {
148
- side,
149
- align,
150
- allowOutOfBounds,
151
- anchorOffset,
152
- alignmentOffset
153
- }) {
154
- // Compute the relative viewport rect, to bring it into the same coordinate space as `pos`
155
- const relativeViewportRect = {
156
- top: viewportRect.top - relativePosition.top,
157
- left: viewportRect.left - relativePosition.left,
158
- width: viewportRect.width,
159
- height: viewportRect.height
160
- };
161
- let pos = calculatePosition(floatingRect, anchorRect, side, align, anchorOffset, alignmentOffset);
162
- let anchorSide = side;
163
- pos.top -= relativePosition.top;
164
- pos.left -= relativePosition.left; // Handle screen overflow
165
-
166
- if (!allowOutOfBounds) {
167
- const alternateOrder = alternateOrders[side];
168
- let positionAttempt = 0;
169
-
170
- if (alternateOrder) {
171
- let prevSide = side; // Try all the alternate sides until one does not overflow
172
-
173
- while (positionAttempt < alternateOrder.length && shouldRecalculatePosition(prevSide, pos, relativeViewportRect, floatingRect)) {
174
- const nextSide = alternateOrder[positionAttempt++];
175
- prevSide = nextSide; // If we have cut off in the same dimension as the "side" option, try flipping to the opposite side.
176
-
177
- pos = calculatePosition(floatingRect, anchorRect, nextSide, align, anchorOffset, alignmentOffset);
178
- pos.top -= relativePosition.top;
179
- pos.left -= relativePosition.left;
180
- anchorSide = nextSide;
181
- }
182
- } // At this point we've flipped the position if applicable. Now just nudge until it's on-screen.
183
-
184
-
185
- if (pos.top < relativeViewportRect.top) {
186
- pos.top = relativeViewportRect.top;
187
- }
188
-
189
- if (pos.left < relativeViewportRect.left) {
190
- pos.left = relativeViewportRect.left;
191
- }
192
-
193
- if (pos.left + floatingRect.width > viewportRect.width + relativeViewportRect.left) {
194
- pos.left = viewportRect.width + relativeViewportRect.left - floatingRect.width;
195
- } // If we have exhausted all possible positions and none of them worked, we
196
- // say that overflowing the bottom of the screen is acceptable since it is
197
- // likely to be able to scroll.
198
-
199
-
200
- if (alternateOrder && positionAttempt < alternateOrder.length) {
201
- if (pos.top + floatingRect.height > viewportRect.height + relativeViewportRect.top) {
202
- pos.top = viewportRect.height + relativeViewportRect.top - floatingRect.height;
203
- }
204
- }
205
- }
206
-
207
- return { ...pos,
208
- anchorSide
209
- };
210
- }
211
- /**
212
- * Given a floating element and an anchor element, return coordinates for the
213
- * top-left of the floating element in order to absolutely position it such
214
- * that it appears near the anchor element.
215
- *
216
- * @param elementDimensions Dimensions of the floating element
217
- * @param anchorPosition Position of the anchor element
218
- * @param side Side of the anchor to position the floating element
219
- * @param align How to align the floating element with the anchor element
220
- * @param anchorOffset Absolute pixel offset for anchor positioning
221
- * @param alignmentOffset Absolute pixel offset for alignment
222
- * @returns {top: number, left: number} coordinates for the floating element
223
- */
224
-
225
-
226
- function calculatePosition(elementDimensions, anchorPosition, side, align, anchorOffset, alignmentOffset) {
227
- const anchorRight = anchorPosition.left + anchorPosition.width;
228
- const anchorBottom = anchorPosition.top + anchorPosition.height;
229
- let top = -1;
230
- let left = -1;
231
-
232
- if (side === 'outside-top') {
233
- top = anchorPosition.top - anchorOffset - elementDimensions.height;
234
- } else if (side === 'outside-bottom') {
235
- top = anchorBottom + anchorOffset;
236
- } else if (side === 'outside-left') {
237
- left = anchorPosition.left - anchorOffset - elementDimensions.width;
238
- } else if (side === 'outside-right') {
239
- left = anchorRight + anchorOffset;
240
- }
241
-
242
- if (side === 'outside-top' || side === 'outside-bottom') {
243
- if (align === 'start') {
244
- left = anchorPosition.left + alignmentOffset;
245
- } else if (align === 'center') {
246
- left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
247
- } else {
248
- // end
249
- left = anchorRight - elementDimensions.width - alignmentOffset;
250
- }
251
- }
252
-
253
- if (side === 'outside-left' || side === 'outside-right') {
254
- if (align === 'start') {
255
- top = anchorPosition.top + alignmentOffset;
256
- } else if (align === 'center') {
257
- top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
258
- } else {
259
- // end
260
- top = anchorBottom - elementDimensions.height - alignmentOffset;
261
- }
262
- }
263
-
264
- if (side === 'inside-top') {
265
- top = anchorPosition.top + anchorOffset;
266
- } else if (side === 'inside-bottom') {
267
- top = anchorBottom - anchorOffset - elementDimensions.height;
268
- } else if (side === 'inside-left') {
269
- left = anchorPosition.left + anchorOffset;
270
- } else if (side === 'inside-right') {
271
- left = anchorRight - anchorOffset - elementDimensions.width;
272
- } else if (side === 'inside-center') {
273
- left = (anchorRight + anchorPosition.left) / 2 - elementDimensions.width / 2 + anchorOffset;
274
- }
275
-
276
- if (side === 'inside-top' || side === 'inside-bottom') {
277
- if (align === 'start') {
278
- left = anchorPosition.left + alignmentOffset;
279
- } else if (align === 'center') {
280
- left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
281
- } else {
282
- // end
283
- left = anchorRight - elementDimensions.width - alignmentOffset;
284
- }
285
- } else if (side === 'inside-left' || side === 'inside-right' || side === 'inside-center') {
286
- if (align === 'start') {
287
- top = anchorPosition.top + alignmentOffset;
288
- } else if (align === 'center') {
289
- top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
290
- } else {
291
- // end
292
- top = anchorBottom - elementDimensions.height - alignmentOffset;
293
- }
294
- }
295
-
296
- return {
297
- top,
298
- left
299
- };
300
- }
301
- /**
302
- * Determines if there is an overflow
303
- * @param side
304
- * @param currentPos
305
- * @param containerDimensions
306
- * @param elementDimensions
307
- */
308
-
309
-
310
- function shouldRecalculatePosition(side, currentPos, containerDimensions, elementDimensions) {
311
- if (side === 'outside-top' || side === 'outside-bottom') {
312
- return currentPos.top < containerDimensions.top || currentPos.top + elementDimensions.height > containerDimensions.height + containerDimensions.top;
313
- } else {
314
- return currentPos.left < containerDimensions.left || currentPos.left + elementDimensions.width > containerDimensions.width + containerDimensions.left;
315
- }
316
- }
@@ -1,12 +0,0 @@
1
- /**
2
- * Traps focus within the given container.
3
- * @param container The container in which to trap focus
4
- * @returns AbortController - call `.abort()` to disable the focus trap
5
- */
6
- export declare function focusTrap(container: HTMLElement, initialFocus?: HTMLElement): AbortController;
7
- /**
8
- * Traps focus within the given container.
9
- * @param container The container in which to trap focus
10
- * @param abortSignal An AbortSignal to control the focus trap.
11
- */
12
- export declare function focusTrap(container: HTMLElement, initialFocus: HTMLElement | undefined, abortSignal: AbortSignal): void;
@@ -1,179 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.focusTrap = focusTrap;
7
-
8
- var _iterateFocusableElements = require("../utils/iterateFocusableElements");
9
-
10
- var _eventListenerSignal = require("../polyfills/eventListenerSignal");
11
-
12
- (0, _eventListenerSignal.polyfill)();
13
- const suspendedTrapStack = [];
14
- let activeTrap = undefined;
15
-
16
- function tryReactivate() {
17
- const trapToReactivate = suspendedTrapStack.pop();
18
-
19
- if (trapToReactivate) {
20
- focusTrap(trapToReactivate.container, trapToReactivate.initialFocus, trapToReactivate.originalSignal);
21
- }
22
- } // @todo If AbortController.prototype.follow is ever implemented, that
23
- // could replace this function. @see https://github.com/whatwg/dom/issues/920
24
-
25
-
26
- function followSignal(signal) {
27
- const controller = new AbortController();
28
- signal.addEventListener('abort', () => {
29
- controller.abort();
30
- });
31
- return controller;
32
- }
33
- /**
34
- * Returns the first focusable child of `container`. If `lastChild` is true,
35
- * returns the last focusable child of `container`.
36
- * @param container
37
- * @param lastChild
38
- */
39
-
40
-
41
- function getFocusableChild(container, lastChild = false) {
42
- return (0, _iterateFocusableElements.iterateFocusableElements)(container, {
43
- reverse: lastChild,
44
- strict: true,
45
- onlyTabbable: true
46
- }).next().value;
47
- }
48
- /**
49
- * Traps focus within the given container.
50
- * @param container The container in which to trap focus
51
- * @returns AbortController - call `.abort()` to disable the focus trap
52
- */
53
-
54
-
55
- function focusTrap(container, initialFocus, abortSignal) {
56
- // Set up an abort controller if a signal was not passed in
57
- const controller = new AbortController();
58
- const signal = abortSignal !== null && abortSignal !== void 0 ? abortSignal : controller.signal;
59
- container.setAttribute('data-focus-trap', 'active');
60
- let lastFocusedChild = undefined; // Ensure focus remains in the trap zone by checking that a given recently-focused
61
- // element is inside the trap zone. If it isn't, redirect focus to a suitable
62
- // element within the trap zone. If need to redirect focus and a suitable element
63
- // is not found, focus the container.
64
-
65
- function ensureTrapZoneHasFocus(focusedElement) {
66
- if (focusedElement instanceof HTMLElement && document.contains(container)) {
67
- if (container.contains(focusedElement)) {
68
- // If a child of the trap zone was focused, remember it
69
- lastFocusedChild = focusedElement;
70
- return;
71
- } else {
72
- if (lastFocusedChild && (0, _iterateFocusableElements.isTabbable)(lastFocusedChild) && container.contains(lastFocusedChild)) {
73
- lastFocusedChild.focus();
74
- return;
75
- } else if (initialFocus && container.contains(initialFocus)) {
76
- initialFocus.focus();
77
- return;
78
- } else {
79
- // Ensure the container is focusable:
80
- // - Either the container already has a `tabIndex`
81
- // - Or provide a temporary `tabIndex`
82
- const containerNeedsTemporaryTabIndex = container.getAttribute('tabindex') === null;
83
-
84
- if (containerNeedsTemporaryTabIndex) {
85
- container.setAttribute('tabindex', '-1');
86
- } // Focus the container.
87
-
88
-
89
- container.focus(); // If a temporary `tabIndex` was provided, remove it.
90
-
91
- if (containerNeedsTemporaryTabIndex) {
92
- // Once focus has moved from the container to a child within the FocusTrap,
93
- // the container can be made un-refocusable by removing `tabIndex`.
94
- container.addEventListener('blur', () => container.removeAttribute('tabindex'), {
95
- once: true
96
- }); // NB: If `tabIndex` was removed *before* `blur`, then certain browsers (e.g. Chrome)
97
- // would consider `body` the `activeElement`, and as a result, keyboard navigation
98
- // between children would break, since `body` is outside the `FocusTrap`.
99
- }
100
-
101
- return;
102
- }
103
- }
104
- }
105
- }
106
-
107
- const wrappingController = followSignal(signal);
108
- container.addEventListener('keydown', event => {
109
- if (event.key !== 'Tab' || event.defaultPrevented) {
110
- return;
111
- }
112
-
113
- const {
114
- target
115
- } = event;
116
- const firstFocusableChild = getFocusableChild(container);
117
- const lastFocusableChild = getFocusableChild(container, true);
118
-
119
- if (target === firstFocusableChild && event.shiftKey) {
120
- event.preventDefault();
121
- lastFocusableChild === null || lastFocusableChild === void 0 ? void 0 : lastFocusableChild.focus();
122
- } else if (target === lastFocusableChild && !event.shiftKey) {
123
- event.preventDefault();
124
- firstFocusableChild === null || firstFocusableChild === void 0 ? void 0 : firstFocusableChild.focus();
125
- }
126
- }, {
127
- signal: wrappingController.signal
128
- });
129
-
130
- if (activeTrap) {
131
- const suspendedTrap = activeTrap;
132
- activeTrap.container.setAttribute('data-focus-trap', 'suspended');
133
- activeTrap.controller.abort();
134
- suspendedTrapStack.push(suspendedTrap);
135
- } // When this trap is canceled, either by the user or by us for suspension
136
-
137
-
138
- wrappingController.signal.addEventListener('abort', () => {
139
- activeTrap = undefined;
140
- }); // Only when user-canceled
141
-
142
- signal.addEventListener('abort', () => {
143
- container.removeAttribute('data-focus-trap');
144
- const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
145
-
146
- if (suspendedTrapIndex >= 0) {
147
- suspendedTrapStack.splice(suspendedTrapIndex, 1);
148
- }
149
-
150
- tryReactivate();
151
- }); // Prevent focus leaving the trap container
152
-
153
- document.addEventListener('focus', event => {
154
- ensureTrapZoneHasFocus(event.target);
155
- }, // use capture to ensure we get all events. focus events do not bubble
156
- {
157
- signal: wrappingController.signal,
158
- capture: true
159
- }); // focus the first element
160
-
161
- ensureTrapZoneHasFocus(document.activeElement);
162
- activeTrap = {
163
- container,
164
- controller: wrappingController,
165
- initialFocus,
166
- originalSignal: signal
167
- }; // If we are activating a focus trap for a container that was previously
168
- // suspended, just remove it from the suspended list.
169
-
170
- const suspendedTrapIndex = suspendedTrapStack.findIndex(t => t.container === container);
171
-
172
- if (suspendedTrapIndex >= 0) {
173
- suspendedTrapStack.splice(suspendedTrapIndex, 1);
174
- }
175
-
176
- if (!abortSignal) {
177
- return controller;
178
- }
179
- }
@@ -1,137 +0,0 @@
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';
3
- export declare enum FocusKeys {
4
- ArrowHorizontal = 1,
5
- ArrowVertical = 2,
6
- JK = 4,
7
- HL = 8,
8
- HomeAndEnd = 16,
9
- PageUpDown = 256,
10
- WS = 32,
11
- AD = 64,
12
- Tab = 128,
13
- ArrowAll = 3,
14
- HJKL = 12,
15
- WASD = 96,
16
- All = 511
17
- }
18
- /**
19
- * Options that control the behavior of the arrow focus behavior.
20
- */
21
- export interface FocusZoneSettings {
22
- /**
23
- * Choose the behavior applied in cases where focus is currently at either the first or
24
- * last element of the container.
25
- *
26
- * "stop" - do nothing and keep focus where it was
27
- * "wrap" - wrap focus around to the first element from the last, or the last element from the first
28
- *
29
- * Default: "stop"
30
- */
31
- focusOutBehavior?: 'stop' | 'wrap';
32
- /**
33
- * If set, this will be called to get the next focusable element. If this function
34
- * returns null, we will try to determine the next direction ourselves. Use the
35
- * `bindKeys` option to customize which keys are listened to.
36
- *
37
- * The function can accept a Direction, indicating the direction focus should move,
38
- * the HTMLElement that was previously focused, and lastly the `KeyboardEvent` object
39
- * created by the original `"keydown"` event.
40
- */
41
- getNextFocusable?: (direction: Direction, from: Element | undefined, event: KeyboardEvent) => HTMLElement | undefined;
42
- /**
43
- * Called to decide if a focusable element is allowed to participate in the arrow
44
- * key focus behavior.
45
- *
46
- * By default, all focusable elements within the given container will participate
47
- * in the arrow key focus behavior. If you need to withhold some elements from
48
- * participation, implement this callback to return false for those elements.
49
- */
50
- focusableElementFilter?: (element: HTMLElement) => boolean;
51
- /**
52
- * Bit flags that identify keys that will be bound to. Each available key either
53
- * moves focus to the "next" element or the "previous" element, so it is best
54
- * to only bind the keys that make sense to move focus in your UI. Use the `FocusKeys`
55
- * object to discover supported keys.
56
- *
57
- * Use the bitwise "OR" operator (`|`) to combine key types. For example,
58
- * `FocusKeys.WASD | FocusKeys.HJKL` represents all of W, A, S, D, H, J, K, and L.
59
- *
60
- * A note on FocusKeys.PageUpDown: This behavior does not support paging, so by default
61
- * using these keys will result in the same behavior as Home and End. To override this
62
- * behavior, implement `getNextFocusable`.
63
- *
64
- * The default for this setting is `FocusKeys.ArrowVertical | FocusKeys.HomeAndEnd`, unless
65
- * `getNextFocusable` is provided, in which case `FocusKeys.ArrowAll | FocusKeys.HomeAndEnd`
66
- * is used as the default.
67
- */
68
- bindKeys?: FocusKeys;
69
- /**
70
- * If provided, this signal can be used to disable the behavior and remove any
71
- * event listeners.
72
- */
73
- abortSignal?: AbortSignal;
74
- /**
75
- * If `activeDescendantControl` is supplied, do not move focus or alter `tabindex` on
76
- * any element. Instead, manage `aria-activedescendant` according to the ARIA best
77
- * practices guidelines.
78
- * @see https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_focus_activedescendant
79
- *
80
- * The given `activeDescendantControl` will be given an `aria-controls` attribute that
81
- * references the ID of the `container`. Additionally, it will be given an
82
- * `aria-activedescendant` attribute that references the ID of the currently-active
83
- * descendant.
84
- *
85
- * This element will retain DOM focus as arrow keys are pressed.
86
- */
87
- activeDescendantControl?: HTMLElement;
88
- /**
89
- * Called each time the active descendant changes. Note that either of the parameters
90
- * may be undefined, e.g. when an element in the container first becomes active, or
91
- * when the controlling element becomes unfocused.
92
- */
93
- onActiveDescendantChanged?: (newActiveDescendant: HTMLElement | undefined, previousActiveDescendant: HTMLElement | undefined, directlyActivated: boolean) => void;
94
- /**
95
- * This option allows customization of the behavior that determines which of the
96
- * focusable elements should be focused when focus enters the container via the Tab key.
97
- *
98
- * When set to "first", whenever focus enters the container via Tab, we will focus the
99
- * first focusable element. When set to "previous", the most recently focused element
100
- * will be focused (fallback to first if there was no previous).
101
- *
102
- * The "closest" strategy works like "first", except either the first or the last element
103
- * of the container will be focused, depending on the direction from which focus comes.
104
- *
105
- * If a function is provided, this function should return the HTMLElement intended
106
- * to receive focus. This is useful if you want to focus the currently "selected"
107
- * item or element.
108
- *
109
- * Default: "previous"
110
- *
111
- * For more information, @see https://www.w3.org/TR/wai-aria-practices-1.1/#kbd_general_within
112
- */
113
- focusInStrategy?: 'first' | 'closest' | 'previous' | ((previousFocusedElement: Element) => HTMLElement | undefined);
114
- }
115
- export declare const isActiveDescendantAttribute = "data-is-active-descendant";
116
- /**
117
- * A value of activated-directly for data-is-active-descendant indicates the descendant was activated
118
- * by a manual user interaction with intent to move active descendant. This usually translates to the
119
- * user pressing one of the bound keys (up/down arrow, etc) to move through the focus zone. This is
120
- * intended to be roughly equivalent to the :focus-visible pseudo-class
121
- **/
122
- export declare const activeDescendantActivatedDirectly = "activated-directly";
123
- /**
124
- * A value of activated-indirectly for data-is-active-descendant indicates the descendant was activated
125
- * implicitly, and not by a direct key press. This includes focus zone being created from scratch, focusable
126
- * elements being added/removed, and mouseover events. This is intended to be roughly equivalent
127
- * to :focus:not(:focus-visible)
128
- **/
129
- export declare const activeDescendantActivatedIndirectly = "activated-indirectly";
130
- export declare const hasActiveDescendantAttribute = "data-has-active-descendant";
131
- /**
132
- * Sets up the arrow key focus behavior for all focusable elements in the given `container`.
133
- * @param container
134
- * @param settings
135
- * @returns
136
- */
137
- export declare function focusZone(container: HTMLElement, settings?: FocusZoneSettings): AbortController;