@instructure/ui-position 9.0.1 → 9.0.2-snapshot-0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@instructure/ui-position",
3
- "version": "9.0.1",
3
+ "version": "9.0.2-snapshot-0",
4
4
  "description": "A component for positioning content with respect to a designated target.",
5
5
  "author": "Instructure, Inc. Engineering and Product Design",
6
6
  "module": "./es/index.js",
@@ -24,24 +24,26 @@
24
24
  "license": "MIT",
25
25
  "dependencies": {
26
26
  "@babel/runtime": "^7.23.2",
27
- "@instructure/debounce": "9.0.1",
28
- "@instructure/emotion": "9.0.1",
29
- "@instructure/shared-types": "9.0.1",
30
- "@instructure/ui-dom-utils": "9.0.1",
31
- "@instructure/ui-portal": "9.0.1",
32
- "@instructure/ui-prop-types": "9.0.1",
33
- "@instructure/ui-react-utils": "9.0.1",
34
- "@instructure/ui-testable": "9.0.1",
35
- "@instructure/ui-utils": "9.0.1",
36
- "@instructure/uid": "9.0.1",
27
+ "@instructure/debounce": "9.0.2-snapshot-0",
28
+ "@instructure/emotion": "9.0.2-snapshot-0",
29
+ "@instructure/shared-types": "9.0.2-snapshot-0",
30
+ "@instructure/ui-dom-utils": "9.0.2-snapshot-0",
31
+ "@instructure/ui-portal": "9.0.2-snapshot-0",
32
+ "@instructure/ui-prop-types": "9.0.2-snapshot-0",
33
+ "@instructure/ui-react-utils": "9.0.2-snapshot-0",
34
+ "@instructure/ui-testable": "9.0.2-snapshot-0",
35
+ "@instructure/ui-utils": "9.0.2-snapshot-0",
36
+ "@instructure/uid": "9.0.2-snapshot-0",
37
37
  "prop-types": "^15.8.1"
38
38
  },
39
39
  "devDependencies": {
40
- "@instructure/ui-babel-preset": "9.0.1",
41
- "@instructure/ui-color-utils": "9.0.1",
42
- "@instructure/ui-test-locator": "9.0.1",
43
- "@instructure/ui-test-utils": "9.0.1",
44
- "@instructure/ui-themes": "9.0.1"
40
+ "@instructure/ui-babel-preset": "9.0.2-snapshot-0",
41
+ "@instructure/ui-color-utils": "9.0.2-snapshot-0",
42
+ "@instructure/ui-test-locator": "9.0.2-snapshot-0",
43
+ "@instructure/ui-test-utils": "9.0.2-snapshot-0",
44
+ "@instructure/ui-themes": "9.0.2-snapshot-0",
45
+ "@testing-library/jest-dom": "^6.1.4",
46
+ "@testing-library/react": "^14.1.2"
45
47
  },
