@aleph-alpha/ui-library 6.3.4 → 6.4.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.
Files changed (61) hide show
  1. package/config.js +2 -1
  2. package/dist/system/components/UiDropdownMenu/UiDropdownMenuSubContent.vue.d.ts +102 -2
  3. package/dist/system/components/UiDropdownMenu/UiDropdownMenuViewport.vue.d.ts +23 -0
  4. package/dist/system/components/UiDropdownMenu/menu-metrics.d.ts +4 -0
  5. package/dist/system/components/UiDropdownMenu/placeMobileSubmenu.d.ts +55 -0
  6. package/dist/system/components/UiDropdownMenu/types.d.ts +3 -0
  7. package/dist/system/components/UiDropdownMenu/useMobileSubmenuPlacement.d.ts +18 -0
  8. package/dist/system/components/UiFileUploadBox/UiFileUploadBox.vue.d.ts +2 -2
  9. package/dist/system/components/UiResizable/UiResizablePanel.vue.d.ts +2 -2
  10. package/dist/system/components/UiToggle/UiToggle.vue.d.ts +2 -1
  11. package/dist/system/components/UiToggle/types.d.ts +10 -2
  12. package/dist/system/components/UiToggleGroup/UiToggleGroupItem.vue.d.ts +5 -1
  13. package/dist/system/components/UiToggleGroup/types.d.ts +12 -2
  14. package/dist/system/components/core/dropdown-menu/DropdownMenuSubContent.vue.d.ts +2 -6
  15. package/dist/system/components/core/resizable/ResizablePanel.vue.d.ts +2 -2
  16. package/dist/system/components/core/toggle/Toggle.vue.d.ts +3 -1
  17. package/dist/system/components/core/toggle/index.d.ts +47 -2
  18. package/dist/system/components/core/toggle-group/ToggleGroup.vue.d.ts +3 -1
  19. package/dist/system/components/core/toggle-group/ToggleGroupItem.vue.d.ts +11 -1
  20. package/dist/system/components/core/toggle-group/context.d.ts +1 -0
  21. package/dist/system/components/core/toggle-group/index.d.ts +10 -2
  22. package/dist/system/lib.js +12660 -12479
  23. package/dist/system/patterns/UiFileUpload/UiFileUpload.vue.d.ts +2 -2
  24. package/dist/system/patterns/UiLayout/layout-shell.d.ts +171 -12
  25. package/dist/system/patterns/UiProfileCard/profile-card.d.ts +51 -1
  26. package/dist/system/patterns/UiTopBar/accessory-dropdown.d.ts +9 -4
  27. package/package.json +2 -2
  28. package/src/components/UiDropdownMenu/UiDropdownMenu.stories.ts +819 -31
  29. package/src/components/UiDropdownMenu/UiDropdownMenuContent.vue +4 -1
  30. package/src/components/UiDropdownMenu/UiDropdownMenuSubContent.vue +142 -1
  31. package/src/components/UiDropdownMenu/UiDropdownMenuSubTrigger.vue +28 -0
  32. package/src/components/UiDropdownMenu/UiDropdownMenuViewport.vue +171 -0
  33. package/src/components/UiDropdownMenu/__tests__/UiDropdownMenu.test.ts +326 -0
  34. package/src/components/UiDropdownMenu/__tests__/placeMobileSubmenu.test.ts +128 -0
  35. package/src/components/UiDropdownMenu/menu-metrics.ts +5 -0
  36. package/src/components/UiDropdownMenu/placeMobileSubmenu.ts +96 -0
  37. package/src/components/UiDropdownMenu/types.ts +3 -0
  38. package/src/components/UiDropdownMenu/useMobileSubmenuPlacement.ts +163 -0
  39. package/src/components/UiSnackbar/UiSnackbar.stories.ts +104 -32
  40. package/src/components/UiToggle/UiToggle.stories.ts +386 -261
  41. package/src/components/UiToggle/UiToggle.vue +2 -0
  42. package/src/components/UiToggle/__tests__/UiToggle.test.ts +63 -10
  43. package/src/components/UiToggle/types.ts +10 -2
  44. package/src/components/UiToggleGroup/UiToggleGroup.stories.ts +765 -30
  45. package/src/components/UiToggleGroup/UiToggleGroup.vue +2 -0
  46. package/src/components/UiToggleGroup/UiToggleGroupItem.vue +10 -1
  47. package/src/components/UiToggleGroup/__tests__/UiToggleGroup.test.ts +83 -4
  48. package/src/components/UiToggleGroup/types.ts +12 -2
  49. package/src/components/core/dropdown-menu/DropdownMenuContent.vue +1 -1
  50. package/src/components/core/dropdown-menu/DropdownMenuItem.vue +1 -1
  51. package/src/components/core/dropdown-menu/DropdownMenuSubContent.vue +31 -20
  52. package/src/components/core/toggle/Toggle.vue +12 -5
  53. package/src/components/core/toggle/index.ts +47 -19
  54. package/src/components/core/toggle-group/ToggleGroup.vue +20 -4
  55. package/src/components/core/toggle-group/ToggleGroupItem.vue +36 -8
  56. package/src/components/core/toggle-group/context.ts +1 -0
  57. package/src/components/core/toggle-group/index.ts +10 -24
  58. package/src/patterns/UiDataTable/UiDataTableColumnFilter.vue +1 -1
  59. package/src/patterns/UiDataTable/UiDataTableFilterMenu.vue +1 -1
  60. package/src/patterns/UiProfileCard/UiProfileCard.stories.ts +13 -6
  61. package/src/patterns/UiTopBar/accessory-dropdown.ts +5 -3
