@primer/behaviors 0.0.0-2021113221730 → 0.0.0-2022017101021

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 (73) hide show
  1. package/lib/__tests__/anchored-position.test.d.ts +1 -0
  2. package/lib/__tests__/anchored-position.test.js +388 -0
  3. package/lib/__tests__/focus-trap.test.d.ts +1 -0
  4. package/lib/__tests__/focus-trap.test.js +234 -0
  5. package/lib/__tests__/focus-zone.test.d.ts +1 -0
  6. package/lib/__tests__/focus-zone.test.js +570 -0
  7. package/lib/__tests__/iterate-focusable-elements.test.d.ts +1 -0
  8. package/lib/__tests__/iterate-focusable-elements.test.js +55 -0
  9. package/lib/__tests__/scroll-into-view.test.d.ts +1 -0
  10. package/lib/__tests__/scroll-into-view.test.js +245 -0
  11. package/lib/anchored-position.d.ts +89 -0
  12. package/lib/anchored-position.js +316 -0
  13. package/lib/focus-trap.d.ts +12 -0
  14. package/lib/focus-trap.js +179 -0
  15. package/lib/focus-zone.d.ts +137 -0
  16. package/lib/focus-zone.js +578 -0
  17. package/{dist/index.js → lib/index.d.ts} +0 -0
  18. package/lib/index.js +57 -0
  19. package/lib/polyfills/event-listener-signal.d.ts +6 -0
  20. package/lib/polyfills/event-listener-signal.js +64 -0
  21. package/lib/scroll-into-view.d.ts +7 -0
  22. package/lib/scroll-into-view.js +42 -0
  23. package/{dist/utils/index.js → lib/utils/index.d.ts} +0 -0
  24. package/lib/utils/index.js +44 -0
  25. package/lib/utils/iterate-focusable-elements.d.ts +42 -0
  26. package/lib/utils/iterate-focusable-elements.js +113 -0
  27. package/lib/utils/unique-id.d.ts +1 -0
  28. package/lib/utils/unique-id.js +12 -0
  29. package/lib/utils/user-agent.d.ts +1 -0
  30. package/lib/utils/user-agent.js +15 -0
  31. package/lib-esm/__tests__/anchored-position.test.d.ts +1 -0
  32. package/lib-esm/__tests__/anchored-position.test.js +386 -0
  33. package/lib-esm/__tests__/focus-trap.test.d.ts +1 -0
  34. package/lib-esm/__tests__/focus-trap.test.js +227 -0
  35. package/lib-esm/__tests__/focus-zone.test.d.ts +1 -0
  36. package/lib-esm/__tests__/focus-zone.test.js +487 -0
  37. package/lib-esm/__tests__/iterate-focusable-elements.test.d.ts +1 -0
  38. package/lib-esm/__tests__/iterate-focusable-elements.test.js +48 -0
  39. package/lib-esm/__tests__/scroll-into-view.test.d.ts +1 -0
  40. package/lib-esm/__tests__/scroll-into-view.test.js +243 -0
  41. package/lib-esm/anchored-position.d.ts +89 -0
  42. package/lib-esm/anchored-position.js +309 -0
  43. package/lib-esm/focus-trap.d.ts +12 -0
  44. package/lib-esm/focus-trap.js +170 -0
  45. package/lib-esm/focus-zone.d.ts +137 -0
  46. package/lib-esm/focus-zone.js +559 -0
  47. package/{dist → lib-esm}/index.d.ts +0 -0
  48. package/lib-esm/index.js +4 -0
  49. package/{dist → lib-esm}/polyfills/event-listener-signal.d.ts +0 -0
  50. package/lib-esm/polyfills/event-listener-signal.js +57 -0
  51. package/{dist → lib-esm}/scroll-into-view.d.ts +0 -0
  52. package/lib-esm/scroll-into-view.js +35 -0
  53. package/{dist → lib-esm}/utils/index.d.ts +0 -0
  54. package/lib-esm/utils/index.js +3 -0
  55. package/lib-esm/utils/iterate-focusable-elements.d.ts +42 -0
  56. package/lib-esm/utils/iterate-focusable-elements.js +102 -0
  57. package/{dist → lib-esm}/utils/unique-id.d.ts +0 -0
  58. package/lib-esm/utils/unique-id.js +5 -0
  59. package/{dist → lib-esm}/utils/user-agent.d.ts +0 -0
  60. package/lib-esm/utils/user-agent.js +8 -0
  61. package/package.json +37 -5
  62. package/dist/anchored-position.d.ts +0 -15
  63. package/dist/anchored-position.js +0 -206
  64. package/dist/focus-trap.d.ts +0 -2
  65. package/dist/focus-trap.js +0 -107
  66. package/dist/focus-zone.d.ts +0 -32
  67. package/dist/focus-zone.js +0 -406
  68. package/dist/polyfills/event-listener-signal.js +0 -40
  69. package/dist/scroll-into-view.js +0 -17
  70. package/dist/utils/iterate-focusable-elements.d.ts +0 -8
  71. package/dist/utils/iterate-focusable-elements.js +0 -57
  72. package/dist/utils/unique-id.js +0 -4
  73. package/dist/utils/user-agent.js +0 -7