46
48
  "peerDependencies": {
47
49
  "react": ">=16.8 <=18"
@@ -0,0 +1,586 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import React from 'react'
26
+ import { render, screen, waitFor } from '@testing-library/react'
27
+ import '@testing-library/jest-dom'
28
+ import { within } from '@instructure/ui-utils'
29
+
30
+ import { Position } from '../index'
31
+
32
+ describe('<Position />', () => {
33
+ const parentDefaults = {
34
+ width: 500,
35
+ height: 150,
36
+ padding: 100,
37
+ overflow: 'auto'
38
+ }
39
+
40
+ it('should render', () => {
41
+ render(
42
+ <div style={{ padding: '50px' }}>
43
+ <div style={{ ...parentDefaults }}>
44
+ <Position
45
+ constrain="window"
46
+ renderTarget={<button data-testid="target-btn">Target</button>}
47
+ >
48
+ <div data-testid="content">
49
+ <div>Content</div>
50
+ </div>
51
+ </Position>
52
+ </div>
53
+ </div>
54
+ )
55
+
56
+ const target = screen.getByTestId('target-btn')
57
+ const content = screen.getByTestId('content')
58
+
59
+ expect(target).toBeInTheDocument()
60
+ expect(target).toHaveTextContent('Target')
61
+ expect(content).toBeInTheDocument()
62
+ expect(content).toHaveTextContent('Content')
63
+ })
64
+
65
+ it('should absolutely position content', () => {
66
+ render(
67
+ <div style={{ padding: '50px' }}>
68
+ <div style={{ ...parentDefaults }}>
69
+ <Position
70
+ constrain="window"
71
+ renderTarget={<button data-testid="target">Target</button>}
72
+ >
73
+ <div data-testid="content">
74
+ <div>Content</div>
75
+ </div>
76
+ </Position>
77
+ </div>
78
+ </div>
79
+ )
80
+
81
+ const content = screen.getByTestId('content')
82
+ const style = window.getComputedStyle(content)
83
+
84
+ expect(style.position).toBe('absolute')
85
+ })
86
+
87
+ it('should render right of target', async () => {
88
+ const onPositionChanged = jest.fn()
89
+ render(
90
+ <div style={{ padding: '50px' }}>
91
+ <div style={{ ...parentDefaults }}>
92
+ <Position
93
+ placement="end"
94
+ onPositionChanged={onPositionChanged}
95
+ renderTarget={<button data-testid="target">Target</button>}
96
+ >
97
+ <div data-testid="content">
98
+ <div>Content</div>
99
+ </div>
100
+ </Position>
101
+ </div>
102
+ </div>
103
+ )
104
+
105
+ const target = screen.getByTestId('target')
106
+ const content = screen.getByTestId('content')
107
+
108
+ await waitFor(() => {
109
+ expect(onPositionChanged).toHaveBeenCalled()
110
+ })
111
+
112
+ const targetRect = target.getBoundingClientRect()
113
+ const contentRect = content.getBoundingClientRect()
114
+
115
+ expect(
116
+ within(Math.floor(contentRect.left), Math.floor(targetRect.right))
117
+ ).toBe(true)
118
+ })
119
+
120
+ it('should render below target', async () => {
121
+ const onPositionChanged = jest.fn()
122
+ render(
123
+ <div style={{ padding: '50px' }}>
124
+ <div style={{ ...parentDefaults }}>
125
+ <Position
126
+ placement="bottom"
127
+ onPositionChanged={onPositionChanged}
128
+ renderTarget={<button data-testid="target">Target</button>}
129
+ >
130
+ <div data-testid="content">
131
+ <div>Content</div>
132
+ </div>
133
+ </Position>
134
+ </div>
135
+ </div>
136
+ )
137
+
138
+ const target = screen.getByTestId('target')
139
+ const content = screen.getByTestId('content')
140
+
141
+ await waitFor(() => {
142
+ expect(onPositionChanged).toHaveBeenCalled()
143
+ })
144
+
145
+ const targetRect = target.getBoundingClientRect()
146
+ const contentRect = content.getBoundingClientRect()
147
+
148
+ expect(
149
+ within(Math.floor(contentRect.top), Math.floor(targetRect.bottom), 1)
150
+ ).toBe(true)
151
+ })
152
+
153
+ it('should render left of target', async () => {
154
+ const onPositionChanged = jest.fn()
155
+ render(
156
+ <div style={{ padding: '50px' }}>
157
+ <div style={{ ...parentDefaults }}>
158
+ <Position
159
+ placement="start"
160
+ onPositionChanged={onPositionChanged}
161
+ renderTarget={<button data-testid="target">Target</button>}
162
+ >
163
+ <div data-testid="content">
164
+ <div>Content</div>
165
+ </div>
166
+ </Position>
167
+ </div>
168
+ </div>
169
+ )
170
+
171
+ const target = screen.getByTestId('target')
172
+ const content = screen.getByTestId('content')
173
+
174
+ await waitFor(() => {
175
+ expect(onPositionChanged).toHaveBeenCalled()
176
+ })
177
+
178
+ const targetRect = target.getBoundingClientRect()
179
+ const contentRect = content.getBoundingClientRect()
180
+
181
+ expect(
182
+ within(Math.floor(contentRect.right), Math.floor(targetRect.left))
183
+ ).toBe(true)
184
+ })
185
+
186
+ it('should render above target', async () => {
187
+ const onPositionChanged = jest.fn()
188
+ render(
189
+ <div style={{ padding: '50px' }}>
190
+ <div style={{ ...parentDefaults }}>
191
+ <Position
192
+ placement="top"
193
+ onPositionChanged={onPositionChanged}
194
+ renderTarget={<button data-testid="target">Target</button>}
195
+ >
196
+ <div data-testid="content">
197
+ <div>Content</div>
198
+ </div>
199
+ </Position>
200
+ </div>
201
+ </div>
202
+ )
203
+
204
+ const target = screen.getByTestId('target')
205
+ const content = screen.getByTestId('content')
206
+
207
+ await waitFor(() => {
208
+ expect(onPositionChanged).toHaveBeenCalled()
209
+ })
210
+
211
+ const targetRect = target.getBoundingClientRect()
212
+ const contentRect = content.getBoundingClientRect()
213
+
214
+ expect(Math.floor(contentRect.bottom)).toEqual(Math.floor(targetRect.top))
215
+ })
216
+
217
+ it('should center vertically', async () => {
218
+ const onPositionChanged = jest.fn()
219
+ render(
220
+ <div style={{ padding: '50px' }}>
221
+ <div style={{ ...parentDefaults }}>
222
+ <Position
223
+ placement="end"
224
+ onPositionChanged={onPositionChanged}
225
+ renderTarget={<button data-testid="target">Target</button>}
226
+ >
227
+ <div data-testid="content">
228
+ <div>Content</div>
229
+ </div>
230
+ </Position>
231
+ </div>
232
+ </div>
233
+ )
234
+
235
+ const target = screen.getByTestId('target')
236
+ const content = screen.getByTestId('content')
237
+
238
+ await waitFor(() => {
239
+ expect(onPositionChanged).toHaveBeenCalled()
240
+ })
241
+
242
+ const targetRect = target.getBoundingClientRect()
243
+ const contentRect = content.getBoundingClientRect()
244
+
245
+ const top = Math.floor(contentRect.top)
246
+ const center = Math.floor(
247
+ targetRect.top + (targetRect.height / 2 - contentRect.height / 2)
248
+ )
249
+
250
+ expect(within(top, center)).toBe(true)
251
+ })
252
+
253
+ it('should center horizontally', async () => {
254
+ const onPositionChanged = jest.fn()
255
+ render(
256
+ <div style={{ padding: '50px' }}>
257
+ <div style={{ ...parentDefaults }}>
258
+ <Position
259
+ placement="bottom"
260
+ onPositionChanged={onPositionChanged}
261
+ renderTarget={<button data-testid="target">Target</button>}
262
+ >
263
+ <div data-testid="content">
264
+ <div>Content</div>
265
+ </div>
266
+ </Position>
267
+ </div>
268
+ </div>
269
+ )
270
+
271
+ const target = screen.getByTestId('target')
272
+ const content = screen.getByTestId('content')
273
+
274
+ await waitFor(() => {
275
+ expect(onPositionChanged).toHaveBeenCalled()
276
+ })
277
+
278
+ const targetRect = target.getBoundingClientRect()
279
+ const contentRect = content.getBoundingClientRect()
280
+
281
+ const left = Math.floor(contentRect.left)
282
+ const targetCenter = targetRect.width / 2
283
+ const contentCenter = contentRect.width / 2
284
+ const center = Math.floor(targetRect.left + (targetCenter - contentCenter))
285
+
286
+ expect(within(left, center)).toBe(true)
287
+ })
288
+
289
+ describe('when constrained to scroll-parent', () => {
290
+ it('should re-position below target', async () => {
291
+ const onPositionChanged = jest.fn()
292
+
293
+ render(
294
+ <div style={{ padding: '50px' }}>
295
+ <div data-testid="mountNode">mount</div>
296
+ <div
297
+ style={{
298
+ width: '50px',
299
+ height: '50px',
300
+ overflow: 'scroll',
301
+ padding: '0 50px 50px 50px'
302
+ }}
303
+ >
304
+ <Position
305
+ placement="top"
306
+ constrain="scroll-parent"
307
+ mountNode={() => document.getElementById('mountNode')}
308
+ onPositionChanged={onPositionChanged}
309
+ renderTarget={<button data-testid="target">Target</button>}
310
+ >
311
+ <div data-testid="content">
312
+ <div>Content</div>
313
+ </div>
314
+ </Position>
315
+ </div>
316
+ </div>
317
+ )
318
+
319
+ const target = screen.getByTestId('target')
320
+ const content = screen.getByTestId('content')
321
+
322
+ await waitFor(() => {
323
+ expect(onPositionChanged).toHaveBeenCalled()
324
+ })
325
+
326
+ const targetRect = target.getBoundingClientRect()
327
+ const contentRect = content.getBoundingClientRect()
328
+
329
+ expect(
330
+ within(Math.floor(contentRect.top), Math.floor(targetRect.bottom), 1)
331
+ ).toBe(true)
332
+ })
333
+
334
+ it('should re-position above target', async () => {
335
+ const onPositionChanged = jest.fn()
336
+
337
+ render(
338
+ <div style={{ padding: '50px' }}>
339
+ <div id="mountNode">mount</div>
340
+ <div
341
+ style={{
342
+ width: '50px',
343
+ height: '0px',
344
+ overflow: 'scroll',
345
+ padding: '50px 50px 0 50px'
346
+ }}
347
+ >
348
+ <Position
349
+ placement="bottom"
350
+ constrain="scroll-parent"
351
+ mountNode={() => document.getElementById('mountNode')}
352
+ onPositionChanged={onPositionChanged}
353
+ renderTarget={<button data-testid="target">Target</button>}
354
+ >
355
+ <div data-testid="content">
356
+ <div>Content</div>
357
+ </div>
358
+ </Position>
359
+ </div>
360
+ </div>
361
+ )
362
+
363
+ const target = screen.getByTestId('target')
364
+ const content = screen.getByTestId('content')
365
+
366
+ await waitFor(() => {
367
+ expect(onPositionChanged).toHaveBeenCalled()
368
+ })
369
+
370
+ const targetRect = target.getBoundingClientRect()
371
+ const contentRect = content.getBoundingClientRect()
372
+
373
+ expect(
374
+ within(Math.floor(contentRect.bottom), Math.floor(targetRect.top), 1)
375
+ ).toBe(true)
376
+ })
377
+
378
+ it('should re-position after target', async () => {
379
+ const onPositionChanged = jest.fn()
380
+
381
+ render(
382
+ <div style={{ padding: '50px' }}>
383
+ <div id="mountNode">mount</div>
384
+ <div
385
+ style={{
386
+ width: '50px',
387
+ height: '50px',
388
+ overflow: 'scroll',
389
+ padding: '50px 80px 50px 0'
390
+ }}
391
+ >
392
+ <Position
393
+ placement="start"
394
+ constrain="scroll-parent"
395
+ mountNode={() => document.getElementById('mountNode')}
396
+ onPositionChanged={onPositionChanged}
397
+ renderTarget={<button data-testid="target">Target</button>}
398
+ >
399
+ <div data-testid="content">
400
+ <div>Content</div>
401
+ </div>
402
+ </Position>
403
+ </div>
404
+ </div>
405
+ )
406
+
407
+ const target = screen.getByTestId('target')
408
+ const content = screen.getByTestId('content')
409
+
410
+ await waitFor(() => {
411
+ expect(onPositionChanged).toHaveBeenCalled()
412
+ })
413
+
414
+ const targetRect = target.getBoundingClientRect()
415
+ const contentRect = content.getBoundingClientRect()
416
+
417
+ expect(
418
+ within(Math.floor(contentRect.left), Math.floor(targetRect.right), 1)
419
+ ).toBe(true)
420
+ })
421
+
422
+ it('should re-position before target', async () => {
423
+ const onPositionChanged = jest.fn()
424
+
425
+ render(
426
+ <div style={{ padding: '50px' }}>
427
+ <div id="mountNode">mount</div>
428
+ <div
429
+ style={{
430
+ width: '50px',
431
+ height: '50px',
432
+ overflow: 'scroll',
433
+ padding: '50px 0px 50px 80px'
434
+ }}
435
+ >
436
+ <Position
437
+ placement="end"
438
+ constrain="scroll-parent"
439
+ mountNode={() => document.getElementById('mountNode')}
440
+ onPositionChanged={onPositionChanged}
441
+ renderTarget={<button data-testid="target">Target</button>}
442
+ >
443
+ <div data-testid="content">
444
+ <div>Content</div>
445
+ </div>
446
+ </Position>
447
+ </div>
448
+ </div>
449
+ )
450
+
451
+ const target = screen.getByTestId('target')
452
+ const content = screen.getByTestId('content')
453
+
454
+ await waitFor(() => {
455
+ expect(onPositionChanged).toHaveBeenCalled()
456
+ })
457
+
458
+ const targetRect = target.getBoundingClientRect()
459
+ const contentRect = content.getBoundingClientRect()
460
+
461
+ expect(
462
+ within(Math.floor(contentRect.right), Math.floor(targetRect.left), 1)
463
+ ).toBe(true)
464
+ })
465
+ })
466
+
467
+ describe('when the documentElement is offset', () => {
468
+ beforeEach(() => {
469
+ document.documentElement.style.position = 'fixed'
470
+ document.documentElement.style.top = '-100px'
471
+ })
472
+
473
+ afterEach(() => {
474
+ document.documentElement.style.position = ''
475
+ document.documentElement.style.top = ''
476
+ })
477
+
478
+ it('should position correctly', async () => {
479
+ render(
480
+ <div style={{ padding: '100px' }}>
481
+ <Position
482
+ placement="bottom"
483
+ renderTarget={<button data-testid="target">Target</button>}
484
+ >
485
+ <div data-testid="content">
486
+ <div>Content</div>
487
+ </div>
488
+ </Position>
489
+ </div>
490
+ )
491
+
492
+ const target = screen.getByTestId('target')
493
+ const content = screen.getByTestId('content')
494
+
495
+ await waitFor(() => {
496
+ expect(content.getBoundingClientRect().top).toEqual(
497
+ target.getBoundingClientRect().bottom
498
+ )
499
+ })
500
+ })
501
+
502
+ it('should position correctly with mountNode', async () => {
503
+ render(
504
+ <div style={{ padding: '100px' }}>
505
+ <div data-testid="mountNode">mount</div>
506
+ <div
507
+ style={{
508
+ width: '50px',
509
+ height: '50px',
510
+ overflow: 'scroll',
511
+ padding: '50px'
512
+ }}
513
+ >
514
+ <Position
515
+ placement="bottom"
516
+ constrain="scroll-parent"
517
+ mountNode={() => document.getElementById('mountNode')}
518
+ renderTarget={<button data-testid="target">Target</button>}
519
+ >
520
+ <div data-testid="content">
521
+ <div>Content</div>
522
+ </div>
523
+ </Position>
524
+ </div>
525
+ </div>
526
+ )
527
+
528
+ const target = screen.getByTestId('target')
529
+ const content = screen.getByTestId('content')
530
+
531
+ await waitFor(() => {
532
+ expect(content.getBoundingClientRect().top).toEqual(
533
+ target.getBoundingClientRect().bottom
534
+ )
535
+ })
536
+ })
537
+ })
538
+
539
+ describe('containerDisplay prop', () => {
540
+ it('should apply "inline-block"', () => {
541
+ const { container } = render(
542
+ <div style={{ padding: '50px' }}>
543
+ <div style={{ ...parentDefaults }}>
544
+ <Position
545
+ constrain="window"
546
+ renderTarget={<button data-testid="target">Target</button>}
547
+ containerDisplay="inline-block"
548
+ >
549
+ <div data-testid="content">
550
+ <div>Content</div>
551
+ </div>
552
+ </Position>
553
+ </div>
554
+ </div>
555
+ )
556
+
557
+ const position = container.querySelector("span[class$='-position']")
558
+ const style = getComputedStyle(position!)
559
+
560
+ expect(style.display).toEqual('inline-block')
561
+ })
562
+
563
+ it('should apply "block"', () => {
564
+ const { container } = render(
565
+ <div style={{ padding: '50px' }}>
566
+ <div style={{ ...parentDefaults }}>
567
+ <Position
568
+ constrain="window"
569
+ renderTarget={<button data-testid="target">Target</button>}
570
+ containerDisplay="block"
571
+ >
572
+ <div data-testid="content">
573
+ <div>Content</div>
574
+ </div>
575
+ </Position>
576
+ </div>
577
+ </div>
578
+ )
579
+
580
+ const position = container.querySelector("span[class$='-position']")
581
+ const style = getComputedStyle(position!)
582
+
583
+ expect(style.display).toEqual('block')
584
+ })
585
+ })
586
+ })