@@ -282,4 +282,330 @@ describe('UiDropdownMenuSub', () => {
282
282
  'data-[state=open]:bg-background-surface-muted-subtle',
283
283
  );
284
284
  });
285
+
286
+ test('washes the parent menu while the submenu is open', async () => {
287
+ const user = userEvent.setup();
288
+ const { getByRole } = render({
289
+ components: {
290
+ UiDropdownMenu,
291
+ UiDropdownMenuTrigger,
292
+ UiDropdownMenuContent,
293
+ UiDropdownMenuItem,
294
+ UiDropdownMenuSub,
295
+ UiDropdownMenuSubTrigger,
296
+ UiDropdownMenuSubContent,
297
+ },
298
+ template: `
299
+ <UiDropdownMenu>
300
+ <UiDropdownMenuTrigger>Open</UiDropdownMenuTrigger>
301
+ <UiDropdownMenuContent>
302
+ <UiDropdownMenuSub>
303
+ <UiDropdownMenuSubTrigger>More Options</UiDropdownMenuSubTrigger>
304
+ <UiDropdownMenuSubContent>
305
+ <UiDropdownMenuItem>Sub Item 1</UiDropdownMenuItem>
306
+ </UiDropdownMenuSubContent>
307
+ </UiDropdownMenuSub>
308
+ </UiDropdownMenuContent>
309
+ </UiDropdownMenu>
310
+ `,
311
+ });
312
+
313
+ await user.click(getByRole('button', { name: 'Open' }));
314
+ const trigger = getByRole('menuitem', { name: 'More Options' });
315
+ trigger.focus();
316
+ await user.keyboard('{ArrowRight}');
317
+
318
+ await waitFor(() => {
319
+ expect(getByRole('menuitem', { name: 'Sub Item 1' })).toBeInTheDocument();
320
+ });
321
+
322
+ const parent = document.querySelector(
323
+ '[data-slot="dropdown-menu-content"]',
324
+ );
325
+ const submenu = document.querySelector(
326
+ '[data-slot="dropdown-menu-sub-content"]',
327
+ );
328
+ const openTrigger =
329
+ '[data-slot="dropdown-menu-sub-trigger"][data-state="open"]';
330
+
331
+ expect(parent?.matches(`:has(${openTrigger})`)).toBe(true);
332
+ expect(submenu?.matches(`:has(${openTrigger})`)).toBe(false);
333
+ });
334
+
335
+ /**
336
+ * Pretends the window is a phone: the mobile media query matches, the panels
337
+ * have the given viewport boxes, and the viewport is `innerHeight` tall.
338
+ * @param geometry Boxes and viewport size to pretend.
339
+ * @param geometry.content Viewport box of the parent menu panel.
340
+ * @param geometry.subTrigger Viewport box of the sub trigger row.
341
+ * @param geometry.subContent Viewport box of the submenu panel.
342
+ * @param geometry.innerHeight Viewport height, in pixels.
343
+ * @returns Restores the original window and element behaviour.
344
+ */
345
+ function stubMobileGeometry(geometry: {
346
+ content: DOMRect;
347
+ subTrigger: DOMRect;
348
+ subContent: DOMRect;
349
+ innerHeight: number;
350
+ }) {
351
+ const originalMatchMedia = window.matchMedia;
352
+ const originalRect = HTMLElement.prototype.getBoundingClientRect;
353
+ const originalIntersectionObserver = window.IntersectionObserver;
354
+ const originalInnerHeight = window.innerHeight;
355
+
356
+ // A trigger with a real size makes floating-ui watch it for movement with
357
+ // an IntersectionObserver, which jsdom does not have.
358
+ window.IntersectionObserver = class {
359
+ observe = vi.fn();
360
+ unobserve = vi.fn();
361
+ disconnect = vi.fn();
362
+ takeRecords = vi.fn(() => []);
363
+ } as unknown as typeof IntersectionObserver;
364
+
365
+ window.matchMedia = ((query: string) => ({
366
+ matches: query === '(max-width: 639px)',
367
+ media: query,
368
+ onchange: null,
369
+ addListener: () => undefined,
370
+ removeListener: () => undefined,
371
+ addEventListener: () => undefined,
372
+ removeEventListener: () => undefined,
373
+ dispatchEvent: () => false,
374
+ })) as typeof window.matchMedia;
375
+
376
+ window.innerHeight = geometry.innerHeight;
377
+
378
+ HTMLElement.prototype.getBoundingClientRect =
379
+ function getBoundingClientRect() {
380
+ if (this.dataset.slot === 'dropdown-menu-content') {
381
+ return geometry.content;
382
+ }
383
+ if (this.dataset.slot === 'dropdown-menu-sub-trigger') {
384
+ return geometry.subTrigger;
385
+ }
386
+ if (this.dataset.slot === 'dropdown-menu-sub-content') {
387
+ return geometry.subContent;
388
+ }
389
+ return originalRect.call(this);
390
+ };
391
+
392
+ return () => {
393
+ window.matchMedia = originalMatchMedia;
394
+ window.IntersectionObserver = originalIntersectionObserver;
395
+ window.innerHeight = originalInnerHeight;
396
+ HTMLElement.prototype.getBoundingClientRect = originalRect;
397
+ };
398
+ }
399
+
400
+ /**
401
+ * Renders a menu with one submenu and opens that submenu with the keyboard.
402
+ * @returns The opened submenu panel and its trigger row.
403
+ */
404
+ async function openStackedSubmenu() {
405
+ const user = userEvent.setup();
406
+ const { getByRole } = render({
407
+ components: {
408
+ UiDropdownMenu,
409
+ UiDropdownMenuTrigger,
410
+ UiDropdownMenuContent,
411
+ UiDropdownMenuItem,
412
+ UiDropdownMenuSub,
413
+ UiDropdownMenuSubTrigger,
414
+ UiDropdownMenuSubContent,
415
+ },
416
+ template: `
417
+ <UiDropdownMenu>
418
+ <UiDropdownMenuTrigger>Open</UiDropdownMenuTrigger>
419
+ <UiDropdownMenuContent>
420
+ <UiDropdownMenuSub>
421
+ <UiDropdownMenuSubTrigger>More Options</UiDropdownMenuSubTrigger>
422
+ <UiDropdownMenuSubContent>
423
+ <UiDropdownMenuItem>Sub Item 1</UiDropdownMenuItem>
424
+ </UiDropdownMenuSubContent>
425
+ </UiDropdownMenuSub>
426
+ </UiDropdownMenuContent>
427
+ </UiDropdownMenu>
428
+ `,
429
+ });
430
+
431
+ await user.click(getByRole('button', { name: 'Open' }));
432
+ const trigger = getByRole('menuitem', { name: 'More Options' });
433
+ trigger.focus();
434
+ await user.keyboard('{ArrowRight}');
435
+
436
+ const submenu = await waitFor(() => {
437
+ const found = document.querySelector<HTMLElement>(
438
+ '[data-slot="dropdown-menu-sub-content"][data-mobile-side]',
439
+ );
440
+ expect(found).not.toBeNull();
441
+ return found as HTMLElement;
442
+ });
443
+ return { submenu, trigger };
444
+ }
445
+
446
+ test('stacks an open submenu 8px under its trigger row at the parent width', async () => {
447
+ const restore = stubMobileGeometry({
448
+ content: new DOMRect(40, 100, 220, 180),
449
+ subTrigger: new DOMRect(48, 108, 204, 36),
450
+ subContent: new DOMRect(0, 0, 160, 120),
451
+ innerHeight: 768,
452
+ });
453
+
454
+ try {
455
+ const { submenu, trigger } = await openStackedSubmenu();
456
+ const value = (name: string) =>
457
+ submenu.style.getPropertyValue(`--ui-dropdown-submenu-${name}`);
458
+ expect(submenu).toHaveAttribute('data-mobile-side', 'bottom');
459
+ expect(value('left')).toBe('40px');
460
+ expect(value('width')).toBe('220px');
461
+ // Row bottom (108 + 36) plus the 8px gap.
462
+ expect(value('top')).toBe('152px');
463
+ // The whole panel fits, so the scroll area is not capped.
464
+ expect(submenu).not.toHaveAttribute('data-mobile-clamped');
465
+ expect(value('scroll-max-height')).toBe('');
466
+
467
+ // The row bridges the gap toward the submenu.
468
+ expect(trigger).toHaveAttribute('data-mobile-submenu-side', 'bottom');
469
+ expect(trigger.style.getPropertyValue('--ui-dropdown-submenu-gap')).toBe(
470
+ '8px',
471
+ );
472
+ } finally {
473
+ restore();
474
+ }
475
+ });
476
+
477
+ test('caps the scroll area so the submenu ends at the viewport edge when neither side can hold it', async () => {
478
+ // A 190px panel from a row at 142–178 in a 320px viewport: 142px above
479
+ // and 142px below, neither enough. It opens upward and gives up the
480
+ // 56px it would have hung past the top.
481
+ const restore = stubMobileGeometry({
482
+ content: new DOMRect(0, 40, 320, 240),
483
+ subTrigger: new DOMRect(8, 142, 304, 36),
484
+ subContent: new DOMRect(0, 0, 320, 190),
485
+ innerHeight: 320,
486
+ });
487
+
488
+ try {
489
+ const { submenu } = await openStackedSubmenu();
490
+ const value = (name: string) =>
491
+ submenu.style.getPropertyValue(`--ui-dropdown-submenu-${name}`);
492
+ expect(submenu).toHaveAttribute('data-mobile-side', 'top');
493
+ expect(submenu).toHaveAttribute('data-mobile-clamped');
494
+ // jsdom draws no padding or border, so the whole 134px (142 - 8) goes
495
+ // to the scroll area, and the panel starts at the top edge.
496
+ expect(value('scroll-max-height')).toBe('134px');
497
+ expect(value('top')).toBe('0px');
498
+ } finally {
499
+ restore();
500
+ }
501
+ });
502
+ });
503
+
504
+ describe('UiDropdownMenuContent', () => {
505
+ test('fades the top and bottom rows when the menu overflows five rows', async () => {
506
+ const user = userEvent.setup();
507
+ const { getByRole } = render({
508
+ components: {
509
+ UiDropdownMenu,
510
+ UiDropdownMenuTrigger,
511
+ UiDropdownMenuContent,
512
+ UiDropdownMenuItem,
513
+ },
514
+ template: `
515
+ <UiDropdownMenu>
516
+ <UiDropdownMenuTrigger>Open</UiDropdownMenuTrigger>
517
+ <UiDropdownMenuContent>
518
+ <UiDropdownMenuItem>One</UiDropdownMenuItem>
519
+ <UiDropdownMenuItem>Two</UiDropdownMenuItem>
520
+ </UiDropdownMenuContent>
521
+ </UiDropdownMenu>
522
+ `,
523
+ });
524
+
525
+ await user.click(getByRole('button', { name: 'Open' }));
526
+ await waitFor(() => expect(getByRole('menu')).toBeInTheDocument());
527
+
528
+ const scroll = document.querySelector<HTMLElement>(
529
+ '[data-slot="dropdown-menu-scroll"]',
530
+ );
531
+ expect(scroll).not.toBeNull();
532
+ Object.defineProperty(scroll, 'scrollHeight', {
533
+ configurable: true,
534
+ get: () => 500,
535
+ });
536
+ Object.defineProperty(scroll, 'clientHeight', {
537
+ configurable: true,
538
+ get: () => 100,
539
+ });
540
+ scroll?.appendChild(document.createTextNode(''));
541
+
542
+ await waitFor(() => {
543
+ const bands = [
544
+ ...document.querySelectorAll<HTMLElement>('.ui-fade-mask__band'),
545
+ ];
546
+ expect(bands.map((band) => band.dataset.position)).toEqual(['bottom']);
547
+ // The band sits inside the panel padding, over the rows.
548
+ expect(bands[0]?.closest('[data-slot="dropdown-menu-viewport"]')).toBe(
549
+ scroll?.parentElement,
550
+ );
551
+ });
552
+
553
+ let top = 0;
554
+ Object.defineProperty(scroll, 'scrollTop', {
555
+ configurable: true,
556
+ get: () => top,
557
+ set: (value: number) => {
558
+ top = value;
559
+ },
560
+ });
561
+
562
+ top = 200;
563
+ scroll?.dispatchEvent(new Event('scroll'));
564
+ await waitFor(() => {
565
+ expect(
566
+ [...document.querySelectorAll('.ui-fade-mask__band')].map((band) =>
567
+ band.getAttribute('data-position'),
568
+ ),
569
+ ).toEqual(['top', 'bottom']);
570
+ });
571
+
572
+ top = 400;
573
+ scroll?.dispatchEvent(new Event('scroll'));
574
+ await waitFor(() => {
575
+ expect(
576
+ [...document.querySelectorAll('.ui-fade-mask__band')].map((band) =>
577
+ band.getAttribute('data-position'),
578
+ ),
579
+ ).toEqual(['top']);
580
+ });
581
+ });
582
+
583
+ test('leaves a menu that fits on screen without a fade', async () => {
584
+ const user = userEvent.setup();
585
+ const { getByRole } = render({
586
+ components: {
587
+ UiDropdownMenu,
588
+ UiDropdownMenuTrigger,
589
+ UiDropdownMenuContent,
590
+ UiDropdownMenuItem,
591
+ },
592
+ template: `
593
+ <UiDropdownMenu>
594
+ <UiDropdownMenuTrigger>Open</UiDropdownMenuTrigger>
595
+ <UiDropdownMenuContent>
596
+ <UiDropdownMenuItem>One</UiDropdownMenuItem>
597
+ </UiDropdownMenuContent>
598
+ </UiDropdownMenu>
599
+ `,
600
+ });
601
+
602
+ await user.click(getByRole('button', { name: 'Open' }));
603
+ await waitFor(() => expect(getByRole('menu')).toBeInTheDocument());
604
+
605
+ const viewport = document.querySelector(
606
+ '[data-slot="dropdown-menu-viewport"]',
607
+ );
608
+ expect(viewport).toHaveClass('ui-fade-mask');
609
+ expect(viewport).toHaveAttribute('data-fade', 'none');
610
+ });
285
611
  });
