@primer/behaviors 0.0.0-202201852029 → 0.0.0-202201891826
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.
- package/dist/anchored-position.d.ts +89 -0
- package/dist/focus-trap.d.ts +12 -0
- package/dist/focus-zone.d.ts +137 -0
- package/dist/index.d.ts +4 -0
- package/dist/polyfills/event-listener-signal.d.ts +6 -0
- package/dist/scroll-into-view.d.ts +7 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/iterate-focusable-elements.d.ts +42 -0
- package/dist/utils/unique-id.d.ts +1 -0
- package/dist/utils/user-agent.d.ts +1 -0
- package/package.json +13 -17
- package/utils/package.json +7 -0
- package/lib/__tests__/anchored-position.test.js +0 -388
- package/lib/__tests__/focus-trap.test.js +0 -234
- package/lib/__tests__/focus-zone.test.js +0 -570
- package/lib/__tests__/iterate-focusable-elements.test.js +0 -55
- package/lib/__tests__/scroll-into-view.test.js +0 -245
- package/lib-esm/__tests__/anchored-position.test.js +0 -386
- package/lib-esm/__tests__/focus-trap.test.js +0 -227
- package/lib-esm/__tests__/focus-zone.test.js +0 -487
- package/lib-esm/__tests__/iterate-focusable-elements.test.js +0 -48
- package/lib-esm/__tests__/scroll-into-view.test.js +0 -243
|
@@ -1,245 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _scrollIntoView = require("../scroll-into-view.js");
|
|
4
|
-
|
|
5
|
-
function scrollPositionFormula(positionData, isChildAboveViewingArea) {
|
|
6
|
-
const {
|
|
7
|
-
viewingAreaEdgePosition,
|
|
8
|
-
childEdgePosition,
|
|
9
|
-
margin
|
|
10
|
-
} = positionData;
|
|
11
|
-
const marginOffset = margin * (isChildAboveViewingArea ? -1 : 1);
|
|
12
|
-
return childEdgePosition - viewingAreaEdgePosition + marginOffset;
|
|
13
|
-
} // The DOMRect constructor isn't available in JSDOM, so we improvise here.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
function makeDOMRect(x, y, width, height) {
|
|
17
|
-
return {
|
|
18
|
-
x,
|
|
19
|
-
y,
|
|
20
|
-
width,
|
|
21
|
-
height,
|
|
22
|
-
top: y,
|
|
23
|
-
left: x,
|
|
24
|
-
right: x + width,
|
|
25
|
-
bottom: y + height,
|
|
26
|
-
|
|
27
|
-
toJSON() {
|
|
28
|
-
return this;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
};
|
|
32
|
-
} // Since Jest/JSDOM doesn't support layout, we can stub out getBoundingClientRect if we know the
|
|
33
|
-
// correct dimensions. JSDOM will handle the rest of the DOM API used by getAnchoredPosition.
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
function createVirtualDOM(viewingAreaRect, childRect) {
|
|
37
|
-
const viewingArea = document.createElement('div');
|
|
38
|
-
viewingArea.style.overflow = 'auto';
|
|
39
|
-
viewingArea.id = 'viewingArea';
|
|
40
|
-
viewingArea.innerHTML = '<div id="child"></div>';
|
|
41
|
-
const child = viewingArea.querySelector('#child');
|
|
42
|
-
|
|
43
|
-
child.getBoundingClientRect = () => childRect;
|
|
44
|
-
|
|
45
|
-
viewingArea.getBoundingClientRect = () => viewingAreaRect;
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
viewingArea,
|
|
49
|
-
child
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
describe('scrollIntoView', () => {
|
|
54
|
-
it('scrolls the expected amount when only the viewingArea element and child element are passed to the function', () => {
|
|
55
|
-
const scrollToMock = jest.fn();
|
|
56
|
-
Object.defineProperty(window.Element.prototype, 'scrollTo', {
|
|
57
|
-
writable: true,
|
|
58
|
-
value: scrollToMock
|
|
59
|
-
});
|
|
60
|
-
const childHeight = 50;
|
|
61
|
-
const viewAreaHeight = 100;
|
|
62
|
-
const childStart = viewAreaHeight + 10;
|
|
63
|
-
const expectedScrollPosition = scrollPositionFormula({
|
|
64
|
-
viewingAreaEdgePosition: viewAreaHeight,
|
|
65
|
-
childEdgePosition: childStart + childHeight,
|
|
66
|
-
margin: 0
|
|
67
|
-
}, false);
|
|
68
|
-
const viewingAreaRect = makeDOMRect(0, 0, 100, viewAreaHeight);
|
|
69
|
-
const childRect = makeDOMRect(0, childStart, 100, childHeight);
|
|
70
|
-
const {
|
|
71
|
-
viewingArea,
|
|
72
|
-
child
|
|
73
|
-
} = createVirtualDOM(viewingAreaRect, childRect);
|
|
74
|
-
|
|
75
|
-
viewingArea.getBoundingClientRect = () => viewingAreaRect;
|
|
76
|
-
|
|
77
|
-
viewingArea.scrollTop = 0;
|
|
78
|
-
|
|
79
|
-
child.getBoundingClientRect = () => childRect;
|
|
80
|
-
|
|
81
|
-
(0, _scrollIntoView.scrollIntoView)(child, viewingArea);
|
|
82
|
-
expect(scrollToMock).toHaveBeenCalledWith({
|
|
83
|
-
behavior: 'smooth',
|
|
84
|
-
top: expectedScrollPosition
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
describe('y-axis', () => {
|
|
88
|
-
it('scrolls the child into the viewing area when it is AFTER the overflow cutoff point', () => {
|
|
89
|
-
const scrollToMock = jest.fn();
|
|
90
|
-
Object.defineProperty(window.Element.prototype, 'scrollTo', {
|
|
91
|
-
writable: true,
|
|
92
|
-
value: scrollToMock
|
|
93
|
-
});
|
|
94
|
-
const childHeight = 50;
|
|
95
|
-
const viewAreaHeight = 100;
|
|
96
|
-
const childStart = viewAreaHeight + 10;
|
|
97
|
-
const scrollMargin = 10;
|
|
98
|
-
const expectedScrollPosition = scrollPositionFormula({
|
|
99
|
-
viewingAreaEdgePosition: viewAreaHeight,
|
|
100
|
-
childEdgePosition: childStart + childHeight,
|
|
101
|
-
margin: scrollMargin
|
|
102
|
-
}, false);
|
|
103
|
-
const viewingAreaRect = makeDOMRect(0, 0, 100, viewAreaHeight);
|
|
104
|
-
const childRect = makeDOMRect(0, childStart, 100, childHeight);
|
|
105
|
-
const {
|
|
106
|
-
viewingArea,
|
|
107
|
-
child
|
|
108
|
-
} = createVirtualDOM(viewingAreaRect, childRect);
|
|
109
|
-
|
|
110
|
-
viewingArea.getBoundingClientRect = () => viewingAreaRect;
|
|
111
|
-
|
|
112
|
-
viewingArea.scrollTop = 0;
|
|
113
|
-
|
|
114
|
-
child.getBoundingClientRect = () => childRect;
|
|
115
|
-
|
|
116
|
-
(0, _scrollIntoView.scrollIntoView)(child, viewingArea, {
|
|
117
|
-
direction: 'vertical',
|
|
118
|
-
startMargin: scrollMargin,
|
|
119
|
-
endMargin: scrollMargin,
|
|
120
|
-
behavior: 'auto'
|
|
121
|
-
});
|
|
122
|
-
expect(scrollToMock).toHaveBeenCalledWith({
|
|
123
|
-
behavior: 'auto',
|
|
124
|
-
top: expectedScrollPosition
|
|
125
|
-
});
|
|
126
|
-
});
|
|
127
|
-
it('scrolls the child into the viewing area when it is BEFORE the overflow cutoff point', () => {
|
|
128
|
-
const scrollToMock = jest.fn();
|
|
129
|
-
Object.defineProperty(window.Element.prototype, 'scrollTo', {
|
|
130
|
-
writable: true,
|
|
131
|
-
value: scrollToMock
|
|
132
|
-
});
|
|
133
|
-
const childHeight = 50;
|
|
134
|
-
const childStart = childHeight * -1 - 10;
|
|
135
|
-
const scrollMargin = 10;
|
|
136
|
-
const expectedScrollPosition = scrollPositionFormula({
|
|
137
|
-
viewingAreaEdgePosition: 0,
|
|
138
|
-
childEdgePosition: childStart,
|
|
139
|
-
margin: scrollMargin
|
|
140
|
-
}, true);
|
|
141
|
-
const viewingAreaRect = makeDOMRect(0, 0, 100, 100);
|
|
142
|
-
const childRect = makeDOMRect(0, childStart, 100, childHeight);
|
|
143
|
-
const {
|
|
144
|
-
viewingArea,
|
|
145
|
-
child
|
|
146
|
-
} = createVirtualDOM(viewingAreaRect, childRect);
|
|
147
|
-
|
|
148
|
-
viewingArea.getBoundingClientRect = () => viewingAreaRect;
|
|
149
|
-
|
|
150
|
-
viewingArea.scrollTop = 0;
|
|
151
|
-
|
|
152
|
-
child.getBoundingClientRect = () => childRect;
|
|
153
|
-
|
|
154
|
-
(0, _scrollIntoView.scrollIntoView)(child, viewingArea, {
|
|
155
|
-
direction: 'vertical',
|
|
156
|
-
startMargin: scrollMargin,
|
|
157
|
-
endMargin: scrollMargin,
|
|
158
|
-
behavior: 'auto'
|
|
159
|
-
});
|
|
160
|
-
expect(scrollToMock).toHaveBeenCalledWith({
|
|
161
|
-
behavior: 'auto',
|
|
162
|
-
top: expectedScrollPosition
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
describe('x-axis', () => {
|
|
167
|
-
it('scrolls the child into the viewing area when it is AFTER the overflow cutoff point', () => {
|
|
168
|
-
const scrollToMock = jest.fn();
|
|
169
|
-
Object.defineProperty(window.Element.prototype, 'scrollTo', {
|
|
170
|
-
writable: true,
|
|
171
|
-
value: scrollToMock
|
|
172
|
-
});
|
|
173
|
-
const childWidth = 50;
|
|
174
|
-
const viewAreaWidth = 100;
|
|
175
|
-
const childStart = viewAreaWidth + 10;
|
|
176
|
-
const scrollMargin = 10;
|
|
177
|
-
const expectedScrollPosition = scrollPositionFormula({
|
|
178
|
-
viewingAreaEdgePosition: viewAreaWidth,
|
|
179
|
-
childEdgePosition: childStart + childWidth,
|
|
180
|
-
margin: scrollMargin
|
|
181
|
-
}, false);
|
|
182
|
-
const viewingAreaRect = makeDOMRect(0, 0, 100, viewAreaWidth);
|
|
183
|
-
const childRect = makeDOMRect(childStart, 0, childWidth, 100);
|
|
184
|
-
const {
|
|
185
|
-
viewingArea,
|
|
186
|
-
child
|
|
187
|
-
} = createVirtualDOM(viewingAreaRect, childRect);
|
|
188
|
-
|
|
189
|
-
viewingArea.getBoundingClientRect = () => viewingAreaRect;
|
|
190
|
-
|
|
191
|
-
viewingArea.scrollLeft = 0;
|
|
192
|
-
|
|
193
|
-
child.getBoundingClientRect = () => childRect;
|
|
194
|
-
|
|
195
|
-
(0, _scrollIntoView.scrollIntoView)(child, viewingArea, {
|
|
196
|
-
direction: 'horizontal',
|
|
197
|
-
startMargin: scrollMargin,
|
|
198
|
-
endMargin: scrollMargin,
|
|
199
|
-
behavior: 'auto'
|
|
200
|
-
});
|
|
201
|
-
expect(scrollToMock).toHaveBeenCalledWith({
|
|
202
|
-
behavior: 'auto',
|
|
203
|
-
left: expectedScrollPosition
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
it('scrolls the child into the viewing area when it is BEFORE the overflow cutoff point', () => {
|
|
207
|
-
const scrollToMock = jest.fn();
|
|
208
|
-
Object.defineProperty(window.Element.prototype, 'scrollTo', {
|
|
209
|
-
writable: true,
|
|
210
|
-
value: scrollToMock
|
|
211
|
-
});
|
|
212
|
-
const childWidth = 50;
|
|
213
|
-
const childStart = childWidth * -1 - 10;
|
|
214
|
-
const scrollMargin = 10;
|
|
215
|
-
const expectedScrollPosition = scrollPositionFormula({
|
|
216
|
-
viewingAreaEdgePosition: 0,
|
|
217
|
-
childEdgePosition: childStart,
|
|
218
|
-
margin: scrollMargin
|
|
219
|
-
}, true);
|
|
220
|
-
const viewingAreaRect = makeDOMRect(0, 0, 100, 100);
|
|
221
|
-
const childRect = makeDOMRect(childStart, 0, childWidth, 100);
|
|
222
|
-
const {
|
|
223
|
-
viewingArea,
|
|
224
|
-
child
|
|
225
|
-
} = createVirtualDOM(viewingAreaRect, childRect);
|
|
226
|
-
|
|
227
|
-
viewingArea.getBoundingClientRect = () => viewingAreaRect;
|
|
228
|
-
|
|
229
|
-
viewingArea.scrollTop = 0;
|
|
230
|
-
|
|
231
|
-
child.getBoundingClientRect = () => childRect;
|
|
232
|
-
|
|
233
|
-
(0, _scrollIntoView.scrollIntoView)(child, viewingArea, {
|
|
234
|
-
direction: 'horizontal',
|
|
235
|
-
startMargin: scrollMargin,
|
|
236
|
-
endMargin: scrollMargin,
|
|
237
|
-
behavior: 'auto'
|
|
238
|
-
});
|
|
239
|
-
expect(scrollToMock).toHaveBeenCalledWith({
|
|
240
|
-
behavior: 'auto',
|
|
241
|
-
left: expectedScrollPosition
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
});
|
|
@@ -1,386 +0,0 @@
|
|
|
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
|
-
});
|