@@ -0,0 +1,243 @@
1
+ import { scrollIntoView } from '../scroll-into-view.js';
2
+
3
+ function scrollPositionFormula(positionData, isChildAboveViewingArea) {
4
+ const {
5
+ viewingAreaEdgePosition,
6
+ childEdgePosition,
7
+ margin
8
+ } = positionData;
9
+ const marginOffset = margin * (isChildAboveViewingArea ? -1 : 1);
10
+ return childEdgePosition - viewingAreaEdgePosition + marginOffset;
11
+ } // The DOMRect constructor isn't available in JSDOM, so we improvise here.
12
+
13
+
14
+ function makeDOMRect(x, y, width, height) {
15
+ return {
16
+ x,
17
+ y,
18
+ width,
19
+ height,
20
+ top: y,
21
+ left: x,
22
+ right: x + width,
23
+ bottom: y + height,
24
+
25
+ toJSON() {
26
+ return this;
27
+ }
28
+
29
+ };
30
+ } // Since Jest/JSDOM doesn't support layout, we can stub out getBoundingClientRect if we know the
31
+ // correct dimensions. JSDOM will handle the rest of the DOM API used by getAnchoredPosition.
32
+
33
+
34
+ function createVirtualDOM(viewingAreaRect, childRect) {
35
+ const viewingArea = document.createElement('div');
36
+ viewingArea.style.overflow = 'auto';
37
+ viewingArea.id = 'viewingArea';
38
+ viewingArea.innerHTML = '<div id="child"></div>';
39
+ const child = viewingArea.querySelector('#child');
40
+
41
+ child.getBoundingClientRect = () => childRect;
42
+
43
+ viewingArea.getBoundingClientRect = () => viewingAreaRect;
44
+
45
+ return {
46
+ viewingArea,
47
+ child
48
+ };
49
+ }
50
+
51
+ describe('scrollIntoView', () => {
52
+ it('scrolls the expected amount when only the viewingArea element and child element are passed to the function', () => {
53
+ const scrollToMock = jest.fn();
54
+ Object.defineProperty(window.Element.prototype, 'scrollTo', {
55
+ writable: true,
56
+ value: scrollToMock
57
+ });
58
+ const childHeight = 50;
59
+ const viewAreaHeight = 100;
60
+ const childStart = viewAreaHeight + 10;
61
+ const expectedScrollPosition = scrollPositionFormula({
62
+ viewingAreaEdgePosition: viewAreaHeight,
63
+ childEdgePosition: childStart + childHeight,
64
+ margin: 0
65
+ }, false);
66
+ const viewingAreaRect = makeDOMRect(0, 0, 100, viewAreaHeight);
67
+ const childRect = makeDOMRect(0, childStart, 100, childHeight);
68
+ const {
69
+ viewingArea,
70
+ child
71
+ } = createVirtualDOM(viewingAreaRect, childRect);
72
+
73
+ viewingArea.getBoundingClientRect = () => viewingAreaRect;
74
+
75
+ viewingArea.scrollTop = 0;
76
+
77
+ child.getBoundingClientRect = () => childRect;
78
+
79
+ scrollIntoView(child, viewingArea);
80
+ expect(scrollToMock).toHaveBeenCalledWith({
81
+ behavior: 'smooth',
82
+ top: expectedScrollPosition
83
+ });
84
+ });
85
+ describe('y-axis', () => {
86
+ it('scrolls the child into the viewing area when it is AFTER the overflow cutoff point', () => {
87
+ const scrollToMock = jest.fn();
88
+ Object.defineProperty(window.Element.prototype, 'scrollTo', {
89
+ writable: true,
90
+ value: scrollToMock
91
+ });
92
+ const childHeight = 50;
93
+ const viewAreaHeight = 100;
94
+ const childStart = viewAreaHeight + 10;
95
+ const scrollMargin = 10;
96
+ const expectedScrollPosition = scrollPositionFormula({
97
+ viewingAreaEdgePosition: viewAreaHeight,
98
+ childEdgePosition: childStart + childHeight,
99
+ margin: scrollMargin
100
+ }, false);
101
+ const viewingAreaRect = makeDOMRect(0, 0, 100, viewAreaHeight);
102
+ const childRect = makeDOMRect(0, childStart, 100, childHeight);
103
+ const {
104
+ viewingArea,
105
+ child
106
+ } = createVirtualDOM(viewingAreaRect, childRect);
107
+
108
+ viewingArea.getBoundingClientRect = () => viewingAreaRect;
109
+
110
+ viewingArea.scrollTop = 0;
111
+
112
+ child.getBoundingClientRect = () => childRect;
113
+
114
+ scrollIntoView(child, viewingArea, {
115
+ direction: 'vertical',
116
+ startMargin: scrollMargin,
117
+ endMargin: scrollMargin,
118
+ behavior: 'auto'
119
+ });
120
+ expect(scrollToMock).toHaveBeenCalledWith({
121
+ behavior: 'auto',
122
+ top: expectedScrollPosition
123
+ });
124
+ });
125
+ it('scrolls the child into the viewing area when it is BEFORE the overflow cutoff point', () => {
126
+ const scrollToMock = jest.fn();
127
+ Object.defineProperty(window.Element.prototype, 'scrollTo', {
128
+ writable: true,
129
+ value: scrollToMock
130
+ });
131
+ const childHeight = 50;
132
+ const childStart = childHeight * -1 - 10;
133
+ const scrollMargin = 10;
134
+ const expectedScrollPosition = scrollPositionFormula({
135
+ viewingAreaEdgePosition: 0,
136
+ childEdgePosition: childStart,
137
+ margin: scrollMargin
138
+ }, true);
139
+ const viewingAreaRect = makeDOMRect(0, 0, 100, 100);
140
+ const childRect = makeDOMRect(0, childStart, 100, childHeight);
141
+ const {
142
+ viewingArea,
143
+ child
144
+ } = createVirtualDOM(viewingAreaRect, childRect);
145
+
146
+ viewingArea.getBoundingClientRect = () => viewingAreaRect;
147
+
148
+ viewingArea.scrollTop = 0;
149
+
150
+ child.getBoundingClientRect = () => childRect;
151
+
152
+ scrollIntoView(child, viewingArea, {
153
+ direction: 'vertical',
154
+ startMargin: scrollMargin,
155
+ endMargin: scrollMargin,
156
+ behavior: 'auto'
157
+ });
158
+ expect(scrollToMock).toHaveBeenCalledWith({
159
+ behavior: 'auto',
160
+ top: expectedScrollPosition
161
+ });
162
+ });
163
+ });
164
+ describe('x-axis', () => {
165
+ it('scrolls the child into the viewing area when it is AFTER the overflow cutoff point', () => {
166
+ const scrollToMock = jest.fn();
167
+ Object.defineProperty(window.Element.prototype, 'scrollTo', {
168
+ writable: true,
169
+ value: scrollToMock
170
+ });
171
+ const childWidth = 50;
172
+ const viewAreaWidth = 100;
173
+ const childStart = viewAreaWidth + 10;
174
+ const scrollMargin = 10;
175
+ const expectedScrollPosition = scrollPositionFormula({
176
+ viewingAreaEdgePosition: viewAreaWidth,
177
+ childEdgePosition: childStart + childWidth,
178
+ margin: scrollMargin
179
+ }, false);
180
+ const viewingAreaRect = makeDOMRect(0, 0, 100, viewAreaWidth);
181
+ const childRect = makeDOMRect(childStart, 0, childWidth, 100);
182
+ const {
183
+ viewingArea,
184
+ child
185
+ } = createVirtualDOM(viewingAreaRect, childRect);
186
+
187
+ viewingArea.getBoundingClientRect = () => viewingAreaRect;
188
+
189
+ viewingArea.scrollLeft = 0;
190
+
191
+ child.getBoundingClientRect = () => childRect;
192
+
193
+ scrollIntoView(child, viewingArea, {
194
+ direction: 'horizontal',
195
+ startMargin: scrollMargin,
196
+ endMargin: scrollMargin,
197
+ behavior: 'auto'
198
+ });
199
+ expect(scrollToMock).toHaveBeenCalledWith({
200
+ behavior: 'auto',
201
+ left: expectedScrollPosition
202
+ });
203
+ });
204
+ it('scrolls the child into the viewing area when it is BEFORE the overflow cutoff point', () => {
205
+ const scrollToMock = jest.fn();
206
+ Object.defineProperty(window.Element.prototype, 'scrollTo', {
207
+ writable: true,
208
+ value: scrollToMock
209
+ });
210
+ const childWidth = 50;
211
+ const childStart = childWidth * -1 - 10;
212
+ const scrollMargin = 10;
213
+ const expectedScrollPosition = scrollPositionFormula({
214
+ viewingAreaEdgePosition: 0,
215
+ childEdgePosition: childStart,
216
+ margin: scrollMargin
217
+ }, true);
218
+ const viewingAreaRect = makeDOMRect(0, 0, 100, 100);
219
+ const childRect = makeDOMRect(childStart, 0, childWidth, 100);
220
+ const {
221
+ viewingArea,
222
+ child
223
+ } = createVirtualDOM(viewingAreaRect, childRect);
224
+
225
+ viewingArea.getBoundingClientRect = () => viewingAreaRect;
226
+
227
+ viewingArea.scrollTop = 0;
228
+
229
+ child.getBoundingClientRect = () => childRect;
230
+
231
+ scrollIntoView(child, viewingArea, {
232
+ direction: 'horizontal',
233
+ startMargin: scrollMargin,
234
+ endMargin: scrollMargin,
235
+ behavior: 'auto'
236
+ });
237
+ expect(scrollToMock).toHaveBeenCalledWith({
238
+ behavior: 'auto',
239
+ left: expectedScrollPosition
240
+ });
241
+ });
242
+ });
243
+ });
@@ -0,0 +1,89 @@
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
+ /**
4
+ * Settings that customize how a floating element is positioned
5
+ * with respect to an anchor element.
6
+ */
7
+ export interface PositionSettings {
8
+ /**
9
+ * Sets the side of the anchor element that the floating element should be
10
+ * pinned to. This side is given by a string starting with either "inside" or
11
+ * "outside", followed by a hyphen, followed by either "top", "right", "bottom",
12
+ * or "left". Additionally, "inside-center" is an allowed value.
13
+ *
14
+ * The first part of this string, "inside" or "outside", determines whether the
15
+ * floating element should be attempted to be placed "inside" the anchor element
16
+ * or "outside" of it. Using "inside" is useful for making it appear that the
17
+ * anchor _contains_ the floating element, and it can be used for implementing a
18
+ * dialog that is centered on the screen. The "outside" value is more common and
19
+ * can be used for tooltips, popovers, menus, etc.
20
+ *
21
+ * The second part of this string determines the _edge_ on the anchor element that
22
+ * the floating element will be anchored to. If side is "inside-center", then
23
+ * the floating element will be centered in the X-direction (while align is used
24
+ * to position it in the Y-direction).
25
+ * Note: "outside-center" is _not_ a valid value for this property.
26
+ */
27
+ side: AnchorSide;
28
+ /**
29
+ * Determines how the floating element should align with the anchor element. If
30
+ * set to "start", the floating element's first edge (top or left) will align
31
+ * with the anchor element's first edge. If set to "center", the floating
32
+ * element will be centered along the axis of the anchor edge. If set to "end",
33
+ * the floating element's last edge will align with the anchor element's last edge.
34
+ */
35
+ align: AnchorAlignment;
36
+ /**
37
+ * The number of pixels between the anchor edge and the floating element.
38
+ *
39
+ * Positive values move the floating element farther from the anchor element
40
+ * (for outside positioning) or further inside the anchor element (for inside
41
+ * positioning). Negative values have the opposite effect.
42
+ */
43
+ anchorOffset: number;
44
+ /**
45
+ * An additional offset, in pixels, to move the floating element from
46
+ * the aligning edge.
47
+ *
48
+ * Positive values move the floating element in the direction of center-
49
+ * alignment. Negative values move the floating element away from center-
50
+ * alignment. When align is "center", positive offsets move the floating
51
+ * element right (top or bottom anchor side) or down (left or right
52
+ * anchor side).
53
+ */
54
+ alignmentOffset: number;
55
+ /**
56
+ * If false, when the above settings result in rendering the floating element
57
+ * wholly or partially outside of the bounds of the containing element, attempt
58
+ * to adjust the settings to prevent this. Only applies to "outside" positioning.
59
+ *
60
+ * First, attempt to flip to the opposite edge of the anchor if the floating
61
+ * element is getting clipped in that direction. If flipping results in a
62
+ * similar clipping, try moving to the adjacent sides.
63
+ *
64
+ * Once we find a side that does not clip the overlay in its own dimension,
65
+ * check the rest of the sides to see if we need to adjust the alignment offset
66
+ * to fit in other dimensions.
67
+ *
68
+ * If we try all four sides and get clipped each time, settle for overflowing
69
+ * and use the "bottom" side, since the ability to scroll is most likely in
70
+ * this direction.
71
+ */
72
+ allowOutOfBounds: boolean;
73
+ }
74
+ export interface AnchorPosition {
75
+ top: number;
76
+ left: number;
77
+ anchorSide: AnchorSide;
78
+ }
79
+ /**
80
+ * Given a floating element and an anchor element, return coordinates for the top-left
81
+ * of the floating element in order to absolutely position it such that it appears
82
+ * near the anchor element.
83
+ *
84
+ * @param floatingElement Element intended to be positioned near or within an anchor
85
+ * @param anchorElement The element to serve as the position anchor
86
+ * @param settings Settings to determine the rules for positioning the floating element
87
+ * @returns {top: number, left: number} coordinates for the floating element
88
+ */
89
+ export declare function getAnchoredPosition(floatingElement: Element, anchorElement: Element | DOMRect, settings?: Partial<PositionSettings>): AnchorPosition;
@@ -0,0 +1,309 @@
1
+ // When prettier supports template literal types...
2
+ // export type AnchorSide = `${'inside' | 'outside'}-${'top' | 'bottom' | 'right' | 'left'}` | 'inside-center'
3
+
4
+ /**
5
+ * Settings that customize how a floating element is positioned
6
+ * with respect to an anchor element.
7
+ */
8
+ // For each outside anchor position, list the order of alternate positions to try in
9
+ // the event that the original position overflows. See comment on `allowOutOfBounds`
10
+ // for a more detailed description.
11
+ const alternateOrders = {
12
+ 'outside-top': ['outside-bottom', 'outside-right', 'outside-left', 'outside-bottom'],
13
+ 'outside-bottom': ['outside-top', 'outside-right', 'outside-left', 'outside-bottom'],
14
+ 'outside-left': ['outside-right', 'outside-bottom', 'outside-top', 'outside-bottom'],
15
+ 'outside-right': ['outside-left', 'outside-bottom', 'outside-top', 'outside-bottom']
16
+ };
17
+
18
+ /**
19
+ * Given a floating element and an anchor element, return coordinates for the top-left
20
+ * of the floating element in order to absolutely position it such that it appears
21
+ * near the anchor element.
22
+ *
23
+ * @param floatingElement Element intended to be positioned near or within an anchor
24
+ * @param anchorElement The element to serve as the position anchor
25
+ * @param settings Settings to determine the rules for positioning the floating element
26
+ * @returns {top: number, left: number} coordinates for the floating element
27
+ */
28
+ export function getAnchoredPosition(floatingElement, anchorElement, settings = {}) {
29
+ const parentElement = getPositionedParent(floatingElement);
30
+ const clippingRect = getClippingRect(parentElement);
31
+ const parentElementStyle = getComputedStyle(parentElement);
32
+ const parentElementRect = parentElement.getBoundingClientRect();
33
+ const [borderTop, borderLeft] = [parentElementStyle.borderTopWidth, parentElementStyle.borderLeftWidth].map(v => parseInt(v, 10) || 0);
34
+ const relativeRect = {
35
+ top: parentElementRect.top + borderTop,
36
+ left: parentElementRect.left + borderLeft
37
+ };
38
+ return pureCalculateAnchoredPosition(clippingRect, relativeRect, floatingElement.getBoundingClientRect(), anchorElement instanceof Element ? anchorElement.getBoundingClientRect() : anchorElement, getDefaultSettings(settings));
39
+ }
40
+ /**
41
+ * Returns the nearest proper HTMLElement parent of `element` whose
42
+ * position is not "static", or document.body, whichever is closer
43
+ */
44
+
45
+ function getPositionedParent(element) {
46
+ let parentNode = element.parentNode;
47
+
48
+ while (parentNode !== null) {
49
+ if (parentNode instanceof HTMLElement && getComputedStyle(parentNode).position !== 'static') {
50
+ return parentNode;
51
+ }
52
+
53
+ parentNode = parentNode.parentNode;
54
+ }
55
+
56
+ return document.body;
57
+ }
58
+ /**
59
+ * Returns the rectangle (relative to the window) that will clip the given element
60
+ * if it is rendered outside of its bounds.
61
+ * @param element
62
+ * @returns
63
+ */
64
+
65
+
66
+ function getClippingRect(element) {
67
+ let parentNode = element;
68
+
69
+ while (parentNode !== null) {
70
+ if (parentNode === document.body) {
71
+ break;
72
+ }
73
+
74
+ const parentNodeStyle = getComputedStyle(parentNode);
75
+
76
+ if (parentNodeStyle.overflow !== 'visible') {
77
+ break;
78
+ }
79
+
80
+ parentNode = parentNode.parentNode;
81
+ }
82
+
83
+ const clippingNode = parentNode === document.body || !(parentNode instanceof HTMLElement) ? document.body : parentNode;
84
+ const elemRect = clippingNode.getBoundingClientRect();
85
+ const elemStyle = getComputedStyle(clippingNode);
86
+ const [borderTop, borderLeft, borderRight, borderBottom] = [elemStyle.borderTopWidth, elemStyle.borderLeftWidth, elemStyle.borderRightWidth, elemStyle.borderBottomWidth].map(v => parseInt(v, 10) || 0);
87
+ return {
88
+ top: elemRect.top + borderTop,
89
+ left: elemRect.left + borderLeft,
90
+ width: elemRect.width - borderRight - borderLeft,
91
+ // If the clipping node is document.body, it can expand to the full height of the window
92
+ height: Math.max(elemRect.height - borderTop - borderBottom, clippingNode === document.body ? window.innerHeight : -Infinity)
93
+ };
94
+ } // Default settings to position a floating element
95
+
96
+
97
+ const positionDefaults = {
98
+ side: 'outside-bottom',
99
+ align: 'start',
100
+ // note: the following default is not applied if side === "inside-center"
101
+ anchorOffset: 4,
102
+ // note: the following default is only applied if side starts with "inside"
103
+ // and align is not center
104
+ alignmentOffset: 4,
105
+ allowOutOfBounds: false
106
+ };
107
+ /**
108
+ * Compute a full PositionSettings object from the given partial PositionSettings object
109
+ * by filling in with defaults where applicable.
110
+ * @param settings Partial settings - any omissions will be defaulted
111
+ */
112
+
113
+ function getDefaultSettings(settings = {}) {
114
+ var _settings$side, _settings$align, _settings$anchorOffse, _settings$alignmentOf, _settings$allowOutOfB;
115
+
116
+ const side = (_settings$side = settings.side) !== null && _settings$side !== void 0 ? _settings$side : positionDefaults.side;
117
+ const align = (_settings$align = settings.align) !== null && _settings$align !== void 0 ? _settings$align : positionDefaults.align;
118
+ return {
119
+ side,
120
+ align,
121
+ // offsets always default to 0 if their respective side/alignment is centered
122
+ anchorOffset: (_settings$anchorOffse = settings.anchorOffset) !== null && _settings$anchorOffse !== void 0 ? _settings$anchorOffse : side === 'inside-center' ? 0 : positionDefaults.anchorOffset,
123
+ alignmentOffset: (_settings$alignmentOf = settings.alignmentOffset) !== null && _settings$alignmentOf !== void 0 ? _settings$alignmentOf : align !== 'center' && side.startsWith('inside') ? positionDefaults.alignmentOffset : 0,
124
+ allowOutOfBounds: (_settings$allowOutOfB = settings.allowOutOfBounds) !== null && _settings$allowOutOfB !== void 0 ? _settings$allowOutOfB : positionDefaults.allowOutOfBounds
125
+ };
126
+ }
127
+ /**
128
+ * Note: This is a pure function with no dependency on DOM APIs.
129
+ * @see getAnchoredPosition
130
+ * @see getDefaultSettings
131
+ * @param viewportRect BoxPosition for the rectangle that will clip the floating element if it is
132
+ * rendered outside of the boundsof the rectangle.
133
+ * @param relativePosition Position for the closest positioned proper parent of the floating element
134
+ * @param floatingRect WidthAndHeight for the floating element
135
+ * @param anchorRect BoxPosition for the anchor element
136
+ * @param PositionSettings to customize the calculated position for the floating element.
137
+ */
138
+
139
+
140
+ function pureCalculateAnchoredPosition(viewportRect, relativePosition, floatingRect, anchorRect, {
141
+ side,
142
+ align,
143
+ allowOutOfBounds,
144
+ anchorOffset,
145
+ alignmentOffset
146
+ }) {
147
+ // Compute the relative viewport rect, to bring it into the same coordinate space as `pos`
148
+ const relativeViewportRect = {
149
+ top: viewportRect.top - relativePosition.top,
150
+ left: viewportRect.left - relativePosition.left,
151
+ width: viewportRect.width,
152
+ height: viewportRect.height
153
+ };
154
+ let pos = calculatePosition(floatingRect, anchorRect, side, align, anchorOffset, alignmentOffset);
155
+ let anchorSide = side;
156
+ pos.top -= relativePosition.top;
157
+ pos.left -= relativePosition.left; // Handle screen overflow
158
+
159
+ if (!allowOutOfBounds) {
160
+ const alternateOrder = alternateOrders[side];
161
+ let positionAttempt = 0;
162
+
163
+ if (alternateOrder) {
164
+ let prevSide = side; // Try all the alternate sides until one does not overflow
165
+
166
+ while (positionAttempt < alternateOrder.length && shouldRecalculatePosition(prevSide, pos, relativeViewportRect, floatingRect)) {
167
+ const nextSide = alternateOrder[positionAttempt++];
168
+ prevSide = nextSide; // If we have cut off in the same dimension as the "side" option, try flipping to the opposite side.
169
+
170
+ pos = calculatePosition(floatingRect, anchorRect, nextSide, align, anchorOffset, alignmentOffset);
171
+ pos.top -= relativePosition.top;
172
+ pos.left -= relativePosition.left;
173
+ anchorSide = nextSide;
174
+ }
175
+ } // At this point we've flipped the position if applicable. Now just nudge until it's on-screen.
176
+
177
+
178
+ if (pos.top < relativeViewportRect.top) {
179
+ pos.top = relativeViewportRect.top;
180
+ }
181
+
182
+ if (pos.left < relativeViewportRect.left) {
183
+ pos.left = relativeViewportRect.left;
184
+ }
185
+
186
+ if (pos.left + floatingRect.width > viewportRect.width + relativeViewportRect.left) {
187
+ pos.left = viewportRect.width + relativeViewportRect.left - floatingRect.width;
188
+ } // If we have exhausted all possible positions and none of them worked, we
189
+ // say that overflowing the bottom of the screen is acceptable since it is
190
+ // likely to be able to scroll.
191
+
192
+
193
+ if (alternateOrder && positionAttempt < alternateOrder.length) {
194
+ if (pos.top + floatingRect.height > viewportRect.height + relativeViewportRect.top) {
195
+ pos.top = viewportRect.height + relativeViewportRect.top - floatingRect.height;
196
+ }
197
+ }
198
+ }
199
+
200
+ return { ...pos,
201
+ anchorSide
202
+ };
203
+ }
204
+ /**
205
+ * Given a floating element and an anchor element, return coordinates for the
206
+ * top-left of the floating element in order to absolutely position it such
207
+ * that it appears near the anchor element.
208
+ *
209
+ * @param elementDimensions Dimensions of the floating element
210
+ * @param anchorPosition Position of the anchor element
211
+ * @param side Side of the anchor to position the floating element
212
+ * @param align How to align the floating element with the anchor element
213
+ * @param anchorOffset Absolute pixel offset for anchor positioning
214
+ * @param alignmentOffset Absolute pixel offset for alignment
215
+ * @returns {top: number, left: number} coordinates for the floating element
216
+ */
217
+
218
+
219
+ function calculatePosition(elementDimensions, anchorPosition, side, align, anchorOffset, alignmentOffset) {
220
+ const anchorRight = anchorPosition.left + anchorPosition.width;
221
+ const anchorBottom = anchorPosition.top + anchorPosition.height;
222
+ let top = -1;
223
+ let left = -1;
224
+
225
+ if (side === 'outside-top') {
226
+ top = anchorPosition.top - anchorOffset - elementDimensions.height;
227
+ } else if (side === 'outside-bottom') {
228
+ top = anchorBottom + anchorOffset;
229
+ } else if (side === 'outside-left') {
230
+ left = anchorPosition.left - anchorOffset - elementDimensions.width;
231
+ } else if (side === 'outside-right') {
232
+ left = anchorRight + anchorOffset;
233
+ }
234
+
235
+ if (side === 'outside-top' || side === 'outside-bottom') {
236
+ if (align === 'start') {
237
+ left = anchorPosition.left + alignmentOffset;
238
+ } else if (align === 'center') {
239
+ left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
240
+ } else {
241
+ // end
242
+ left = anchorRight - elementDimensions.width - alignmentOffset;
243
+ }
244
+ }
245
+
246
+ if (side === 'outside-left' || side === 'outside-right') {
247
+ if (align === 'start') {
248
+ top = anchorPosition.top + alignmentOffset;
249
+ } else if (align === 'center') {
250
+ top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
251
+ } else {
252
+ // end
253
+ top = anchorBottom - elementDimensions.height - alignmentOffset;
254
+ }
255
+ }
256
+
257
+ if (side === 'inside-top') {
258
+ top = anchorPosition.top + anchorOffset;
259
+ } else if (side === 'inside-bottom') {
260
+ top = anchorBottom - anchorOffset - elementDimensions.height;
261
+ } else if (side === 'inside-left') {
262
+ left = anchorPosition.left + anchorOffset;
263
+ } else if (side === 'inside-right') {
264
+ left = anchorRight - anchorOffset - elementDimensions.width;
265
+ } else if (side === 'inside-center') {
266
+ left = (anchorRight + anchorPosition.left) / 2 - elementDimensions.width / 2 + anchorOffset;
267
+ }
268
+
269
+ if (side === 'inside-top' || side === 'inside-bottom') {
270
+ if (align === 'start') {
271
+ left = anchorPosition.left + alignmentOffset;
272
+ } else if (align === 'center') {
273
+ left = anchorPosition.left - (elementDimensions.width - anchorPosition.width) / 2 + alignmentOffset;
274
+ } else {
275
+ // end
276
+ left = anchorRight - elementDimensions.width - alignmentOffset;
277
+ }
278
+ } else if (side === 'inside-left' || side === 'inside-right' || side === 'inside-center') {
279
+ if (align === 'start') {
280
+ top = anchorPosition.top + alignmentOffset;
281
+ } else if (align === 'center') {
282
+ top = anchorPosition.top - (elementDimensions.height - anchorPosition.height) / 2 + alignmentOffset;
283
+ } else {
284
+ // end
285
+ top = anchorBottom - elementDimensions.height - alignmentOffset;
286
+ }
287
+ }
288
+
289
+ return {
290
+ top,
291
+ left
292
+ };
293
+ }
294
+ /**
295
+ * Determines if there is an overflow
296
+ * @param side
297
+ * @param currentPos
298
+ * @param containerDimensions
299
+ * @param elementDimensions
300
+ */
301
+
302
+
303
+ function shouldRecalculatePosition(side, currentPos, containerDimensions, elementDimensions) {
304
+ if (side === 'outside-top' || side === 'outside-bottom') {
305
+ return currentPos.top < containerDimensions.top || currentPos.top + elementDimensions.height > containerDimensions.height + containerDimensions.top;
306
+ } else {
307
+ return currentPos.left < containerDimensions.left || currentPos.left + elementDimensions.width > containerDimensions.width + containerDimensions.left;
308
+ }
309
+ }
@@ -0,0 +1,12 @@
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;