@@ -0,0 +1,128 @@
1
+ import { describe, expect, test } from 'vitest';
2
+ import { placeMobileSubmenu } from '../placeMobileSubmenu';
3
+
4
+ const parent = { left: 40, width: 220 };
5
+
6
+ describe('placeMobileSubmenu', () => {
7
+ test('opens 8px above the trigger row at the parent width when there is room above', () => {
8
+ expect(
9
+ placeMobileSubmenu({
10
+ trigger: { top: 600, bottom: 636 },
11
+ parent,
12
+ submenuHeight: 120,
13
+ viewportHeight: 800,
14
+ }),
15
+ ).toEqual({
16
+ side: 'top',
17
+ top: 472,
18
+ left: 40,
19
+ width: 220,
20
+ height: 120,
21
+ scrollMaxHeight: null,
22
+ });
23
+ });
24
+
25
+ test('opens 8px below the trigger row when only the space below can hold it', () => {
26
+ expect(
27
+ placeMobileSubmenu({
28
+ trigger: { top: 100, bottom: 136 },
29
+ parent: { left: 16, width: 180 },
30
+ submenuHeight: 120,
31
+ viewportHeight: 700,
32
+ }),
33
+ ).toEqual({
34
+ side: 'bottom',
35
+ top: 144,
36
+ left: 16,
37
+ width: 180,
38
+ height: 120,
39
+ scrollMaxHeight: null,
40
+ });
41
+ });
42
+
43
+ // Above is preferred even when both sides have room.
44
+ test('prefers above when both sides have room', () => {
45
+ expect(
46
+ placeMobileSubmenu({
47
+ trigger: { top: 400, bottom: 436 },
48
+ parent,
49
+ submenuHeight: 120,
50
+ viewportHeight: 800,
51
+ }).side,
52
+ ).toBe('top');
53
+ });
54
+
55
+ test('takes the larger side when neither can hold it', () => {
56
+ expect(
57
+ placeMobileSubmenu({
58
+ trigger: { top: 60, bottom: 96 },
59
+ parent,
60
+ submenuHeight: 300,
61
+ viewportHeight: 200,
62
+ }).side,
63
+ ).toBe('bottom');
64
+ });
65
+
66
+ // Neither side fits a 190px panel from a row at 142–178 in a 320px
67
+ // viewport. Unclamped it would start at -56px; instead the scroll area
68
+ // gives up the overflow while the 10px of padding and border stay.
69
+ test('shrinks the scroll area so the panel ends at the viewport edge when the chosen side is too short', () => {
70
+ expect(
71
+ placeMobileSubmenu({
72
+ trigger: { top: 142, bottom: 178 },
73
+ parent,
74
+ submenuHeight: 190,
75
+ chrome: 10,
76
+ viewportHeight: 320,
77
+ }),
78
+ ).toEqual({
79
+ side: 'top',
80
+ top: 0,
81
+ left: 40,
82
+ width: 220,
83
+ height: 134,
84
+ scrollMaxHeight: 124,
85
+ });
86
+ });
87
+
88
+ test('ends at the bottom edge when it has to open downward into too little room', () => {
89
+ const placement = placeMobileSubmenu({
90
+ trigger: { top: 20, bottom: 56 },
91
+ parent,
92
+ submenuHeight: 300,
93
+ chrome: 10,
94
+ viewportHeight: 200,
95
+ });
96
+
97
+ expect(placement.side).toBe('bottom');
98
+ expect(placement.top).toBe(64);
99
+ expect(placement.top + placement.height).toBe(200);
100
+ expect(placement.scrollMaxHeight).toBe(126);
101
+ });
102
+
103
+ test('never shrinks the panel below its own padding and borders', () => {
104
+ const placement = placeMobileSubmenu({
105
+ trigger: { top: 4, bottom: 40 },
106
+ parent,
107
+ submenuHeight: 300,
108
+ chrome: 10,
109
+ viewportHeight: 44,
110
+ });
111
+
112
+ expect(placement.height).toBe(10);
113
+ expect(placement.scrollMaxHeight).toBe(0);
114
+ });
115
+
116
+ // The gap is from the row, so the submenu is allowed to sit over the rest
117
+ // of the parent panel rather than being pushed past its edge.
118
+ test('measures from the trigger row rather than the parent panel', () => {
119
+ const placement = placeMobileSubmenu({
120
+ trigger: { top: 300, bottom: 336 },
121
+ parent,
122
+ submenuHeight: 120,
123
+ viewportHeight: 800,
124
+ });
125
+
126
+ expect(placement.top).toBe(300 - 8 - 120);
127
+ });
128
+ });
@@ -0,0 +1,5 @@
1
+ /** Matches the `sm` breakpoint: below it, the menu uses the mobile layout. */
2
+ export const DROPDOWN_MENU_MOBILE_MEDIA = '(max-width: 639px)';
3
+
4
+ /** Gap between the trigger row and the submenu stacked above or below it. */
5
+ export const DROPDOWN_MENU_SUBMENU_GAP_PX = 8;
@@ -0,0 +1,96 @@
1
+ import { DROPDOWN_MENU_SUBMENU_GAP_PX } from './menu-metrics';
2
+
3
+ /**
4
+ * The trigger row, the parent menu, and the submenu size used to place the
5
+ * submenu.
6
+ */
7
+ export interface MobileSubmenuPlacementInput {
8
+ /** The row that opened the submenu, in viewport coordinates. */
9
+ trigger: Pick<DOMRect, 'top' | 'bottom'>;
10
+ /** The parent menu panel, in viewport coordinates. */
11
+ parent: Pick<DOMRect, 'left' | 'width'>;
12
+ /** The submenu panel height when nothing constrains it, in pixels. */
13
+ submenuHeight: number;
14
+ /**
15
+ * Height of the panel around its scroll area (vertical padding and borders),
16
+ * in pixels. Kept when the scroll area has to shrink. Defaults to 0.
17
+ */
18
+ chrome?: number;
19
+ /** The viewport height, in pixels. */
20
+ viewportHeight: number;
21
+ /** The gap between the trigger row and the submenu, in pixels. */
22
+ gap?: number;
23
+ }
24
+
25
+ /**
26
+ * Where a submenu sits against its trigger row on a narrow screen.
27
+ */
28
+ export interface MobileSubmenuPlacement {
29
+ /** Edge of the trigger row the submenu opens from. */
30
+ side: 'top' | 'bottom';
31
+ /** Viewport `top` of the submenu, in pixels. */
32
+ top: number;
33
+ /** Viewport `left` of the submenu, in pixels. */
34
+ left: number;
35
+ /** Width of the submenu, matching the parent, in pixels. */
36
+ width: number;
37
+ /** Height of the submenu once it is fitted to the chosen side, in pixels. */
38
+ height: number;
39
+ /**
40
+ * Cap for the scroll area so the panel fits the chosen side, in pixels, or
41
+ * `null` when the whole panel fits without one.
42
+ */
43
+ scrollMaxHeight: number | null;
44
+ }
45
+
46
+ /**
47
+ * Places a submenu above or below the row that opened it, at the parent's
48
+ * width.
49
+ *
50
+ * The gap is measured from the trigger row, not from the parent panel, so the
51
+ * submenu may cover the rest of the parent. It opens upward whenever the space
52
+ * above can hold it, downward when only the space below can, and otherwise
53
+ * toward the larger side. When even that side is too short, the scroll area
54
+ * is capped so the panel ends at the viewport edge instead of past it, and
55
+ * every row stays reachable by scrolling.
56
+ * @param input Trigger row, parent panel, submenu height, and viewport height.
57
+ * @returns The side, viewport box, and scroll cap of the submenu.
58
+ */
59
+ export function placeMobileSubmenu(
60
+ input: MobileSubmenuPlacementInput,
61
+ ): MobileSubmenuPlacement {
62
+ const gap = input.gap ?? DROPDOWN_MENU_SUBMENU_GAP_PX;
63
+ const chrome = input.chrome ?? 0;
64
+ const spaceBelow = input.viewportHeight - input.trigger.bottom;
65
+ const spaceAbove = input.trigger.top;
66
+ const needed = input.submenuHeight + gap;
67
+ let side: MobileSubmenuPlacement['side'];
68
+ if (spaceAbove >= needed) {
69
+ side = 'top';
70
+ } else if (spaceBelow >= needed) {
71
+ side = 'bottom';
72
+ } else {
73
+ side = spaceAbove >= spaceBelow ? 'top' : 'bottom';
74
+ }
75
+
76
+ // The panel can never be shorter than its own padding and borders.
77
+ const available = Math.max(
78
+ (side === 'top' ? spaceAbove : spaceBelow) - gap,
79
+ chrome,
80
+ );
81
+ const height = Math.min(input.submenuHeight, available);
82
+ const scrollMaxHeight =
83
+ height < input.submenuHeight ? Math.max(0, height - chrome) : null;
84
+
85
+ return {
86
+ side,
87
+ left: input.parent.left,
88
+ width: input.parent.width,
89
+ height,
90
+ scrollMaxHeight,
91
+ top:
92
+ side === 'bottom'
93
+ ? input.trigger.bottom + gap
94
+ : input.trigger.top - gap - height,
95
+ };
96
+ }
@@ -258,6 +258,9 @@ export interface UiDropdownMenuSubTriggerProps {
258
258
  /**
259
259
  * Props for the UiDropdownMenuSubContent component.
260
260
  * The floating container for submenu items.
261
+ *
262
+ * On a narrow screen the submenu opens above or below the parent menu, at the
263
+ * parent's width, instead of to the side.
261
264
  */
262
265
  export interface UiDropdownMenuSubContentProps {
263
266
  /**