@primer/behaviors 0.0.0-202201891142 → 0.0.0-2022020145740

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 (82) hide show
  1. package/dist/cjs/anchored-position.d.ts +15 -0
  2. package/dist/cjs/anchored-position.js +210 -0
  3. package/dist/cjs/focus-trap.d.ts +2 -0
  4. package/dist/cjs/focus-trap.js +111 -0
  5. package/dist/cjs/focus-zone.d.ts +32 -0
  6. package/dist/cjs/focus-zone.js +410 -0
  7. package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  8. package/dist/cjs/index.js +16 -0
  9. package/dist/{polyfills → cjs/polyfills}/event-listener-signal.d.ts +0 -0
  10. package/dist/cjs/polyfills/event-listener-signal.js +44 -0
  11. package/dist/{scroll-into-view.d.ts → cjs/scroll-into-view.d.ts} +0 -0
  12. package/dist/cjs/scroll-into-view.js +21 -0
  13. package/dist/{utils → cjs/utils}/index.d.ts +0 -0
  14. package/dist/cjs/utils/index.js +15 -0
  15. package/dist/cjs/utils/iterate-focusable-elements.d.ts +8 -0
  16. package/dist/cjs/utils/iterate-focusable-elements.js +63 -0
  17. package/dist/{utils → cjs/utils}/unique-id.d.ts +0 -0
  18. package/dist/cjs/utils/unique-id.js +8 -0
  19. package/dist/{utils → cjs/utils}/user-agent.d.ts +0 -0
  20. package/dist/cjs/utils/user-agent.js +11 -0
  21. package/dist/esm/anchored-position.d.ts +15 -0
  22. package/dist/esm/anchored-position.js +206 -0
  23. package/dist/esm/focus-trap.d.ts +2 -0
  24. package/dist/esm/focus-trap.js +107 -0
  25. package/dist/esm/focus-zone.d.ts +32 -0
  26. package/dist/esm/focus-zone.js +406 -0
  27. package/{lib-esm → dist/esm}/index.d.ts +0 -0
  28. package/{lib/index.d.ts → dist/esm/index.js} +0 -0
  29. package/{lib-esm → dist/esm}/polyfills/event-listener-signal.d.ts +0 -0
  30. package/dist/esm/polyfills/event-listener-signal.js +40 -0
  31. package/{lib-esm → dist/esm}/scroll-into-view.d.ts +0 -0
  32. package/dist/esm/scroll-into-view.js +17 -0
  33. package/{lib-esm → dist/esm}/utils/index.d.ts +0 -0
  34. package/{lib/utils/index.d.ts → dist/esm/utils/index.js} +0 -0
  35. package/dist/esm/utils/iterate-focusable-elements.d.ts +8 -0
  36. package/dist/esm/utils/iterate-focusable-elements.js +57 -0
  37. package/{lib-esm → dist/esm}/utils/unique-id.d.ts +0 -0
  38. package/dist/esm/utils/unique-id.js +4 -0
  39. package/{lib-esm → dist/esm}/utils/user-agent.d.ts +0 -0
  40. package/dist/esm/utils/user-agent.js +7 -0
  41. package/package.json +18 -30
  42. package/utils/package.json +6 -6
  43. package/dist/anchored-position.d.ts +0 -89
  44. package/dist/focus-trap.d.ts +0 -12
  45. package/dist/focus-zone.d.ts +0 -137
  46. package/dist/utils/iterate-focusable-elements.d.ts +0 -42
  47. package/lib/__tests__/anchored-position.test.js +0 -388
  48. package/lib/__tests__/scroll-into-view.test.js +0 -245
  49. package/lib/anchored-position.d.ts +0 -89
  50. package/lib/anchored-position.js +0 -316
  51. package/lib/focus-trap.d.ts +0 -12
  52. package/lib/focus-trap.js +0 -179
  53. package/lib/focus-zone.d.ts +0 -137
  54. package/lib/focus-zone.js +0 -578
  55. package/lib/index.js +0 -57
  56. package/lib/polyfills/event-listener-signal.d.ts +0 -6
  57. package/lib/polyfills/event-listener-signal.js +0 -64
  58. package/lib/scroll-into-view.d.ts +0 -7
  59. package/lib/scroll-into-view.js +0 -42
  60. package/lib/utils/index.js +0 -44
  61. package/lib/utils/iterate-focusable-elements.d.ts +0 -42
  62. package/lib/utils/iterate-focusable-elements.js +0 -113
  63. package/lib/utils/unique-id.d.ts +0 -1
  64. package/lib/utils/unique-id.js +0 -12
  65. package/lib/utils/user-agent.d.ts +0 -1
  66. package/lib/utils/user-agent.js +0 -15
  67. package/lib-esm/__tests__/anchored-position.test.js +0 -386
  68. package/lib-esm/__tests__/scroll-into-view.test.js +0 -243
  69. package/lib-esm/anchored-position.d.ts +0 -89
  70. package/lib-esm/anchored-position.js +0 -309
  71. package/lib-esm/focus-trap.d.ts +0 -12
  72. package/lib-esm/focus-trap.js +0 -170
  73. package/lib-esm/focus-zone.d.ts +0 -137
  74. package/lib-esm/focus-zone.js +0 -559
  75. package/lib-esm/index.js +0 -4
  76. package/lib-esm/polyfills/event-listener-signal.js +0 -57
  77. package/lib-esm/scroll-into-view.js +0 -35
  78. package/lib-esm/utils/index.js +0 -3
  79. package/lib-esm/utils/iterate-focusable-elements.d.ts +0 -42
  80. package/lib-esm/utils/iterate-focusable-elements.js +0 -102
  81. package/lib-esm/utils/unique-id.js +0 -5
  82. package/lib-esm/utils/user-agent.js +0 -8
@@ -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
- });
@@ -1,243 +0,0 @@
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
- });