@primer/behaviors 0.0.0-202201710334 → 0.0.0-2022017122041

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 (61) 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/lib/index.d.ts +4 -0
  18. package/lib/polyfills/event-listener-signal.d.ts +6 -0
  19. package/lib/polyfills/event-listener-signal.js +64 -0
  20. package/lib/scroll-into-view.d.ts +7 -0
  21. package/lib/scroll-into-view.js +42 -0
  22. package/lib/utils/index.d.ts +3 -0
  23. package/lib/utils/index.js +44 -0
  24. package/lib/utils/iterate-focusable-elements.d.ts +42 -0
  25. package/lib/utils/iterate-focusable-elements.js +113 -0
  26. package/lib/utils/unique-id.d.ts +1 -0
  27. package/lib/utils/unique-id.js +12 -0
  28. package/lib/utils/user-agent.d.ts +1 -0
  29. package/lib/utils/user-agent.js +15 -0
  30. package/lib-esm/__tests__/anchored-position.test.d.ts +1 -0
  31. package/lib-esm/__tests__/anchored-position.test.js +386 -0
  32. package/lib-esm/__tests__/focus-trap.test.d.ts +1 -0
  33. package/lib-esm/__tests__/focus-trap.test.js +227 -0
  34. package/lib-esm/__tests__/focus-zone.test.d.ts +1 -0
  35. package/lib-esm/__tests__/focus-zone.test.js +487 -0
  36. package/lib-esm/__tests__/iterate-focusable-elements.test.d.ts +1 -0
  37. package/lib-esm/__tests__/iterate-focusable-elements.test.js +48 -0
  38. package/lib-esm/__tests__/scroll-into-view.test.d.ts +1 -0
  39. package/lib-esm/__tests__/scroll-into-view.test.js +243 -0
  40. package/lib-esm/anchored-position.d.ts +89 -0
  41. package/lib-esm/anchored-position.js +309 -0
  42. package/lib-esm/focus-trap.d.ts +12 -0
  43. package/lib-esm/focus-trap.js +170 -0
  44. package/lib-esm/focus-zone.d.ts +137 -0
  45. package/lib-esm/focus-zone.js +559 -0
  46. package/lib-esm/index.d.ts +4 -0
  47. package/lib-esm/index.js +4 -0
  48. package/lib-esm/polyfills/event-listener-signal.d.ts +6 -0
  49. package/lib-esm/polyfills/event-listener-signal.js +57 -0
  50. package/lib-esm/scroll-into-view.d.ts +7 -0
  51. package/lib-esm/scroll-into-view.js +35 -0
  52. package/lib-esm/utils/index.d.ts +3 -0
  53. package/lib-esm/utils/index.js +3 -0
  54. package/lib-esm/utils/iterate-focusable-elements.d.ts +42 -0
  55. package/lib-esm/utils/iterate-focusable-elements.js +102 -0
  56. package/lib-esm/utils/unique-id.d.ts +1 -0
  57. package/lib-esm/utils/unique-id.js +5 -0
  58. package/lib-esm/utils/user-agent.d.ts +1 -0
  59. package/lib-esm/utils/user-agent.js +8 -0
  60. package/package.json +8 -6
  61. package/utils/package.json +0 -7
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.scrollIntoView = scrollIntoView;
7
+
8
+ function scrollIntoView(child, viewingArea, {
9
+ direction = 'vertical',
10
+ startMargin = 0,
11
+ endMargin = 0,
12
+ behavior = 'smooth'
13
+ } = {}) {
14
+ const startSide = direction === 'vertical' ? 'top' : 'left';
15
+ const endSide = direction === 'vertical' ? 'bottom' : 'right';
16
+ const scrollSide = direction === 'vertical' ? 'scrollTop' : 'scrollLeft';
17
+ const {
18
+ [startSide]: childStart,
19
+ [endSide]: childEnd
20
+ } = child.getBoundingClientRect();
21
+ const {
22
+ [startSide]: viewingAreaStart,
23
+ [endSide]: viewingAreaEnd
24
+ } = viewingArea.getBoundingClientRect();
25
+ const isChildStartAboveViewingArea = childStart < viewingAreaStart + startMargin;
26
+ const isChildBottomBelowViewingArea = childEnd > viewingAreaEnd - endMargin;
27
+
28
+ if (isChildStartAboveViewingArea) {
29
+ const scrollHeightToChildStart = childStart - viewingAreaStart + viewingArea[scrollSide];
30
+ viewingArea.scrollTo({
31
+ behavior,
32
+ [startSide]: scrollHeightToChildStart - startMargin
33
+ });
34
+ } else if (isChildBottomBelowViewingArea) {
35
+ const scrollHeightToChildBottom = childEnd - viewingAreaEnd + viewingArea[scrollSide];
36
+ viewingArea.scrollTo({
37
+ behavior,
38
+ [startSide]: scrollHeightToChildBottom + endMargin
39
+ });
40
+ } // either completely in view or outside viewing area on both ends, don't scroll
41
+
42
+ }
@@ -0,0 +1,3 @@
1
+ export * from './iterate-focusable-elements.js';
2
+ export * from './unique-id.js';
3
+ export * from './user-agent.js';
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _iterateFocusableElements = require("./iterate-focusable-elements.js");
8
+
9
+ Object.keys(_iterateFocusableElements).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _iterateFocusableElements[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _iterateFocusableElements[key];
16
+ }
17
+ });
18
+ });
19
+
20
+ var _uniqueId = require("./unique-id.js");
21
+
22
+ Object.keys(_uniqueId).forEach(function (key) {
23
+ if (key === "default" || key === "__esModule") return;
24
+ if (key in exports && exports[key] === _uniqueId[key]) return;
25
+ Object.defineProperty(exports, key, {
26
+ enumerable: true,
27
+ get: function () {
28
+ return _uniqueId[key];
29
+ }
30
+ });
31
+ });
32
+
33
+ var _userAgent = require("./user-agent.js");
34
+
35
+ Object.keys(_userAgent).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (key in exports && exports[key] === _userAgent[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function () {
41
+ return _userAgent[key];
42
+ }
43
+ });
44
+ });
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Options to the focusable elements iterator
3
+ */
4
+ export interface IterateFocusableElements {
5
+ /**
6
+ * (Default: false) Iterate through focusable elements in reverse-order
7
+ */
8
+ reverse?: boolean;
9
+ /**
10
+ * (Default: false) Perform additional checks to determine tabbability
11
+ * which may adversely affect app performance.
12
+ */
13
+ strict?: boolean;
14
+ /**
15
+ * (Default: false) Only iterate tabbable elements, which is the subset
16
+ * of focusable elements that are part of the page's tab sequence.
17
+ */
18
+ onlyTabbable?: boolean;
19
+ }
20
+ /**
21
+ * Returns an iterator over all of the focusable elements within `container`.
22
+ * Note: If `container` is itself focusable it will be included in the results.
23
+ * @param container The container over which to find focusable elements.
24
+ * @param reverse If true, iterate backwards through focusable elements.
25
+ */
26
+ export declare function iterateFocusableElements(container: HTMLElement, options?: IterateFocusableElements): Generator<HTMLElement, undefined, undefined>;
27
+ /**
28
+ * Determines whether the given element is focusable. If `strict` is true, we may
29
+ * perform additional checks that require a reflow (less performant).
30
+ * @param elem
31
+ * @param strict
32
+ */
33
+ export declare function isFocusable(elem: HTMLElement, strict?: boolean): boolean;
34
+ /**
35
+ * Determines whether the given element is tabbable. If `strict` is true, we may
36
+ * perform additional checks that require a reflow (less performant). This check
37
+ * ensures that the element is focusable and that its tabindex is not explicitly
38
+ * set to "-1" (which makes it focusable, but removes it from the tab order).
39
+ * @param elem
40
+ * @param strict
41
+ */
42
+ export declare function isTabbable(elem: HTMLElement, strict?: boolean): boolean;
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isFocusable = isFocusable;
7
+ exports.isTabbable = isTabbable;
8
+ exports.iterateFocusableElements = iterateFocusableElements;
9
+
10
+ /**
11
+ * Options to the focusable elements iterator
12
+ */
13
+
14
+ /**
15
+ * Returns an iterator over all of the focusable elements within `container`.
16
+ * Note: If `container` is itself focusable it will be included in the results.
17
+ * @param container The container over which to find focusable elements.
18
+ * @param reverse If true, iterate backwards through focusable elements.
19
+ */
20
+ function* iterateFocusableElements(container, options = {}) {
21
+ var _options$strict, _options$onlyTabbable;
22
+
23
+ const strict = (_options$strict = options.strict) !== null && _options$strict !== void 0 ? _options$strict : false;
24
+ const acceptFn = ((_options$onlyTabbable = options.onlyTabbable) !== null && _options$onlyTabbable !== void 0 ? _options$onlyTabbable : false) ? isTabbable : isFocusable;
25
+ const walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
26
+ acceptNode: node => node instanceof HTMLElement && acceptFn(node, strict) ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP
27
+ });
28
+ let nextNode = null; // Allow the container to participate
29
+
30
+ if (!options.reverse && acceptFn(container, strict)) {
31
+ yield container;
32
+ } // If iterating in reverse, continue traversing down into the last child until we reach
33
+ // a leaf DOM node
34
+
35
+
36
+ if (options.reverse) {
37
+ let lastChild = walker.lastChild();
38
+
39
+ while (lastChild) {
40
+ nextNode = lastChild;
41
+ lastChild = walker.lastChild();
42
+ }
43
+ } else {
44
+ nextNode = walker.firstChild();
45
+ }
46
+
47
+ while (nextNode instanceof HTMLElement) {
48
+ yield nextNode;
49
+ nextNode = options.reverse ? walker.previousNode() : walker.nextNode();
50
+ } // Allow the container to participate (in reverse)
51
+
52
+
53
+ if (options.reverse && acceptFn(container, strict)) {
54
+ yield container;
55
+ }
56
+
57
+ return undefined;
58
+ }
59
+ /**
60
+ * Determines whether the given element is focusable. If `strict` is true, we may
61
+ * perform additional checks that require a reflow (less performant).
62
+ * @param elem
63
+ * @param strict
64
+ */
65
+
66
+
67
+ function isFocusable(elem, strict = false) {
68
+ // Certain conditions cause an element to never be focusable, even if they have tabindex="0"
69
+ const disabledAttrInert = ['BUTTON', 'INPUT', 'SELECT', 'TEXTAREA', 'OPTGROUP', 'OPTION', 'FIELDSET'].includes(elem.tagName) && elem.disabled;
70
+ const hiddenInert = elem.hidden;
71
+ const hiddenInputInert = elem instanceof HTMLInputElement && elem.type === 'hidden';
72
+
73
+ if (disabledAttrInert || hiddenInert || hiddenInputInert) {
74
+ return false;
75
+ } // Each of the conditions checked below require a reflow, thus are gated by the `strict`
76
+ // argument. If any are true, the element is not focusable, even if tabindex is set.
77
+
78
+
79
+ if (strict) {
80
+ const sizeInert = elem.offsetWidth === 0 || elem.offsetHeight === 0;
81
+ const visibilityInert = ['hidden', 'collapse'].includes(getComputedStyle(elem).visibility);
82
+ const clientRectsInert = elem.getClientRects().length === 0;
83
+
84
+ if (sizeInert || visibilityInert || clientRectsInert) {
85
+ return false;
86
+ }
87
+ } // Any element with `tabindex` explicitly set can be focusable, even if it's set to "-1"
88
+
89
+
90
+ if (elem.getAttribute('tabindex') != null) {
91
+ return true;
92
+ } // One last way `elem.tabIndex` can be wrong.
93
+
94
+
95
+ if (elem instanceof HTMLAnchorElement && elem.getAttribute('href') == null) {
96
+ return false;
97
+ }
98
+
99
+ return elem.tabIndex !== -1;
100
+ }
101
+ /**
102
+ * Determines whether the given element is tabbable. If `strict` is true, we may
103
+ * perform additional checks that require a reflow (less performant). This check
104
+ * ensures that the element is focusable and that its tabindex is not explicitly
105
+ * set to "-1" (which makes it focusable, but removes it from the tab order).
106
+ * @param elem
107
+ * @param strict
108
+ */
109
+
110
+
111
+ function isTabbable(elem, strict = false) {
112
+ return isFocusable(elem, strict) && elem.getAttribute('tabindex') !== '-1';
113
+ }
@@ -0,0 +1 @@
1
+ export declare function uniqueId(): string;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.uniqueId = uniqueId;
7
+ // Note: uniqueId may be unsafe in SSR contexts if it is used create DOM IDs or otherwise cause a hydration warning. Use useSSRSafeId instead.
8
+ let idSeed = 10000;
9
+
10
+ function uniqueId() {
11
+ return `__primer_id_${idSeed++}`;
12
+ }
@@ -0,0 +1 @@
1
+ export declare function isMacOS(): boolean;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isMacOS = isMacOS;
7
+ let isMac = undefined;
8
+
9
+ function isMacOS() {
10
+ if (isMac === undefined) {
11
+ isMac = /^mac/i.test(window.navigator.platform);
12
+ }
13
+
14
+ return isMac;
15
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,386 @@
1
+ import { getAnchoredPosition } from '../anchored-position.js';
2
+ /*
3
+
4
+ Note: In each test below, we check the calculation from getAnchoredPosition against exact
5
+ values. For each `expect` call, there is an accompanying comment that distills the effective
6
+ calculation from the inputs, which may help debugging in the event of a test failure.
7
+
8
+ */
9
+ // The DOMRect constructor isn't available in JSDOM, so we improvise here.
10
+
11
+ function makeDOMRect(x, y, width, height) {
12
+ return {
13
+ x,
14
+ y,
15
+ width,
16
+ height,
17
+ top: y,
18
+ left: x,
19
+ right: x + width,
20
+ bottom: y + height,
21
+
22
+ toJSON() {
23
+ return this;
24
+ }
25
+
26
+ };
27
+ } // Since Jest/JSDOM doesn't support layout, we can stub out getBoundingClientRect if we know the
28
+ // correct dimensions. JSDOM will handle the rest of the DOM API used by getAnchoredPosition.
29
+
30
+
31
+ function createVirtualDOM(parentRect, anchorRect, floatingRect, parentBorders = {
32
+ top: 0,
33
+ right: 0,
34
+ bottom: 0,
35
+ left: 0
36
+ }) {
37
+ const parent = document.createElement('div');
38
+ parent.style.overflow = 'hidden';
39
+ parent.style.position = 'relative';
40
+ parent.style.borderTopWidth = `${parentBorders.top}px`;
41
+ parent.style.borderRightWidth = `${parentBorders.right}px`;
42
+ parent.style.borderBottomWidth = `${parentBorders.bottom}px`;
43
+ parent.style.borderLeftWidth = `${parentBorders.left}px`;
44
+ parent.id = 'parent';
45
+ parent.innerHTML = '<div id="float"></div><div id="anchor"></div>';
46
+ const float = parent.querySelector('#float');
47
+ const anchor = parent.querySelector('#anchor');
48
+
49
+ anchor.getBoundingClientRect = () => anchorRect;
50
+
51
+ parent.getBoundingClientRect = () => parentRect;
52
+
53
+ float.getBoundingClientRect = () => floatingRect;
54
+
55
+ return {
56
+ float,
57
+ parent,
58
+ anchor
59
+ };
60
+ }
61
+
62
+ describe('getAnchoredPosition', () => {
63
+ it('returns the correct position in the default case with no overflow', () => {
64
+ const anchorRect = makeDOMRect(300, 200, 50, 50);
65
+ const floatingRect = makeDOMRect(NaN, NaN, 100, 100);
66
+ document.body.innerHTML = '<div id="float"></div><div id="anchor"></div>';
67
+ const float = document.querySelector('#float');
68
+ const anchor = document.querySelector('#anchor');
69
+
70
+ float.getBoundingClientRect = () => floatingRect;
71
+
72
+ anchor.getBoundingClientRect = () => anchorRect;
73
+
74
+ document.body.getBoundingClientRect = () => makeDOMRect(0, 0, 1920, 0);
75
+
76
+ Object.defineProperty(window, 'innerHeight', {
77
+ get: () => 1080
78
+ });
79
+ const settings = {
80
+ anchorOffset: 4
81
+ };
82
+ const {
83
+ top,
84
+ left
85
+ } = getAnchoredPosition(float, anchor, settings);
86
+ expect(top).toEqual(254);
87
+ expect(left).toEqual(300);
88
+ });
89
+ it('returns the correct position in the default case with no overflow, inside a clipping parent', () => {
90
+ const parentRect = makeDOMRect(20, 20, 500, 500);
91
+ const anchorRect = makeDOMRect(300, 200, 50, 50);
92
+ const floatingRect = makeDOMRect(NaN, NaN, 100, 100);
93
+ const {
94
+ float,
95
+ anchor
96
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
97
+ const settings = {
98
+ anchorOffset: 4
99
+ };
100
+ const {
101
+ top,
102
+ left
103
+ } = getAnchoredPosition(float, anchor, settings);
104
+ expect(top).toEqual(234);
105
+ expect(left).toEqual(280);
106
+ });
107
+ it('returns the correct position for different outside side settings with no overflow', () => {
108
+ const parentRect = makeDOMRect(20, 20, 500, 500);
109
+ const anchorRect = makeDOMRect(300, 200, 50, 50);
110
+ const floatingRect = makeDOMRect(NaN, NaN, 100, 100);
111
+ const {
112
+ float,
113
+ anchor
114
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
115
+ const settings = {};
116
+ let top = 0;
117
+ let left = 0; // should be the same calculation as the default settings test above
118
+
119
+ settings.side = 'outside-bottom';
120
+ ({
121
+ top,
122
+ left
123
+ } = getAnchoredPosition(float, anchor, settings));
124
+ expect(top).toEqual(234); // anchorRect.top + anchorRect.height + (settings.anchorOffset ?? 4) - parentRect.top
125
+
126
+ expect(left).toEqual(280); // anchorRect.left - parentRect.left
127
+
128
+ settings.side = 'outside-left';
129
+ ({
130
+ top,
131
+ left
132
+ } = getAnchoredPosition(float, anchor, settings));
133
+ expect(top).toEqual(180); // anchorRect.top - parentRect.top
134
+
135
+ expect(left).toEqual(176); // anchorRect.left - floatingRect.width - (settings.anchorOffset ?? 4) - parentRect.left
136
+
137
+ settings.side = 'outside-right';
138
+ ({
139
+ top,
140
+ left
141
+ } = getAnchoredPosition(float, anchor, settings));
142
+ expect(top).toEqual(180); // anchorRect.top - parentRect.top
143
+
144
+ expect(left).toEqual(334); // anchorRect.left + anchorRect.width + (settings.anchorOffset ?? 4) - parentRect.left
145
+
146
+ settings.side = 'outside-top';
147
+ ({
148
+ top,
149
+ left
150
+ } = getAnchoredPosition(float, anchor, settings));
151
+ expect(top).toEqual(76); // anchorRect.top - floatingRect.height - (settings.anchorOffset ?? 4) - parentRect.top
152
+
153
+ expect(left).toEqual(280); // anchorRect.left - parentRect.left
154
+ });
155
+ it('returns the correct position for different inside side settings', () => {
156
+ const parentRect = makeDOMRect(20, 20, 500, 500);
157
+ const anchorRect = makeDOMRect(300, 200, 50, 50);
158
+ const floatingRect = makeDOMRect(NaN, NaN, 100, 100);
159
+ const {
160
+ float,
161
+ anchor
162
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
163
+ const settings = {};
164
+ let top = 0;
165
+ let left = 0;
166
+ settings.side = 'inside-bottom';
167
+ ({
168
+ top,
169
+ left
170
+ } = getAnchoredPosition(float, anchor, settings)); // anchorRect.top + anchorRect.height - (settings.anchorOffset ?? 4) - floatingRect.height - parentRect.top
171
+
172
+ expect(top).toEqual(126); // anchorRect.left + (settings.alignmentOffset ?? 4) - parentRect.left
173
+
174
+ expect(left).toEqual(284);
175
+ settings.side = 'inside-left';
176
+ ({
177
+ top,
178
+ left
179
+ } = getAnchoredPosition(float, anchor, settings));
180
+ expect(top).toEqual(184); // anchorRect.top + (settings.alignmentOffset ?? 4) - parentRect.top
181
+
182
+ expect(left).toEqual(284); // anchorRect.left + (settings.anchorOffset ?? 4) - parentRect.left
183
+
184
+ settings.side = 'inside-right';
185
+ ({
186
+ top,
187
+ left
188
+ } = getAnchoredPosition(float, anchor, settings)); // anchorRect.top + (settings.alignmentOffset ?? 4) - parentRect.top
189
+
190
+ expect(top).toEqual(184); // anchorRect.left + anchorRect.width - (settings.anchorOffset ?? 4) - floatingRect.width - parentRect.left
191
+
192
+ expect(left).toEqual(226); // almost the same as inside-left, with the exception of offsets
193
+
194
+ settings.side = 'inside-top';
195
+ ({
196
+ top,
197
+ left
198
+ } = getAnchoredPosition(float, anchor, settings));
199
+ expect(top).toEqual(184); // anchorRect.top + (settings.anchorOffset ?? 4) - parentRect.top
200
+
201
+ expect(left).toEqual(284); // anchorRect.left + (settings.alignmentOffset ?? 4) - parentRect.left
202
+
203
+ settings.side = 'inside-center';
204
+ ({
205
+ top,
206
+ left
207
+ } = getAnchoredPosition(float, anchor, settings));
208
+ expect(top).toEqual(184); // anchorRect.top + (settings.alignmentOffset ?? 4) - parentRect.top
209
+
210
+ expect(left).toEqual(255); // anchorRect.left + anchorRect.width / 2 - floatingRect.width / 2 - parentRect.left
211
+ });
212
+ it('returns the correct position inside centering along both axes', () => {
213
+ const parentRect = makeDOMRect(20, 20, 500, 500);
214
+ const anchorRect = makeDOMRect(300, 200, 50, 50);
215
+ const floatingRect = makeDOMRect(NaN, NaN, 100, 100);
216
+ const {
217
+ float,
218
+ anchor
219
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
220
+ const settings = {
221
+ side: 'inside-center',
222
+ align: 'center'
223
+ };
224
+ const {
225
+ top,
226
+ left
227
+ } = getAnchoredPosition(float, anchor, settings);
228
+ expect(top).toEqual(155); // anchorRect.top + anchorRect.height / 2 - floatingRect.height / 2 - parentRect.top
229
+
230
+ expect(left).toEqual(255); // anchorRect.left + anchorRect.width / 2 - floatingRect.width / 2 - parentRect.left
231
+ });
232
+ it('returns the correct position for different alignment settings with no overflow', () => {
233
+ const parentRect = makeDOMRect(20, 20, 500, 500);
234
+ const anchorRect = makeDOMRect(300, 200, 50, 50);
235
+ const floatingRect = makeDOMRect(NaN, NaN, 100, 100);
236
+ const {
237
+ float,
238
+ anchor
239
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
240
+ const settings = {};
241
+ let top = 0;
242
+ let left = 0;
243
+ settings.align = 'start';
244
+ ({
245
+ top,
246
+ left
247
+ } = getAnchoredPosition(float, anchor, settings));
248
+ expect(top).toEqual(234); // anchorRect.top + anchorRect.height + (settings.anchorOffset ?? 4) - parentRect.top
249
+
250
+ expect(left).toEqual(280); // anchorRect.left + (settings.alignmentOffset ?? 0) - parentRect.left
251
+
252
+ settings.align = 'center';
253
+ ({
254
+ top,
255
+ left
256
+ } = getAnchoredPosition(float, anchor, settings)); // anchorRect.top + anchorRect.height + (settings.anchorOffset ?? 4) - parentRect.top
257
+
258
+ expect(top).toEqual(234); // anchorRect.left + anchorRect.width / 2 - floatingRect.width / 2 + (settings.anchorOffset ?? 0) - parentRect.left
259
+
260
+ expect(left).toEqual(255);
261
+ settings.align = 'end';
262
+ ({
263
+ top,
264
+ left
265
+ } = getAnchoredPosition(float, anchor, settings)); // anchorRect.top + anchorRect.height + (settings.anchorOffset ?? 4) - parentRect.top
266
+
267
+ expect(top).toEqual(234); // anchorRect.left + anchorRect.width - floatingRect.width - (settings.alignmentOffset ?? 0) - parentRect.left
268
+
269
+ expect(left).toEqual(230);
270
+ });
271
+ it('properly flips to the opposite side if the calculated position overflows along the same axis', () => {
272
+ const parentRect = makeDOMRect(20, 20, 500, 500);
273
+ const anchorRect = makeDOMRect(300, 400, 50, 50);
274
+ const floatingRect = makeDOMRect(NaN, NaN, 100, 100);
275
+ const {
276
+ float,
277
+ anchor
278
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
279
+ const settings = {};
280
+ const {
281
+ top,
282
+ left
283
+ } = getAnchoredPosition(float, anchor, settings);
284
+ expect(top).toEqual(276); // anchorRect.top - floatingRect.height - (settings.anchorOffset ?? 4) - parentRect.top
285
+
286
+ expect(left).toEqual(280); // anchorRect.left - parentRect.left
287
+ });
288
+ it('properly moves to an adjacent side if overflow happens along side edge and flipped edge', () => {
289
+ const parentRect = makeDOMRect(20, 20, 500, 200);
290
+ const anchorRect = makeDOMRect(300, 100, 50, 50);
291
+ const floatingRect = makeDOMRect(NaN, NaN, 100, 100);
292
+ const {
293
+ float,
294
+ anchor
295
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
296
+ const settings = {};
297
+ const {
298
+ top,
299
+ left
300
+ } = getAnchoredPosition(float, anchor, settings);
301
+ expect(top).toEqual(80); // anchorRect.top - parentRect.top
302
+
303
+ expect(left).toEqual(334); // anchorRect.left + anchorRect.width + (settings.anchorOffset ?? 4) - parentRect.left
304
+ });
305
+ it('properly adjusts the position using an alignment offset if overflow happens along the alignment edge', () => {
306
+ const parentRect = makeDOMRect(20, 20, 500, 500);
307
+ const anchorRect = makeDOMRect(300, 200, 50, 50);
308
+ const floatingRect = makeDOMRect(NaN, NaN, 400, 100);
309
+ const {
310
+ float,
311
+ anchor
312
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
313
+ const settings = {};
314
+ const {
315
+ top,
316
+ left
317
+ } = getAnchoredPosition(float, anchor, settings);
318
+ expect(top).toEqual(234); // anchorRect.top + anchorRect.height + (settings.anchorOffset ?? 4) - parentRect.top
319
+
320
+ expect(left).toEqual(100); // parentRect.width - floatingRect.width
321
+ });
322
+ it('properly calculates the position that needs to be flipped and offset-adjusted', () => {
323
+ const parentRect = makeDOMRect(20, 20, 500, 500);
324
+ const anchorRect = makeDOMRect(300, 400, 50, 50);
325
+ const floatingRect = makeDOMRect(NaN, NaN, 400, 100);
326
+ const {
327
+ float,
328
+ anchor
329
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
330
+ const settings = {};
331
+ const {
332
+ top,
333
+ left
334
+ } = getAnchoredPosition(float, anchor, settings);
335
+ expect(top).toEqual(276); // anchorRect.top - floatingRect.height - (settings.anchorOffset ?? 4) - parentRect.top
336
+
337
+ expect(left).toEqual(100); // parentRect.width - floatingRect.width
338
+ });
339
+ it('properly calculates the outside position with many simultaneous settings interactions (stress test)', () => {
340
+ const parentRect = makeDOMRect(20, 20, 200, 500);
341
+ const anchorRect = makeDOMRect(95, 295, 100, 200);
342
+ const floatingRect = makeDOMRect(NaN, NaN, 175, 200);
343
+ const {
344
+ float,
345
+ anchor
346
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
347
+ const settings = {
348
+ side: 'outside-right',
349
+ align: 'center',
350
+ alignmentOffset: 10,
351
+ anchorOffset: -10
352
+ };
353
+ const {
354
+ top,
355
+ left
356
+ } = getAnchoredPosition(float, anchor, settings); // expect to try right, left, and bottom before ending on top
357
+
358
+ expect(top).toEqual(85); // anchorRect.top - floatingRect.height - (settings.anchorOffset ?? 4) - parentRect.top
359
+ // expect center alignment to run against edge, so ignored. Also causes alignment offset to be ignored.
360
+
361
+ expect(left).toEqual(25); // parentRect.width - floatingRect.width
362
+ });
363
+ it('properly calculates the inside position with many simultaneous settings interactions (stress test)', () => {
364
+ const parentRect = makeDOMRect(20, 20, 500, 500);
365
+ const anchorRect = makeDOMRect(100, 100, 300, 300);
366
+ const floatingRect = makeDOMRect(NaN, NaN, 100, 200);
367
+ const {
368
+ float,
369
+ anchor
370
+ } = createVirtualDOM(parentRect, anchorRect, floatingRect);
371
+ const settings = {
372
+ side: 'inside-right',
373
+ align: 'center',
374
+ alignmentOffset: 10,
375
+ anchorOffset: -10
376
+ };
377
+ const {
378
+ top,
379
+ left
380
+ } = getAnchoredPosition(float, anchor, settings); // anchorRect.top + anchorRect.height / 2 - floatingRect.height / 2 + (settings.alignmentOffset ?? 4) - parentRect.top
381
+
382
+ expect(top).toEqual(140); // anchorRect.left + anchorRect.width - floatingRect.width - (settings.anchorOffset ?? 4) - parentRect.left
383
+
384
+ expect(left).toEqual(290);
385
+ });
386
+ });
@@ -0,0 +1 @@
1
+ export {};